我是使用Spring的新手。我理解自动装配的概念,但对实施感到困惑。我有一个Maven多模块项目,我正在尝试Autowire模块A中的管理器类,以便我可以在模块B中使用它。当我尝试运行webapp时,我得到一个错误:
No matching bean of type [com..Manager] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
模块A(jar): Manager.java
public interface Manager
{...}
ManagerImpl.java
@Service(value="manager")
public class ManagerImpl implements Manager
{...}
模块B(战争): WebController.java
@Controller
public class WebController
{
@Autowired
private Manager manager;
..}
applicationContext.xml(在模块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>
答案 0 :(得分:1)
尝试将<context:annotation-config/>
添加到applicationContext.xml。
我总是包括两者以确保完整的项目扫描。另外,您尝试在组件扫描的基础包中自动装配的字段是什么?
答案 1 :(得分:0)
您要做的第一件事就是实例化“Manager”,以便在接口和实现之间执行映射:
<bean id="ManagerBean" class="[package.].ManagerImpl/>
然后你必须实例化“Controller”,以便执行manager和ManagerImpl类之间的映射
<bean id="Controller" class="[package.]WebController.java">
<property name="manager" ref="ManagerBean" />
</bean>
编辑:两段代码都应该放在xml
中答案 2 :(得分:0)
由于模块B对模块A具有依赖性,而模块A具有自己的应用程序上下文文件applicationContext.xml,因此请确保将applicationContext.xml与模块A jar捆绑在一起以将其放在类路径中。
现在需要在Module B web.xml中添加ContextLoaderListener,
<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>
此外,您还需要在xmls, applicationContext.xml和mvc-dispatcher-servlet.xml中添加以下行
<context:component-scan base-package="<packages for stereotype>" />
有了这个,就应该注入豆子。