Get Session in Spring AOP

I'm not sure my codes are thread safe, who can help?

@Aspect
public class MyAspect {

   @Autowired
   private HttpSession session;


   @Before("...")
   private void myMethod() {
       seesion.getId();
   }

}

Since the MyAspect scope is by default (singleton), there are so many exsits at the same time, and also a lot of session.OK, what session do I get in my code? Is it thread safe? Or is this the wrong code, if it is wrong, how can I do?

Thank!

+4
source share
1 answer

That's right, that's fine.

  • Your MyAspectmust be registered as a bean in any case.

  • It doesn't matter if it's AOP Aspect or not: the dependency injection infrastructure is the same.

  • HttpSession. bean, Spring - WebApplicationContextUtils.SessionObjectFactory.

beanFactory.registerResolvableDependency(HttpSession.class, new SessionObjectFactory());

, SessionObjectFactory Proxy ThreadLocal<RequestAttributes>. , MyAspect.myMethod HttpSession, - -, .

, : , .

+5

Source: https://habr.com/ru/post/1534998/


All Articles