java - 我如何正确 Autowiring 此类和 applicationContext.xml?

标签 java spring spring-mvc autowired applicationcontext

我对使用 Spring 还很陌生。我理解 Autowiring 的概念,但对实现感到困惑。我有一个 Maven 多模块项目,我正在尝试 Autowiring 模块 A 中的管理器类,以便我可以在模块 B 中使用它。当我尝试运行 web 应用程序时,出现以下错误:

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( war ): 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>

最佳答案

尝试添加<context:annotation-config/>到你的applicationContext.xml。 我总是将两者都包含在内,以确保对项目进行完整的扫描。此外,您正在尝试在基础包中 Autowiring 该字段以进行组件扫描吗?

关于java - 我如何正确 Autowiring 此类和 applicationContext.xml?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17347478/

相关文章:

spring-mvc - OSGi Spring MVC 捆绑噩梦 - java.lang.ClassNotFoundException : org. springframework.context.config.ContextNamespaceHandler 从捆绑中找不到

java - 添加 HttpServletResponse/HttpServletRequest 作为方法参数时 Spring 请求挂起

java - 了解 servlet-api 中的请求对象。是单例吗?

java - StandardOpenOption 问题 java

java - 没有实现的子类

java - JSTL 无法打印对象列表

spring - 如何在服务层为@Transactional 时回滚单元测试

java - 仅为 ModelMapper 设置一次转换器

java - MediaPlayer 在 JavaFX 中陷入无限循环

java - Spring Hibernate 自定义 bean 验证 - NullPointerException