Java Spring@Autowired字段-NoSuchBeanDefinitionException
我意识到这个问题已经被问过很多次了,但这些答案似乎并不能让我工作 具有@Autowired字段的类:Java Spring@Autowired字段-NoSuchBeanDefinitionException,java,spring,Java,Spring,我意识到这个问题已经被问过很多次了,但这些答案似乎并不能让我工作 具有@Autowired字段的类: @Component public class SpecialClaimsCaseManager { @Autowired private SpecialClaimsCaseRepositoryService<SpecialClaimsCaseDto> service; public SpecialClaimsCaseManager() { }
@Component
public class SpecialClaimsCaseManager {
@Autowired
private SpecialClaimsCaseRepositoryService<SpecialClaimsCaseDto> service;
public SpecialClaimsCaseManager() {
}
public Collection<SpecialClaimsCase> findAll() {
return convertToSpecialClaimsCase(service.findAll());
}
实现类(应该注入什么)
@Service(“SpecialClaimCaseRepositoryService”)
公共类SpecialClaimCaseRepositoryServiceImpl实现SpecialClaimCaseRepositoryService{
//一些方法实现,不相关
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>
特殊索赔处理
上下文配置位置
WEB-INF/application-security.xml
classpath*:sch_model_spring.xml
springSecurityFilterChain
org.springframework.web.filter.DelegatingFilterProxy
springSecurityFilterChain
/*
org.springframework.web.context.ContextLoaderListener
mvcDispatcher
org.springframework.web.servlet.DispatcherServlet
1.
mvcDispatcher
/*
5.
sch_model_spring.xml(在另一个项目中)
完整堆栈跟踪(因其相当长而暂停)
所以,正如我们所看到的,mvc:annotation-driven被启用,autowiring被启用,这应该是我所需要的。SpecialClaimCaseRepositoryService
是一个接口,如果这很重要的话,尽管我不认为它应该是相同的@Autowiring
在另一个用@Controller
注释的类中工作得很好
感谢您的帮助!您将从堆栈跟踪中注意到,在初始化由
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>
在本例中,xyz.model.specialClaimCaseManager
具有类型为xyz.service.specialClaimCaseRepositoryService
的@Autowired
字段,但不存在此类bean
不要在应用程序上下文之间混合组件扫描文件夹,由ContextLoaderListener
vsDispatcherServlet
加载的文件夹。重构以使应用程序bean由ContextLoaderListener
加载,控制器相关bean由DispatcherServlet
加载
阅读:
您将从堆栈跟踪中注意到,在初始化由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>
在本例中,xyz.model.specialClaimCaseManager
具有类型为xyz.service.specialClaimCaseRepositoryService
的@Autowired
字段,但不存在此类bean
不要在应用程序上下文之间混合组件扫描文件夹,由ContextLoaderListener
vsDispatcherServlet
加载的文件夹。重构以使应用程序bean由ContextLoaderListener
加载,控制器相关bean由DispatcherServlet
加载
阅读:
您将从堆栈跟踪中注意到,在初始化由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>
在本例中,xyz.model.specialClaimCaseManager
具有类型为xyz.service.specialClaimCaseRepositoryService
的@Autowired
字段,但不存在此类bean
不要在应用程序上下文之间混合组件扫描文件夹,由ContextLoaderListener
vsDispatcherServlet
加载的文件夹。重构以使应用程序bean由ContextLoaderListener
加载,控制器相关bean由DispatcherServlet
加载
阅读:
您将从堆栈跟踪中注意到,在初始化由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>
在本例中,xyz.model.specialClaimCaseManager
具有类型为xyz.service.specialClaimCaseRepositoryService
的@Autowired
字段,但不存在此类bean
不要在应用程序上下文之间混合组件扫描文件夹,由ContextLoaderListener
vsDispatcherServlet
加载的文件夹。重构以使应用程序bean由ContextLoaderListener
加载,控制器相关bean由DispatcherServlet
加载
阅读:
尝试将@组件
注释添加到SpecialClaimCaseMaseManager
中,并让我知道添加的@组件
,对SpecialClaimCaseRepositoryService
没有任何异常。如果有问题,SpecialClaimCaseRepositoryService
是一个接口。这@Autowiring
在ano中确实起作用用@Controller
注释的类。我将更新OP。您使用的是哪个版本的spring?在spring 4 RC1之前,您必须创建自己的impl并将其用作bean。另外,虽然很愚蠢,但可能是,SpecialClaimCaseRepositoryService
注释为@Service
或类似的吗?@ResidentBiscuit您也可以吗w正在运行的@Autowired
的控制器?@Narmer否。如果您有组件扫描
,则不需要注释配置
。OP,去掉AutowiredNotationBeanPostProcessor
,它是
<context:component-scan base-package="com.redacted.sch.model"/>
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.redacted.sch.service.SpecialClaimsCaseRepositoryService com.redacted.sch.model.SpecialClaimsCaseManager.specialClaimsCaseRepositoryService;