我有一个代理的请求范围 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
我检查了什么:
- 启用注释处理,否则 AnyService 根本不会实例化
- AnyBean不是最终的
- 请求范围与AspectJ代理(ScopedProxyMode.TARGET_CLASS)一起在AnyBean中定义
- 存在启用方面JAuto代理注释
以下 jar 位于类路径上:
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\repository\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.释放.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.释放
.jar
所以看起来,一切都应该没事。它仍然不起作用。你有什么想法要检查吗?您将调试哪个 Spring 类?
更新:
Web XML 还包含 RequestContextListener:
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
更新 2:
当我将断点添加到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 的配置混为一谈:)