Question

I am fairly new to using Spring. I understand the concept of autowiring, but am confused on the implementation. I have a Maven multi-module project, and I am trying to Autowire the manager class in Module A so that I can use it in Module B. When I try to run the webapp, I get an error along the lines of:

No matching bean of type [com..Manager] found for               dependency: expected at least 1 bean which qualifies as             autowire candidate for this dependency.

Module A (jar): Manager.java

public interface Manager
{...}

ManagerImpl.java

@Service(value="manager")
public class ManagerImpl implements Manager
{...}

Module B (war): WebController.java

@Controller
public class WebController
{

@Autowired
private Manager manager;

..}

applicationContext.xml (in Module A)

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"         xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee"
    xmlns:lang="http://www.springframework.org/schema/lang" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.1.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-3.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd">

    <context:component-scan base-package="com....si" />

    <bean
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean
        class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />

</beans>
Was it helpful?

Solution 2

As module B has dependency for module A and module A has it's own application context file called applicationContext.xml , then make sure applicationContext.xml is getting bundled with module A jar to place it in classpath.

Now you need to add ContextLoaderListener in Module B web.xml,

   <context-param>
      <param-name>contextConfigLocation</param-name>
          <param-value>
              classpath*:/<package folder structure in jar>/applicationContext.xml      
          </param-value>
   </context-param>

   <listener>
       <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

Also you need to add following lines in both xmls , applicationContext.xml and mvc-dispatcher-servlet.xml

<context:component-scan base-package="<packages for stereotype>" />

With this, bean should get injected.

OTHER TIPS

Try adding <context:annotation-config/> to your applicationContext.xml. I've always included both to ensure complete scanning of the project. In addition, is the field you are trying to autowire within the base-package for the component scan?

The first thingh you have to do is to instantiate the "Manager", in order to perform the mapping between interface and implementation:

<bean id="ManagerBean" class="[package.].ManagerImpl/>

Then you have to instantiate the "Controller", in order to perform the mapping between manager and the ManagerImpl class

<bean id="Controller" class="[package.]WebController.java">
    <property name="manager" ref="ManagerBean" />
</bean>

Edit: both pieces of code should be placed in the xml

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top