我是
靠谱客的博主
神勇大米,最近开发中收集的这篇文章主要介绍
使用AOP统一处理日志,觉得挺不错的,现在分享给大家,希望可以做个参考。
概述
AOP我想大家都很清楚,有时候我们需要处理一些请求日志,或者对某些方法进行一些监控,如果出现例外情况应该进行怎么样的处理,现在,我们从spring-boot中引入AOP.
[开发环境:jdk版本号为1.8,spring
boot的版本号为1.4.1]{style=”background-color:#FF0000”}
首先,我们先引入jar包,
POM文件添加如下内容:
?
1 2 3 4 5 6 7 8 9 10 11 | <!--引用AOP--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency> <!--引用GSON,用于打印--> <dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version> 2.7 </version> </dependency> |
引入jar包后,我们在boot的启动方法,增加两个简单的请求处理方法:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | @SpringBootApplication (scanBasePackages = { "com" }) @RestController public class DemoApplication{ public static void main(String[] args) { SpringApplication.run(DemoApplication. class , args); } //测试无参的get请求 @RequestMapping (value = "/testAspect" ,method = RequestMethod.GET) public UserVo test(){ UserVo userVo = new UserVo(); userVo.setAge( "23" ); userVo.setName( "贺小五" ); userVo.setSex( "男" ); return userVo; } //测试有参的get请求,让aop打印参数内容 @RequestMapping (value = "/testAspectArgs" ,method = RequestMethod.GET) public UserVo test(String name,String age,String sex){ UserVo userVo = new UserVo(); userVo.setName(name); userVo.setAge(age); userVo.setSex(sex); return userVo; } |
增加了两个简单的处理请求方法后,我们来增加我们的aop
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | /** * 项目名:SpringBootDemo * 创建人:贺小五 * 创建时间:16/12/4 下午7:05 * 类名:AspectDemo * 类描述: */ //申明是个切面 @Aspect //申明是个spring管理的bean @Component @Order ( 1 ) public class AspectDemo { private Logger log = Logger.getLogger(getClass()); private Gson gson = new Gson(); //申明一个切点 里面是 execution表达式 @Pointcut ( "execution(public * com.example.DemoApplication.*(..))" ) private void controllerAspect(){} //请求method前打印内容 @Before (value = "controllerAspect()" ) public void methodBefore(JoinPoint joinPoint){ ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = requestAttributes.getRequest(); //打印请求内容 log.info( "===============请求内容===============" ); log.info( "请求地址:" +request.getRequestURL().toString()); log.info( "请求方式:" +request.getMethod()); log.info( "请求类方法:" +joinPoint.getSignature()); log.info( "请求类方法参数:" + Arrays.toString(joinPoint.getArgs())); log.info( "===============请求内容===============" ); } //在方法执行完结后打印返回内容 @AfterReturning (returning = "o" ,pointcut = "controllerAspect()" ) public void methodAfterReturing(Object o ){ log.info( "--------------返回内容----------------" ); log.info( "Response内容:" +gson.toJson(o)); log.info( "--------------返回内容----------------" ); } |
两者配置好之后,接下来,我们请求,然后查看打印日志,首先,启动我们的容器,然后我们先请求有参的处理请求方法的,打印日志如下:
![](https://files.jb51.net/file_images/article/201612/20161213105100993.jpg?20161113105117)
{width=”1232”
height=”132”}
可以发现,请求的url,method,以及args参数的值,还有类型,以及返回的内容都打印出来了,说明,这是AOP拦截成功了.
接下来,我们测试下请求无参的处理请求方法,打印日志如下:
![](https://files.jb51.net/file_images/article/201612/20161213105126579.jpg?20161113105135)
{width=”1100”
height=”130”}
我们可以发现,这个方法,打印的方法参数是个空数组,是因为方法不需要参数传递.
以上就是springboot引用aop进行处理web的日志处理,下面将添加AOP切面的主要几个注解,下面只是对注解的描述跟使用,具体测试,作者就不进行测试了,各位看官有兴趣的话,可以自己测试一下:
类注解:
@Aspect将一个类定义为一个切面类
@order(i)标记切面类的处理优先级,i值越小,优先级别越高.PS:可以注解类,也能注解到方法上
方法注解:
@Pointcut定义一个方法为切点里面的内容为一个表达式,下面详细介绍
@Before 在切点前执行方法,内容为指定的切点
@After 在切点后,return前执行,
@AfterReturning在切入点,return后执行,如果想对某些方法的返回参数进行处理,可以在这操作
@Around 环绕切点,在进入切点前,跟切点后执行
@AfterThrowing 在切点后抛出异常进行处理
@order(i) 标记切点的优先级,i越小,优先级越高
@Pointcut注解组合使用:
上面代码中,我们定义了一个切点,该切点只进行处理指定路径的:
?
1 2 | @Pointcut ( "execution(public * com.example.DemoApplication.*(..))" ) private void controllerAspect(){} |
现在,我们在定义一个处理其他路径下的切点:
?
1 2 | @Pointcut ( "execution(public * com.demo.*.*(..))" ) private void controllerDemo(){} |
以上切点,都是分别处理不同的内容,如果我们需要一个切点来处理他们两者,我们可以这么配置:
?
1 2 | @Pointcut (value = "controllerAspect()||controllerDemo()" ) private void all(){} |
在@Pointcut注解内,直接引用其它被@Pointcut注解过的方法名称,这样,该切点就可以处理两个路径下的方法
@Pointcut注解中的execution表达式: public * com.demo.*.*(..)
第一个 public 表示方法的修饰符,可以用*代替
第一个 * 表示 返回值,*代表所有
com.demo.* 包路径,.*表示路径下的所有包
第三个.* 表示路径下,所有包下的所有类的方法
(..) 表示不限方法参数
关于@order(i)注解的一些注意事项:
注解类,i值是,值越小,优先级越高
注解方法,分两种情况
注解的是 @Before 是i值越小,优先级越高
注解的是 @After或者@AfterReturning 中,i值越大,优先级越高
总结两者的概括就是:
在切入点前的操作,按order的值由小到大执行
在切入点后的操作,按order的值由大到小执行
延伸:
有看官可能会问,如果我要打印请求从进来,到结束,所需要的时间,定义一个成员变量,用来统计时间,同时给@Before跟@AfterReturning访问,可能会有同步的问题,所以我们引用一个ThreadLocal指定泛型的对象,在@Before记录请求的时间,在@AfterReturning扣除记录的时间,就是消耗的时间,代码如下:
?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | /** * 项目名:SpringBootDemo * 创建人:贺小五 * 创建时间:16/12/4 下午7:05 * 类名:AspectDemo * 类描述: */ //申明是个切面 @Aspect //申明是个spring管理的bean @Component @Order ( 1 ) public class AspectDemo { private Logger log = Logger.getLogger(getClass()); private Gson gson = new Gson(); ThreadLocal<Long> startTime = new ThreadLocal<Long>(); //申明一个切点 里面是 execution表达式 @Pointcut ( "execution(public * com.example.DemoApplication.*(..))" ) private void controllerAspect() { } //请求method前打印内容 @Before (value = "controllerAspect()" ) public void methodBefore(JoinPoint joinPoint) { startTime.set(System.currentTimeMillis()); ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletRequest request = requestAttributes.getRequest(); //打印请求内容 log.info( "===============请求内容===============" ); log.info( "请求地址:" + request.getRequestURL().toString()); log.info( "请求方式:" + request.getMethod()); log.info( "请求类方法:" + joinPoint.getSignature()); log.info( "请求类方法参数:" + Arrays.toString(joinPoint.getArgs())); log.info( "===============请求内容===============" ); } //在方法执行完结后打印返回内容 @AfterReturning (returning = "o" , pointcut = "controllerAspect()" ) public void methodAfterReturing(Object o) { log.info( "--------------返回内容----------------" ); log.info( "Response内容:" + gson.toJson(o)); log.info( "--------------返回内容----------------" ); log.info( "请求处理时间为:" +(System.currentTimeMillis() - startTime.get())); } } |
以上,均为本人测试而得出的结果,可能会有出入,或者错误,欢迎指正.
最后
以上就是神勇大米为你收集整理的使用AOP统一处理日志的全部内容,希望文章能够帮你解决使用AOP统一处理日志所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复