Announcement Announcement Module
Collapse
No announcement yet.
injecting session-scoped bean to aspect Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • injecting session-scoped bean to aspect

    Hello we are trying to inject session-scoped bean to an aspect

    the aspect class:
    Code:
    @aspect
    public class TestAspect {
        private ActiveSession activeSession;
        
        @Pointcut("within(x.y.dao..*)")
        public void inDataAccessLayer(){}
    
        @Around("inDataAccessLayer()")
        public Object log() {
            this.activeSession.getSomthing();
        }
    }
    in application-context.xml
    Code:
    <aop:aspectj-autoproxy/>
    <bean id="testAspect" class="x.y.aspect.TestAspect">
        <property name="activeSession" ref="activeSession"/>
    </bean>
    
    <bean id="activeSession" class="x.y.ActiveSession" scope="session">
        <aop:scoped-proxy/>
    </bean>
    However when the aspect try to get bean from context it throws exception like:
    Code:
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scopedTarget.activeSession': Scope 'session' 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 orignally 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.
    ..
    ..
    ..
    I guess the thread running the aspect is not the same as the one with the web request, is it the reason for the exception?
    Did we miss out something?
    Or is it possible to do what we try to achieve?

    Thanks

  • #2
    Originally posted by chris.lam View Post
    ...
    I guess the thread running the aspect is not the same as the one with the web request, is it the reason for the exception?
    That's correct. Feel free to check 'session' scope implementation - org.springframework.web.context.request.SessionSco pe.

    Comment

    Working...
    X