简体   繁体   English

Spring @Autowired字段-NoSuchBeanDefinitionException

[英]Spring @Autowired Field - NoSuchBeanDefinitionException

I realize this question has been asked many times, but those answers don't seem to get me working. 我知道这个问题已经被问过很多次了,但是这些答案似乎并没有使我工作。

The class which has the @Autowired field: 具有@Autowired字段的类:

@Component
public class SpecialClaimsCaseManager {

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

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

The interface SpecialClaimsCaseRepositoryService 接口SpecialClaimsCaseRepositoryService

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

The implementation class (what should be injected) 实现类(应该注入什么)

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

mvcDispatcher.xml 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 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 (in another project) 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>

Full stack trace (fpasted because it's pretty long) http://fpaste.org/116696/14049194/ 完整的堆栈跟踪(由于它很长而被fpasted) http://fpaste.org/116696/14049194/

So, as we can see, mvc:annotation-driven is enabled, and autowiring is enabled. 因此,我们可以看到,启用了mvc:annotation-driven,并且启用了自动装配。 If I understand this correctly (I might not, pretty new to Spring), this should be all I need. 如果我正确理解了这一点(我可能不是,Spring的新手),这应该就是我所需要的。 SpecialClaimsCaseRepositoryService is an interface, if that matters, though I don't think it should as this same @Autowiring worked fine in another class annotated with @Controller . SpecialClaimsCaseRepositoryService是一个接口,如果那很重要,尽管我认为它不应该如此,因为同一@Autowiring在另一个用@Controller注释的类中也可以正常工作。

Thanks for any help! 谢谢你的帮助!

You'll notice from your stack trace that the exception occurs in the process of initializing the root application context loaded by the ContextLoaderListener . 您会从堆栈跟踪中注意到,在初始化由ContextLoaderListener加载的根应用程序上下文的过程中会发生异常。 That's taken from 这是从

<!-- 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>

In those two, you are scanning 在那两个中,您正在扫描

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

but not the com.redacted.sch.service... package that is required by one of the beans. 但不是其中一个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; 

In this case, xyz.model.SpecialClaimsCaseManager has an @Autowired field of type xyz.service.SpecialClaimsCaseRepositoryService , but no such bean exists. 在这种情况下, xyz.model.SpecialClaimsCaseManager具有类型为xyz.service.SpecialClaimsCaseRepositoryService@Autowired字段,但是不存在此类bean。

Don't mix component-scanned folders between application contexts, those loaded by ContextLoaderListener vs DispatcherServlet . 不要在应用程序上下文之间混合使用组件扫描的文件夹,这些文件夹由ContextLoaderListenerDispatcherServlet加载。 Refactor so that application beans are loaded by the ContextLoaderListener and controller-related beans are loaded by the DispatcherServlet . 重构,以便应用程序Bean由ContextLoaderListener加载,而与控制器相关的Bean由DispatcherServlet加载。

Reading: 读:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM