在编程中经常用到切面编程,涉及到动态代理、切面、切点、切入点表达式问题
1、依赖jar包
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>${cglib.version}</version>
</dependency><!--动态代理使用-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency><!-- -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring.version}</version>
</dependency>
动态代理:jdk默认的动态代理是基于接口的(需要实现InvocationHandler),cglib动态代理 使用继承来实现 不能用final修饰
2、切面、切入点表达式、
切入点表达式:如:execution(* com.xxx.service.*.*(..)) 指的是:包名为com.xxx.service下的所有类的所有方法 以及任意返回值
切面:在程序检执行到切入点表达式的代码之前before、之后After、前后Around执行的程序
advisor就是一个特殊的切面
3、多个切面引用同一个切入点时,这时需要排优先级
4、在spring中开启 cglib
Enables the use of the @AspectJ style of Spring AOP 这个配置也是开启AOP注解功能的配置
<aop:aspectj-autoproxy proxy-target-class="true"/>