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

标签 java aop spring-aop restful-authentication

我有我的身份验证类,我想在其中获取需要类中存在的 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/

相关文章:

java - 编译/加载时间用 Spring 编织

java - 将二维字符数组转换为字符串

java - 为什么这个 MinDepth 级别的解决方案与递归解决方案相比如此慢?

Spring 依赖注入(inject)或切面编程

java - 是否可以使用 Spring 在两个不同的数据库(MongoDB 和 MySQL)中进行包含插入的事务?

java - for bean 使用 spEL 的 Spring AOP 代理创建错误

java - JLabel 的变量不会居中?

java - redis pub sub with jedis , sub crashes with error

c# - 如何使用带有 Serilog 的 postsharp 来分离日志记录方面?

java - 在 Spring 的单个项目中同时使用 XML 和注释配置是个坏主意吗?