一、AOP简介
-
AOP(Aspect Orient Programming),面向切面编程。面向切面编程是从动态角度考虑程 序运行过程。
-
AOP 底层,就是采用动态代理模式实现的。采用了两种代理:JDK 的动态代理,与 CGLIB 的动态代理。
AOP 为 Aspect Oriented Programming 的缩写,意为:面向切面编程,可通过运行期动态 代理实现程序功能的统一维护的一种技术。AOP 是 Spring 框架中的一个重要内容。利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程 序的可重用性,同时提高了开发的效率。
面向切面编程,就是将交叉业务逻辑封装成切面,利用 AOP 容器的功能将切面织入到 主业务逻辑中。所谓交叉业务逻辑是指,通用的、与主业务逻辑无关的代码,如安全检查、 事务、日志、缓存等。 若不使用 AOP,则会出现代码纠缠,即交叉业务逻辑与主业务逻辑混合在一起。这样, 会使主业务逻辑变的混杂不清。
面向切面编程的好处:
1.减少重复;
2.专注业务;
注意:面向切面编程只是面向对象编程的一种补充。
使用 AOP 减少重复代码,专注业务实现:
二、AOP编程术语
(1) 切面(Aspect)
切面泛指交叉业务逻辑。上例中的事务处理、日志处理就可以理解为切面。常用的切面 是通知(Advice)。实际就是对主业务逻辑的一种增强。 通常来说就是增加的功能
加上注解:@Aspect的类
(2) 连接点(JoinPoint)
连接点指可以被切面织入的具体方法。通常业务接口中的方法均为连接点。 即程序执行的某个特定位置:如类某个方法调用前、调用后、方法抛出异常后等。连接点由两个信息确定:方法表示的程序执行点;相对点表示的方位。
可以通过通知方法的参数JoinPoint来获取
(3) 切入点(Pointcut)
切入点指声明的一个或多个连接点的集合。通过切入点指定一组方法。 被标记为 final 的方法是不能作为连接点与切入点的。因为最终的是不能被修改的,不 能被增强的。
通过切点表达式所匹配到的方法
(4) 目标对象(Target)
目 标 对 象 指 将 要 被 增 强 的 对 象 。 即被通知的对象/目标对象
要使用这个切面通知的类。JoinPoint.getTarget()方法获取
(5) 通知(Advice)
切面必须要完成的工作
在切面中加入注解@Before;@After 等等的方法
三、AOP操作步骤
1、要在Spring应用中使用AspectJ注解,必须依赖AspectJ类库: aopalliance.jar 、aspectjweaver.jar和 spring-aspects.jar
<!-- https://mvnrepository.com/artifact/aopalliance/aopalliance -->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-aspects -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.2.9.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
2、spring的配置文件需要引用aop 的命名空间
xmlns:aop="http://www.springframework.org/schema/aop"
http://www.springframework.org/schema/aop
3、在Spring的配置文件中加入aop:aspectj-autoproxy
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:contex="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--在spring IOC容器中启动AspextJ注解支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:component-scan base-package="com.zsn.aop"></context:component-scan>
</beans>
4、编写相关代码
编写接口
public interface Operation {
double oper(double num1,double num2);
}
编写实现类
@Component
public class Add implements Operation {
@Override
public double oper(double num1, double num2) {
double result = num1+num2;
return result;
}
}
编写切面
//@Aspect注解,标明这个类是一个切面
@Component
@Aspect
public class OperationAspect {
//切点表达式
@Before("execution(* Add.oper(..))")
public void before(){
System.out.println("执行核心逻辑前需要执行的内容");
}
}
编写测试类
/**
* 测试类
*/
public class AspectTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-2.xml");
Operation add = context.getBean("add", Operation.class);
double oper = add.oper(1, 2);
System.out.println(oper);
}
}
测试结果
说明:可以看到,业务类代码不受使用了AoP之后的影响;测试程序也是同样的。要向业务类中附加功能,要做的仅仅只是去编写一个切面。
四、AspectJ切点表达式
AspectJ 定义了专门的表达式用于指定切入点。
表达式的原型是:
execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)
解释:
-
modifiers-pattern] 访问权限类型
-
ret-type-pattern 返回值类型
-
declaring-type-pattern 包名类名
-
name-pattern(param-pattern) 方法名(参数类型和参数个数)
-
throws-pattern 抛出异常类型
-
? 表示可选的部分
AspectJ 是一个优秀面向切面的框架,它扩展了 Java 语言,提供了强大的切面实现。
以上表达式共 4 个部分。
execution(访问权限 方法返回值 方法声明(参数) 异常类型)
1、最典型的切入点表达式时根据方法的签名来匹配各种方法
2、在 AspectJ中,切入点表达式可以通过操作符&&,||,! 结合起来
"execution(* Add.oper(..)) || execution(* Sub.oper(..))"
说明:第一个表达式和第二个表达式任意一个都可以进行匹配
五、JoinPoint 参数
可以在通知方法中声明一个类型为JoinPoint的参数.然后就能访问链接细节.如方法名称和参数值。
@Component
@Aspect
public class OperationAspect {
@Before("execution(* Add.oper(..))")
public void before(JoinPoint point){
//通过JoinPoint获取被通知的对象
System.out.println("被通知对象的名称:"+point.getTarget().getClass().toString());
System.out.println("指定方法的名称:"+point.getSignature().getName());
System.out.println("方法的参数:"+ Arrays.toString(point.getArgs()));
System.out.println("执行核心逻辑前需要执行的内容");
}
}
六、通知
AspectJ 中常用的通知有五种类型:
(1)@Before: 前置通知,在方法执行前执行
(2)@After:后置通知,在方法执行后执行
(3)@AfterThrowing:异常通知,在方法抛出异常之后执行
(4)@AfterReturning:返回通知,在方法返回结果之后执行
(5)@Around:环绕通知,围绕着方法执行
四种通知的执行顺序。以动态代理方式的位置来进行说明:
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
Object result;
try {
//Before
result = method.invoke(target, args);//@AfterReturning
return result;
} catch (InvocationTargetException e) {
Throwable targetException = e.getTargetException();
//@AfterThrowing
throw targetException;
}finally {
//@After
}
}
@Component
@Aspect
public class OperationAspect {
@Before("execution(* Add.oper(..))")
public void before(JoinPoint point){
System.out.println("执行核心逻辑前需要执行的内容");
}
@After("execution(* Add.oper(..))")
public void after(){
System.out.println("执行核心逻辑后需要执行的内容");
}
@AfterReturning("execution(* Add.oper(..))")
public void afterReturning(){
System.out.println("返回结果时执行的内容");
}
@AfterThrowing("execution(* Add.oper(..))")
public void afterThrowing(){
System.out.println("发生异常时候执行");
}
}
public class AspectTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-2.xml");
Operation add = context.getBean("add", Operation.class);
double oper = add.oper(1, 2);
System.out.println(oper);
}
}
补充
@AfterReturning
返回通知,在方法返回结果之后执行。无论连接点是正常返回还是抛出异常,后置通知都会执行.如果只想在连接点返回的时候记录日志,应使用返回通知代替后置通知
@AfterThrowing
异常通知,只在连接点抛出异常时才执行异常通知
将throwing属性添加到@AfterThrowing 注解中,也可以访问连接点抛出的异常.Throwable是所有错误和异常类的超类。所以在异常通知方法可以捕获到任何错误和异常。如果只对某种特殊的异常类型感兴趣,可以将参数声明为其他异常的参数类型。然后通知就只在抛出这个类型及其子类的异常时才被执行.
@Around
- 环绕通知是所有通知类型中功能最为强大的,能够全面地控制连接点.甚至可以控制是否执行连接点.
- 对于环绕通知来说,连接点的参数类型必须是 ProceedingJoinPoint . 它是 JoinPoint 的子接口,允许控制何时执行,是否执行连接点.
- 在环绕通知中需要明确调用ProceedingJoinPoint 的 proceed()方法来执行被代理的方法.如果忘记这样做就会导致通知被执行了,但目标方法没有被执行.
- 注意:环绕通知的方法需要返回目标方法执行之后的结果,即调用joinPoint.proceed();的返回值,否则会出现空指针异常
@Component
@Aspect
public class OperationAspectAround {
@Around("execution(* Add.oper(..))")
public Object around(ProceedingJoinPoint point){
System.out.println("before....");
Object obj=null;
try{
obj = point.proceed();
System.out.println("after return...");
}catch (Throwable e){
System.out.println("Throwable...");
}finally {
System.out.println("after....");
}
return obj;
}
}
public class AspectTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-2.xml");
Operation add = context.getBean("add", Operation.class);
double oper = add.oper(1, 2);
System.out.println(oper);
}
}
七、重入切入点定义及使用xml配置声明切面
重入切入点定义
在编写AspectJ切面时,可以直按仕迪王用t 9 .、达式可能会在多个通知中重复出现.
在AspectJ切面中,可以通过@Pointcut注解将一个切入点声明成简单的方法.切入点的方法体通常是空的。
//定义切点
@Pointcut("execution(**.oper( ..))")
public void pointcut(){
}
//直接调用切点方法
@Before( "pointcut()")
public void before(JoinPoint point){
}
使用xml配置声明切面
Spring也支持在 Bean 配置文件中声明切面.这种声明是通过 aop schema 中的XML元素完成的。
正常情况下,基于注解的声明要优先于基于XML的声明。
/**
* 使用xml配置的方式定义的切面,不需要任何的注解
*/
public class XMLAspect {
public void before(){
System.out.println("before....");
}
public void after(){
System.out.println("after....");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<!--在spring IOC容器中启动AspextJ注解支持-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
<context:component-scan base-package="com.zsn.aop"></context:component-scan>
<!--功能Bean配置-->
<bean id="add" class="com.zsn.aop.Add"></bean>
<bean id="aspect" class="com.zsn.aop.XMLAspect"></bean>
<!--配置切面-->
<aop:config>
<!--定义切面表达式-->
<aop:pointcut id="p1" expression="execution(* *.oper(..))"/>
<!--定义切面,每一切面就是一个aop:aspect节点-->
<aop:aspect id="textAspect" ref="aspect">
<!--定义通知:切点表达式可以直接写,也可以引用上边的切点表达式-->
<aop:after method="after" pointcut="execution(* *.oper(..))"></aop:after>
<aop:before method="before" pointcut-ref="p1"></aop:before>
</aop:aspect>
</aop:config>
</beans>
public class AspectTest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("spring-2.xml");
Operation add = context.getBean("add", Operation.class);
double oper = add.oper(1, 2);
System.out.println(oper);
}
}