Spring MethodInterceptor—— 轻松实现aop功能的三种方式

为下一篇日志链路追踪做铺垫先了解下AOP,这一块我就不用从新写了 基本可以看看这一篇
原文链接:https://blog.csdn.net/u013905744/article/details/91364736

如果对于spring MethodInterceptor或spring aop概念不清楚,参考:spring aop, spring interceptor, springmvc interceptor有什么区别?

以下是在spring boot环境下执行的

在spring boot下有两种方式设置AOP(实现织入weave):

1. 使用@Aspect注解

2. 使用DefaultPointcutAdvisor

以实现TracingInterceptor为例

方法1:使用aspectj execution(切点) + interceptor(增强Advice)构成织入(DefaultPointcutAdvisor)

interceptor


 
 
  1. class TracingInterceptor implements MethodInterceptor {
  2. Object invoke(MethodInvocation i) throws Throwable {
  3. System.out.println( "method "+i.getMethod()+ " is called on "+
  4. i.getThis()+ " with args "+i.getArguments());
  5. Object ret=i.proceed();
  6. System.out.println( "method "+i.getMethod()+ " returns "+ret);
  7. return ret;
  8. }
  9. }

织入配置类

 


 
 
  1. @Configuration
  2. public class InterceptorConfig {
  3. public static final String traceExecution = "execution(* com.hfi.aop..*.*(..))";
  4. @Bean
  5. public DefaultPointcutAdvisor defaultPointcutAdvisor2() {
  6. TracingInterceptor interceptor = new TracingInterceptor();
  7. AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
  8. pointcut.setExpression(traceExecution);
  9. // 配置增强类advisor
  10. DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
  11. advisor.setPointcut(pointcut);
  12. advisor.setAdvice(interceptor);
  13. return advisor;
  14. }
  15. }

效果:当执行到com.hfi.aop包下的方法,当执行performEncore方法

可以看到我们配置的TracingInterceptor生效了

方法2:使用自定义注解(切点)+interceptor(增强Advice)构成织入(DefaultPointcutAdvisor)

自定义注解HfiTrace


 
 
  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface HfiTrace {
  5. }

interceptor


 
 
  1. public class TracingInterceptor implements MethodInterceptor {
  2. @Override
  3. public Object invoke(MethodInvocation invocation) throws Throwable {
  4. Method method = invocation.getMethod();
  5. HfiTrace annotation = getAnnotation(method);
  6. if (annotation == null) {
  7. return invocation.proceed();
  8. }
  9. // 为什么调用http://127.0.0.1:8089/jpademo/perform时有两次输出呢?
  10. // 因为在Audience里面用的是@Around,会拦截到两次
  11. System.out.println( "method " + invocation.getMethod() + " is called on " + invocation.getThis() + " with args" +
  12. " " + invocation.getArguments());
  13. Object proceed = invocation.proceed();
  14. System.out.println( "method " + invocation.getMethod() + " returns " + proceed);
  15. return proceed;
  16. }
  17. private HfiTrace getAnnotation(Method method) {
  18. // 如果有多个annotation 似乎就不好用了 如放在controller上 由于已经有了@RequestMapping注解了 所以...
  19. if (method.isAnnotationPresent(HfiTrace.class)) {
  20. return method.getAnnotation(HfiTrace.class);
  21. }
  22. return null;
  23. }
  24. }

织入配置类


 
 
  1. @Configuration
  2. public class InterceptorAnnotationConfig {
  3. @Bean
  4. public DefaultPointcutAdvisor defaultPointcutAdvisor3() {
  5. TracingInterceptor interceptor = new TracingInterceptor();
  6. // AnnotationMatchingPointcut pointcut = new AnnotationMatchingPointcut(HfiTrace.class, true);
  7. JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
  8. pointcut.setPatterns( "com.hfi.*");
  9. // 配置增强类advisor
  10. DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
  11. advisor.setPointcut(pointcut);
  12. advisor.setAdvice(interceptor);
  13. return advisor;
  14. }
  15. }

业务代码


 
 
  1. @HfiTrace
  2. @Override
  3. public String perform() {
  4. System.out.println( "perform...");
  5. return "perform";
  6. }

效果:

可以看到执行也是生效的

方法3:使用自定义注解(切点)+@Aspect(切面)构成织入

自定义注解HfiTrace


 
 
  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface HfiTrace {
  5. String name() default "默认注解信息";
  6. }

织入配置类


 
 
  1. @Component
  2. @Aspect
  3. public class TracingAspect {
  4. @Before( "@annotation(test)")
  5. public void beforeTest(JoinPoint point, HfiTrace test){
  6. System.out.println( "method " + point.getSignature().getName() + " is called on " + point.getThis() + " with " +
  7. "args" +
  8. " " + point.getArgs());
  9. System.out.println( "before invoke: "+ test.name());
  10. }
  11. @AfterReturning(value = "@annotation(test)", returning = "rvt")
  12. public void afterTest(JoinPoint point, HfiTrace test, Object rvt) {
  13. System.out.println( "method "+point.getSignature().getName() + " returns " + rvt);
  14. System.out.println( "after invoke: " + test.name());
  15. }
  16. }

业务代码:

controller层:


 
 
  1. @HfiTrace
  2. @GetMapping( "/perform")
  3. public String perform() {
  4. String perform = performance.perform();
  5. return perform;
  6. }
  7. @HfiTrace(name = "abc")
  8. @GetMapping( "/performEncore")
  9. public String performEncore() {
  10. // 强制转换
  11. Encoreable encoreable = (Encoreable) performance;
  12. return encoreable.performEncore();
  13. }

service层:


 
 
  1. @Component
  2. public class PerformanceImpl implements Performance {
  3. @HfiTrace
  4. @Override
  5. public String perform() {
  6. System.out.println( "perform...");
  7. return "perform";
  8. }
  9. }

效果:

综上:三种方式都可以实现相同的功能,方法3看起来最为简洁,只需要定义一个注解,然后写一个@Aspect切面类,就可以拦截指定方法的运行了

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: Spring AOP提供了四种方式实现面向切面编程,它们分别是:基于XML的配置方式、基于注解的方式、基于@AspectJ注解的方式和基于编程的方式。 1. 基于XML的配置方式:通过在XML配置文件中定义切点和通知,将切面逻辑与业务逻辑分离。可以使用<aop:config>元素来配置切面和通知,<aop:aspect>元素用于定义切面,<aop:pointcut>元素用于定义切点,<aop:before>、<aop:after>等元素用于定义通知。 2. 基于注解的方式:通过在Java类中使用注解来定义切面和通知。可以使用@Aspect注解来定义切面,@Pointcut注解来定义切点,@Before、@After等注解来定义通知。需要在Spring配置文件中启用注解支持,可以使用<context:annotation-config>或者<aop:aspectj-autoproxy>元素来实现。 3. 基于@AspectJ注解的方式:与基于注解的方式类似,但是使用了更强大的@AspectJ注解来定义切面和通知。可以使用@Aspect注解来定义切面,@Pointcut注解来定义切点,@Before、@After等注解来定义通知。需要在Spring配置文件中启用@AspectJ支持,可以使用<aop:aspectj-autoproxy>元素来实现。 4. 基于编程的方式:通过编写Java代码来实现切面和通知。可以使用ProxyFactoryBean类来创建代理对象,使用Advice接口的实现类来定义通知。可以在Java类中直接编写切面逻辑,也可以通过实现MethodInterceptor接口来编写通知逻辑。 这些方式都可以实现面向切面编程,选择哪种方式取决于具体的需求和个人偏好。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值