Spring boot method interceptor

Stella981
• 阅读 578

概述

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

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

编写 @LogAndWarn 注解代码

@Target({ ElementType.TYPE, ElementType.METHOD })
@Retention(RetentionPolicy.RUNTIME)
public @interface LogAndWarn {

}

编写 LogAndWarnInterceptor 拦截器代码

import org.aopalliance.intercept.MethodInterceptor;

@Component
public class LogAndWarnInterceptor implements MethodInterceptor {

  @Override
  public Object invoke(MethodInvocation invocation) throws Throwable {
    System.out.println("#### before method");
    Object retVal = invocation.proceed();
    System.out.println("#### after method");
    return retVal;
  }

}

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

编写 LogAndWarnAdviser 切入点配置代码

@Component
public class LogAndWarnAdviser extends AbstractPointcutAdvisor {

  private static final long serialVersionUID = 1L;

  @Autowired
  private LogAndWarnInterceptor interceptor;

  private final StaticMethodMatcherPointcut pointcut = new StaticMethodMatcherPointcut() {
    @Override
    public boolean matches(Method method, Class<?> targetClass) {
      return method.isAnnotationPresent(LogAndWarn.class) || targetClass.isAnnotationPresent(LogAndWarn.class);
    }
  };

  @Override
  public Pointcut getPointcut() {
    return this.pointcut;
  }

  @Override
  public Advice getAdvice() {
    return this.interceptor;
  }

}

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

service简单这么写:

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

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

#### before method
###### test...
#### 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

点赞
收藏
评论区
推荐文章
Easter79 Easter79
2年前
spring注解
随着越来越多地使用Springboot敏捷开发,更多地使用注解配置Spring,而不是Spring的applicationContext.xml文件。Configuration注解:Spring解析为配置类,相当于spring配置文件Bean注解:容器注册Bean组件,默认id为方法名@Configurat
Easter79 Easter79
2年前
Springboot中如何在Utils类中使用@Autowired注入bean
Springboot中如果希望在Utils工具类中,使用到我们已经定义过的Dao层或者Service层Bean,可以如下编写Utils类:1\.使用@Component注解标记工具类StatisticsUtils:2\.使用@Autowired(@Autowired和@Resource的区别不再介绍)注入我们需要的bean:3\.在工具类中
Wesley13 Wesley13
2年前
ubuntu下设置webstorm支持中文
ubuntu系统使用webstorm代码编写时添加中文注解发现在中文输入法下无法在IDE中输入中文字体。有问题,当然先谷歌咯,发现解决方法如下:1,打开webstorm安装路径下的bin/webstorm.sh文件2,在文件最前面添加如下代码:exportXMODIFIERS"@imfcitx"exportGTK
Wesley13 Wesley13
2年前
@Transactional 回滚不生效原因
事务的管理方式有两种,第一种是编程式事务管理,需要将数据库的自动提交等取消,并且需要自己编写事务代码,第二种则是声明式事务管理模式,spring利用springAOP特性编写了注解即题目中所提到的方式来管理事务,避免开发人员编写大量的事务代码。一、特性先来了解一下@Transactional注解的特性吧,可以更好排查问题1\.service类
Easter79 Easter79
2年前
Spring注解大全,汇总版
Spring使用的注解大全和解释注解解释@Controller组合注解(组合了@Component注解),应用在MVC层(控制层),DispatcherServlet会自动扫描注解了此注解的类,然后将web请求映射到注解了@RequestMapping的方法上。@Service组合注解(组合了@Component注解),应用在
Wesley13 Wesley13
2年前
@TableLogic表逻辑处理注解(逻辑删除)
在字段上加上这个注解再执行BaseMapper的删除方法时,删除方法会变成修改例:实体类:@TableLogicprivateIntegerdel;service层:调用BaseMapper的deleteById(id);执行是效果:加@TableLogic的情况下走Update
Stella981 Stella981
2年前
Spring Security 与 OAuth2(客户端)
client(客户端)(改篇文章尚未修改,仅供参考)OAuth2客户端的实现方式没有太多任何规定,可自行编写登录逻辑也可使用OAuth2提供的@EnableOAuth2Sso注解实现单点登录,该注解会添加身份验证过滤器替我们完成所有操作,只需在配置文件里添加授权服务器和资源服务器的配置即可添加配置
Easter79 Easter79
2年前
Spring注解详解
概述  注解的优势主配置简单只需要添加标签,JAVA的反射机制就可以根据标签自动注入自动初始化。@Autowired  配置方式:属性、setter方法或构造器都可以    不推荐使用,建议使用@Resource(http://my.oschina.net/u/929718),因为要使这个注解生效还需要在Spring配置文件中配
Stella981 Stella981
2年前
Spring 12 种 常用注解!
1.声明bean的注解@Component组件,没有明确的角色@Service在业务逻辑层使用(service层)@Repository在数据访问层使用(dao层)@Controller在展现层使用,控制器的声明(C)2.注入bean的注解@Autowired:由Spring提供@Inj
Wesley13 Wesley13
2年前
4、定时任务关闭超时未支付的订单
//1.在主启动类上加上支持定时任务的注解@EnableSchedulingpublicclassApplication{//2.编写定时任务@ComponentpublicclassOrderJob{@Autowiredp