0

我对使用 Spring 相当陌生。我了解自动装配的概念,但对实现感到困惑。我有一个 Maven 多模块项目,我正在尝试自动装配模块 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>
4

3 回答 3

1

尝试添加<context:annotation-config/>到您的 applicationContext.xml。我一直将两者都包括在内,以确保对项目进行完整扫描。此外,您是否尝试在基本包中自动装配组件扫描的字段?

于 2013-06-27T15:56:49.800 回答
0

您要做的第一件事是实例化“Manager”,以便执行接口和实现之间的映射:

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

然后您必须实例化“控制器”,以便执行管理器和 ManagerImpl 类之间的映射

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

编辑:两段代码都应该放在xml中

于 2013-06-27T15:57:56.867 回答
0

由于模块 B 对模块 A 具有依赖关系,并且模块 A 有自己的应用程序上下文文件 applicationContext.xml ,因此请确保 applicationContext.xml 与模块 A jar 捆绑在一起以将其放置在类路径中。

现在您需要在模块 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>" />

有了这个,bean应该被注入。

于 2013-06-27T16:52:59.777 回答