简体   繁体   中英

Session scoped bean in Aspect

I have problem autowiring session scoped bean into an Aspect.

My aspect looks like this:

@Aspect
public class AuditAspect {

   Logger logger = LoggerFactory.getLogger(this.getClass());

   @Autowired
   private AuditService auditService;

   @Autowired
   private SessionData sessionData;


   @AfterReturning(value = "@annotation(fasthiAudit) && execution(* *(..))")
   public void audit(JoinPoint joinPoint, FasthiAudit fasthiAudit) {
      final String className = joinPoint.getTarget().getClass().getName();
      final String methodName = joinPoint.getSignature().getName();

      try {

         UserId userId = sessionData.getUserId();
         TenantId tenantId = sessionData.getTenantId();

      } catch (Exception e) {
         logger.error("Could not log audit entry for method name: " + methodName + " in class " + className, e);
      }

   }

}

My SessionData bean is session scoped and looks like this:

@Component
@Scope(value = "session", proxyMode = ScopedProxyMode.TARGET_CLASS)
public class SessionData {

   private UserId userId;
   private TenantId tenantId;

   public UserId getUserId() {
      return userId;
   }

   public void setUserId(UserId userId) {
      this.userId = userId;
   }

   public TenantId getTenantId() {
      return tenantId;
   }

   public void setTenantId(TenantId tenantId) {
      this.tenantId = tenantId;
   }
}

In the aspect, the AuditService is autowired in okay and the SessionData is not null but it throws an Exception like

Method threw 'org.springframework.beans.factory.BeanCreationException' exception. Cannot evaluate se.reforce.fasthi.core.infrastructure.SessionData$$EnhancerBySpringCGLIB$$26c0d5bb.toString()

I have added a ContextLoaderListener to expose the request like this:

event.getServletContext().addListener(new RequestContextListener());

It works fine to autowire in the SessionData bean as a proxy in other singelton beans but the problem occurs in the aspect

What am I missing?

Thank you /Johan

I found the problem after a few days of headache. The problem was my Vaadin integration (that I forgot to mention in my question). The @Push annotation on my vaadin UI did something confusing with the servlet so that spring didn't recognize the session scoped beans. I solved this my changing the annotation to:

@Push(transport= Transport.WEBSOCKET_XHR)

That was it, now the session scoped beans work perfectly together with the singelton beans

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