我们可以在过滤器 class 中使用 @around 方法吗?

can we use @around method in filter class?

我有我的身份验证 class,我想在其中获取需要 class 中存在的 EntityManager 的东西。 class 仅在身份验证完成后才有效。

我已经尝试在身份验证 class 中导入那个 class 的 bean。然后我尝试在 Authentication class 中初始化 EntityManager。但是我没有从 class 那里得到我想要的东西。我查看了 AOP 并开始了解 @Around 注释,它需要在方法参数中包含 "ProceedingJoinPoint joinPoint" 。但由于我在身份验证 class 中实施了过滤器 class,因此我无法覆盖我的过滤器 class。我们可以解决这个问题吗?

在AOP中,你需要用@Around注解的方法并不是你要包装的方法,而是你要调用的方法'around'它(切面方法)。方法中的 joinPoint 参数代表你的 'wrapped' 方法,告诉它什么时候执行它。

我想举个例子最好理解。 考虑这个打印 'before' 和 'after' 执行的简单 AOP 方法:

这是方面class

@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");
}

这是 'wrapped' 方法:

public void testWrappedMethod(String whatever) {
  System.out.println("inside");
}

执行testWrappedMethod的输出将是:

before
inside
after