我有我的身份验证类,我想在其中获取需要类中存在的 EntityManager 的内容。该类仅在身份验证完成后才起作用。
我尝试在身份验证类中导入该类的 bean。然后我尝试在身份验证类中初始化EntityManager。但我没有从那堂课中得到我想要的东西。我查看了 AOP,了解了 @Around 注释,它需要在方法参数中包含“ProceedingJoinPoint joinPoint”。但由于我已经在身份验证类中实现了过滤器类,因此我无法覆盖我的过滤器类。我们可以解决这个问题吗?
最佳答案
在AOP中,你需要用@Around
注释的方法不是你想要包装的方法,而是你想要被“围绕”它调用的方法(方面方法)。方法中的 joinPoint
参数代表您的“包装”方法,并告诉它何时执行它。
我认为举个例子会更好理解。 考虑这个简单的 AOP 方法,它打印执行“之前”和“之后”:
这是方面类
@Around("execution(* testWrappedMethod(..))")
public void aopSample(ProceedingJoinPoint joinPoint) {
System.out.println("before");
joinPoint.proceed();// this will make the wrapped method execute
System.out.println("after");
}
这是“包装”方法:
public void testWrappedMethod(String whatever) {
System.out.println("inside");
}
执行testWrappedMethod
的输出将是:
before
inside
after
关于java - 我们可以在过滤器类中使用@around方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57760325/