Fork me on GitHub

在SpringBoot中配置aop

前言

aop作为spring的一个强大的功能经常被使用,aop的应用场景有很多,但是实际的应用还是需要根据实际的业务来进行实现。这里就以打印日志作为例子,在SpringBoot中配置aop
已经加入我的github模版中:https://github.com/LinkinStars/springBootTemplate

配置

经过那么长时间的过程,我们也慢慢体会到,在spingboot项目中添加元素是非常方便的,aop也是。

首先配置依赖
compile('org.springframework.boot:spring-boot-starter-aop')

然后就是添加aop了

 1 @Component
 2 @Aspect
 3 public class DemoAop {
 4 
 5     @Pointcut("execution(* com.linkinstars.springBootTemplate.controller.*.*(..))")
 6     private void logPointcut(){}
 7 
 8     @Before("logPointcut()")
 9     public void before(JoinPoint joinPoint){
10         LogUtil.printLog("AOP的before方法执行");
11     }
12 }

 

最后在我们访问任何controller的时候就会打印相应的日志信息

具体切点的配置,以及切入的方法等,都和原来是一样的这里就不在赘述

详细配置说明参数参考:https://www.cnblogs.com/lic309/p/4079194.html

posted @ 2018-02-26 17:06  LinkinStar  阅读(437)  评论(0编辑  收藏  举报