Spring请求范围bean代理bean无法注入

时间:2018-03-12 08:16:10

标签: java spring aop aspectj spring-aop

我有一个像这样的代理请求范围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不是最终的
  • 请求范围在AnyBean中与AspectJ代理(ScopedProxyMode.TARGET_CLASS)一起定义
  • 存在EnableAspectJAutoProxy注释
  • 以下jar类在类路径中:

    d:\用户\ liptak.m2 \库\有机\ AspectJ的\ aspectjweaver \ XXX \ aspectjweaver-xxx.jar d:\用户\ liptak.m2 \库\组织\ AspectJ的\ aspectjrt \ XXX \ aspectjrt-xxx.jar d:\用户\ liptak.m2 \库\ CGLIB \ CGLIB \ XXX \ CGLIB的xxx.jar d:\用户\ liptak.m2 \库\组织\ OW2 \ ASM \ ASM \ XXX \ ASM-xxx.jar d:\用户\ liptak.m2 \库\组织\ springframework的\弹簧AOP \ xxx.RELEASE \弹簧AOP-xxx.RELEASE.jar d:\用户\ liptak.m2 \库\ aopalliance \ aopalliance \ XXX \ aopalliance-xxx.jar d:\ Users \用户liptak.m2 \库\有机\ springframework的\弹簧方面\ xxx.RELEASE \弹簧方面-xxx.RELEASE.jar

看来,一切都应该没问题。它仍然无效。你有任何想法要检查吗?你会调试哪个Spring类?

UPDATE:

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)添加断点时,它根本不被触发。

1 个答案:

答案 0 :(得分:0)

解决方案是我的弹簧背景有点搞砸了。 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的配置混淆时,我:)