AOP:面向切面编程,相对于OOP面向对象编程;
Spring的AOP是为了解耦。可以让一组类共享形同行为。
Spring支持AspectJ的注解式切面编程
使用@AspectJ生命一个切面
使用@After、@Before、@Around定义建言,可以直接将拦截规则(切点)作为参数
将其中@After、@Before、@Around参数的拦截规则为切点(pointCut),为了使切点复用,可以使用@PointCut专门定义拦截规则,然后在@After、@Before、@Around的参数中调用
其中符合条件的每一个被拦截处为连接点(JionPoint)
Spring aop支持AspectJ
<!--spring aop支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectJrt</artifactId>
<version>1.8.5</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.5</version>
</dependency>
开启Spring对于AspectJ的支持
使用@EnableAspectAutoProxy开启Srping对Aspect的支持
@Configuration
@ComponnetScan("***********")
@EnableAspectJAutoProxy
通过@Aspect注解声明一个切面
通过@Component让此切面成为Spring容器管理的Bean
通过@pointCut声明切点
@PonitCut("@annotation(*******.aop.Action")
通过@After声明一个建言,并使用@PointCut定义的切点
@After("annotationPointCut()")
通过反射可获得注解上的属性,然后做日志记录相关的操作