Spring boot method interceptor

概述

需要对某些 service 方法添加日志和监控报警. 找了好长时间, 添加过程如下:

  1. 编写 @LogAndWarn 注解
  2. 编写拦截器 LogAndWarnInterceptor
  3. 编写切入点配置 LogAndWarnAdviser
  4. 在对应 Service 类或方法加上@LogAndWarn 注解, 并测试

编写 @LogAndWarn 注解代码

1@Target({ ElementType.TYPE, ElementType.METHOD }) 2@Retention(RetentionPolicy.RUNTIME) 3public @interface LogAndWarn { 4 5}

编写 LogAndWarnInterceptor 拦截器代码

1import org.aopalliance.intercept.MethodInterceptor; 2 3@Component 4public class LogAndWarnInterceptor implements MethodInterceptor { 5 6 @Override 7 public Object invoke(MethodInvocation invocation) throws Throwable { 8 System.out.println("#### before method"); 9 Object retVal = invocation.proceed(); 10 System.out.println("#### after method"); 11 return retVal; 12 } 13 14} 15 16

注: 此示例代码并没有实现具体逻辑, 详细实现可以参考: https://my.oschina.net/u/1169457/blog/1489767

编写 LogAndWarnAdviser 切入点配置代码

1@Component 2public class LogAndWarnAdviser extends AbstractPointcutAdvisor { 3 4 private static final long serialVersionUID = 1L; 5 6 @Autowired 7 private LogAndWarnInterceptor interceptor; 8 9 private final StaticMethodMatcherPointcut pointcut = new StaticMethodMatcherPointcut() { 10 @Override 11 public boolean matches(Method method, Class<?> targetClass) { 12 return method.isAnnotationPresent(LogAndWarn.class) || targetClass.isAnnotationPresent(LogAndWarn.class); 13 } 14 }; 15 16 @Override 17 public Pointcut getPointcut() { 18 return this.pointcut; 19 } 20 21 @Override 22 public Advice getAdvice() { 23 return this.interceptor; 24 } 25 26}

编写 service 代码, 查看执行结果

service简单这么写:

1 @LogAndWarn 2 public void test(){ 3 System.out.println("###### test..."); 4 }

通过 http 请求调用 controller 再调用 service 方法, 控制台输出如下:

1#### before method 2###### test... 3#### after method

参考:

http://blog.javaforge.net/post/76125490725/spring-aop-method-interceptor-annotation
http://todayleave.blogspot.jp/2016/02/spring-methodinterceptor.html https://my.oschina.net/u/1169457/blog/1489767

点赞
收藏

评论区

加载中...

相关推荐

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

spring注解

随着越来越多地使用Springboot敏捷开发,更多地使用注解配置Spring,而不是Spring的applicationContext.xml文件。Configuration注解:Spring解析为配置类,相当于spring配置文件Bean注解:容器注册Bean组件,默认id为方法名@Configurat

Springboot中如何在Utils类中使用@Autowired注入bean

Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类:1\.使用@Component注解标记工具类StatisticsUtils:2\.使用@Autowired(@Autowired和@Resource的区别不再介绍)注入我们需要的bean:3\.在工具类中

ubuntu下设置webstorm支持中文

ubuntu系统使用webstorm代码编写时添加中文注解发现在中文输入法下无法在IDE中输入中文字体。有问题,当然先谷歌咯,发现解决方法如下:1,打开webstorm安装路径下的bin/webstorm.sh文件2,在文件最前面添加如下代码:exportXMODIFIERS"@imfcitx"exportGTK

@Transactional 回滚不生效原因

事务的管理方式有两种,第一种是编程式事务管理,需要将数据库的自动提交等取消,并且需要自己编写事务代码,第二种则是声明式事务管理模式,spring利用springAOP特性编写了注解即题目中所提到的方式来管理事务,避免开发人员编写大量的事务代码。一、特性先来了解一下@Transactional注解的特性吧,可以更好排查问题1\.service类

Spring注解大全,汇总版

Spring使用的注解大全和解释注解解释@Controller组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类,然后将web请求映射到注解了@RequestMapping的方法上。@Service组合注解(组合了@Component注解),应用在