【问题标题】:can we use @around method in filter class?我们可以在过滤器类中使用@around 方法吗?
【发布时间】:2020-01-05 16:34:25
【问题描述】:

我有我的身份验证类,我想在其中获取需要存在于类中的 EntityManager 的东西。该类仅在完成身份验证后才起作用。

我已经尝试在身份验证类中导入该类的 bean。然后我尝试在 Authentication 类中初始化 EntityManager。但我没有从那节课上得到我想要的东西。我查看了 AOP 并了解了 @Around 注释,它需要在方法参数中有“ProceedingJoinPoint joinPoint”。但是由于我在 Authentication 类中实现了 Filter 类,所以我无法覆盖我的过滤器类。我们可以解决这个问题吗?

【问题讨论】:

  • 欢迎来到 StackOverflow。你的代码在哪里?请提供MCVE,因为我无法编译和调试您的模棱两可的散文。非常感谢。

标签: java aop spring-aop restful-authentication


【解决方案1】:

在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 的执行输出将是:

之前
里面
之后

【讨论】:

  • 感谢您的帮助。但是我在这里还有一个问题,我们可以使用相同的方法在 aopSample 方法的 testWrappedmethod 中设置一个变量吗?
猜你喜欢
  • 2013-09-04
  • 1970-01-01
  • 2014-06-30
  • 1970-01-01
  • 2018-07-17
  • 1970-01-01
  • 2013-06-28
  • 1970-01-01
  • 2011-02-24
相关资源
最近更新 更多