Mark3 项目记录---0x04 AOP

2017-06-10  本文已影响0人  量产型炮灰工程师

在写日志模块之前,突然想起前面还有这个aop。现在这节要做的,是基于切面来打日志,捕捉/处理异常。

准备

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

在引入依赖后,其实已经是什么都不用干了,因为引入依赖后,springBoot为什么自动开启了spring.aop.auto=true选项,它相当于已经启用了@EnableAspectJAutoProxy这个注解。
在application.properties中,点进spring.aop.auto=true选项

application.properties 点进spring.aop.auto选项

我们还发现,这个spring.aop.proxy-target-class选项,默认为false,看它的描述的意思是,默认为false,是采用标准Java实现,如果是true,就采用cglib,顾application.properties中,配置如下:

#Aop Config
#spring.aop.auto这个可以不写,因为引入依赖后,默认是开启的
spring.aop.auto=true
spring.aop.proxy-target-class=true

剩下的就是Spring-AOP的事情了,别无二样

使用环绕切面对任意实现了T*Mapper接口的所有方法进行拦截

关于aop的切入语法,可以看【第六章】 AOP 之 6.5 AspectJ切入点语法详解 ——跟我学spring3
关于aop的注解,虽然SpringBoot略有不同,也可以看【第六章】 AOP 之 6.4 基于@AspectJ的AOP ——跟我学spring3

aop的核心其实就是代理模式 如何让孩子爱上设计模式 ——13.代理模式(Proxy Pattern)

项目地址
代码下载

上一篇 下一篇

猜你喜欢

热点阅读