简体   繁体   中英

Spring request scope bean proxy bean can not be injected

I have a proxied request scope bean like this:

@Component
@Scope(value="request", proxyMode = ScopedProxyMode.TARGET_CLASS)
@Lazy
public class AnyBean {
...
}

I want to have it injected into a service class:

@Service
@Transactional
public class AnyService {
    @Autowired
    private AnyBean anyBean;
}

When I start my application and or my integration tests, then it won't start. Application looks like this:

@Configuration
@EnableCommonsPersistenceAutoConfguration
@ImportResource({
   ....
})
@EnableAspectJAutoProxy
public class ApplicationTest {
...
}

The error I get:

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

What I checked:

  • Annotation processing is enabled, otherwise AnyService would not instantiate at all
  • AnyBean is not final
  • Request scope is defined in AnyBean along with the AspectJ Proxy (ScopedProxyMode.TARGET_CLASS)
  • EnableAspectJAutoProxy annotation is present
  • The following jars are on the classpath:

    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.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

So it seems, that everything should be fine. Still it does not work. Do you have any ideas to check? Which Spring class would you debug?

UPDATE:

Web XML contains RequestContextListener as well:

<listener>
    <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>

UPDATE 2:

When I add a breakpoint to org.springframework.aop.config.ScopedProxyBeanDefinitionDecorator.decorate(Node, BeanDefinitionHolder, ParserContext), it is not triggered at all.

Solution is that my spring context is messed up a bit. AnyBean is not instantiated based on the annotation processing. I have found it in an XML config, where it is created directly like this:

<bean id="anyBean" class="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.AnyBean"  scope="request" lazy-init="true"/>

As soon as I change it like this, it works:

<bean id="anyBean" class="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.AnyBean"  scope="request" lazy-init="true">
    <aop:scoped-proxy proxy-target-class="true"/>
</bean>

I love when annotation and xml based config is mixed up :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM