[英]Spring request scope bean proxy bean can not be injected
我有一个这样的代理请求范围bean:
@Component
@Scope(value="request", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Lazy
public class AnyBean {
...
}
我想将其注入服务类中:
@Service
@Transactional
public class AnyService {
@Autowired
private AnyBean anyBean;
}
当我启动我的应用程序和/或集成测试时,它就不会启动。 应用程序如下所示:
@Configuration
@EnableCommonsPersistenceAutoConfguration
@ImportResource({
....
})
@EnableAspectJAutoProxy
public class ApplicationTest {
...
}
我得到的错误:
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'anyBean': Scope 'request' is not active for the current thread; consider defining a scoped proxy for this bean if you intend to refer to it from a singleton; nested exception is java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:352)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1127)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1051)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:949)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
... 57 more
Caused by: java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread? If you are actually operating within a web request and still receive this message, your code is probably running outside of DispatcherServlet/DispatcherPortlet: In this case, use RequestContextListener or RequestContextFilter to expose the current request.
at org.springframework.web.context.request.RequestContextHolder.currentRequestAttributes(RequestContextHolder.java:131)
at org.springframework.web.context.request.AbstractRequestAttributesScope.get(AbstractRequestAttributesScope.java:41)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:337)
... 62 more
我检查了什么:
以下罐子位于类路径中:
D:\\ Users \\ liptak.m2 \\ repository \\ org \\ aspectj \\ aspectjweaver \\ xxx \\ aspectjweaver-xxx.jar D:\\ Users \\ liptak.m2 \\ repository \\ org \\ aspectj \\ aspectjrt \\ xxx \\ aspectjrt-xxx.jar D: \\ Users \\ liptak.m2 \\存储库\\ cglib \\ cglib \\ xxx \\ cglib-xxx.jar D:\\ Users \\ liptak.m2 \\ repository \\ org \\ ow2 \\ asm \\ asm \\ xxx \\ asm-xxx.jar D:\\ Users \\ liptak.m2 \\ repository \\ org \\ springframework \\ spring-aop \\ xxx.RELEASE \\ spring-aop-xxx.RELEASE.jar D:\\ Users \\ liptak.m2 \\ repository \\ aopalliance \\ aopalliance \\ xxx \\ aopalliance-xxx.jar D:\\ Users \\ liptak.m2 \\ repository \\ org \\ springframework \\ spring-aspects \\ xxx.RELEASE \\ spring-aspects-xxx.RELEASE.jar
看来,一切都应该很好。 仍然不起作用。 您有什么想法要检查吗? 您将调试哪个Spring类?
Web XML还包含RequestContextListener:
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
当我向org.springframework.aop.config.ScopedProxyBeanDefinitionDecorator.decorate(Node,BeanDefinitionHolder,ParserContext)添加一个断点时,根本不会触发它。
解决的办法是我的春季背景有点混乱。 没有基于注释处理实例化AnyBean。 我已经在XML配置中找到了它,在其中直接像这样创建它:
<bean id="anyBean" class="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.AnyBean" scope="request" lazy-init="true"/>
一旦我像这样更改它,它就会起作用:
<bean id="anyBean" class="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.AnyBean" scope="request" lazy-init="true">
<aop:scoped-proxy proxy-target-class="true"/>
</bean>
当注释和基于xml的配置混合在一起时,我很喜欢 :)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.