Spring @Autowired Field - NoSuchBeanDefinitionException

时间:2014-07-09 14:13:19

标签: java spring

我意识到这个问题已被多次询问,但这些答案似乎并没有让我工作。

具有@Autowired字段的类:

@Component
public class SpecialClaimsCaseManager {

    @Autowired
    private SpecialClaimsCaseRepositoryService<SpecialClaimsCaseDto> service;
    public SpecialClaimsCaseManager() {
    }

    public Collection<SpecialClaimsCase> findAll() {
        return convertToSpecialClaimsCase(service.findAll());
    }

界面SpecialClaimsCaseRepositoryService

public interface SpecialClaimsCaseRepositoryService<C extends SpecialClaimsCaseDto> {
    //Some method signatures, not relevant

实现类(应注入的内容)

@Service("specialClaimsCaseRepositoryService") 
public class SpecialClaimsCaseRepositoryServiceImpl implements SpecialClaimsCaseRepositoryService<SpecialClaimsCaseDto> {
    //Some method implementations, not relevant

mvcDispatcher.xml

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xmlns:mvc="http://www.springframework.org/schema/mvc"
           xsi:schemaLocation="http://www.springframework.org/schema/mvc
                http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
                http://www.springframework.org/schema/beans
                http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                http://www.springframework.org/schema/context
                http://www.springframework.org/schema/context/spring-context-3.0.xsd">

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

        <mvc:resources mapping="/resources/**" location="/resources/" />

        <mvc:annotation-driven />
        <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

        <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
           <property name="prefix" value="/WEB-INF/views/" />
           <property name="suffix" value=".jsp" />
        </bean>

    </beans>

的web.xml

    <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
    <display-name>SpecialClaimsHandling</display-name>

    <!-- Spring Configuration Files -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            WEB-INF/application-security.xml
            classpath*:sch_model_spring.xml
        </param-value>
    </context-param>

    <!-- Spring Security Filters -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>
             org.springframework.web.filter.DelegatingFilterProxy
        </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

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

    <!-- MVC Filter -->
    <servlet>
        <servlet-name>mvcDispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvcDispatcher</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

    <!-- Session Configuration -->
    <session-config>
        <session-timeout>5</session-timeout>
    </session-config>

</web-app>

sch_model_spring.xml(在另一个项目中)

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">

        <context:component-scan base-package="com.redacted.sch.model"/>

        <tx:annotation-driven />
        <tx:jta-transaction-manager />

        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
            <property name="url" value="redacted" />
            <property name="username" value="redacted" />
            <property name="password" value="redacted" />
        </bean>

        <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
              <property name="persistenceUnitName" value="schManager" />
              <property name="dataSource" ref="dataSource" />
        </bean>

        <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean> 
</beans>

完整堆栈跟踪(因为它很长)http://fpaste.org/116696/14049194/

因此,正如我们所看到的,启用了mvc:annotation-driven,并启用了自动装配。如果我理解正确(我可能不会,对Spring来说很新),这应该是我所需要的。 SpecialClaimsCaseRepositoryService是一个接口,如果这很重要,虽然我认为不应该这样,@Autowiring在另一个用@Controller注释的类中工作正常。

感谢您的帮助!

1 个答案:

答案 0 :(得分:3)

您将从堆栈跟踪中注意到,在初始化ContextLoaderListener加载的根应用程序上下文的过程中发生了异常。这取自

<!-- Spring Configuration Files -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        WEB-INF/application-security.xml
        classpath*:sch_model_spring.xml
    </param-value>
</context-param>

在这两个中,你正在扫描

<context:component-scan base-package="com.redacted.sch.model"/>

但不是其中一个bean所需的com.redacted.sch.service...包。

Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.redacted.sch.service.SpecialClaimsCaseRepositoryService com.redacted.sch.model.SpecialClaimsCaseManager.specialClaimsCaseRepositoryService; 

在这种情况下,xyz.model.SpecialClaimsCaseManager的{​​{1}}字段类型为@Autowired,但不存在此类bean。

不要在应用程序上下文之间混合使用组件扫描的文件夹,这些文件夹由xyz.service.SpecialClaimsCaseRepositoryServiceContextLoaderListener加载。重构以便DispatcherServlet加载应用程序bean,并且ContextLoaderListener加载与控制器相关的bean。

读: