簡體   English   中英

Spring請求范圍bean代理bean無法注入

[英]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

我檢查了什么:

  • 注釋處理已啟用,否則AnyService將完全不實例化
  • AnyBean不是最終的
  • 請求范圍與AspectJ代理(ScopedProxyMode.TARGET_CLASS)一起在AnyBean中定義。
  • 存在EnableAspectJAutoProxy批注
  • 以下罐子位於類路徑中:

    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>

更新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的配置混合在一起時,我很喜歡 :)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM