AOP 切面编程

AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面向对象编程) 的补充.
AOP 的主要编程对象是切面(aspect), 而切面模块化横切关注点.
在应用 AOP 编程时, 仍然需要定义公共功能, 但可以明确的定义这个功能在哪里, 以什么方式应用, 并且不必修改受影响的类. 这样一来横切关注点就被模块化到特殊的对象(切面)里.
AOP 的好处:
每个事物逻辑位于一个位置, 代码不分散, 便于维护和升级
业务模块更简洁, 只包含核心业务代码.

AOP 术语

切面(Aspect): 横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象

通知(Advice): 切面必须要完成的工作

目标(Target): 被通知的对象

代理(Proxy): 向目标对象应用通知之后创建的对象

连接点(Joinpoint):程序执行的某个特定位置:如类某个方法调用前、调用后、方法抛出异常后等。连接点由两个信息确定:方法表示的程序执行点;相对点表示的方位。例如 ArithmethicCalculator#add() 方法执行前的连接点,执行点为 ArithmethicCalculator#add(); 方位为该方法执行前的位置

切点(pointcut):每个类都拥有多个连接点:例如 ArithmethicCalculator 的所有方法实际上都是连接点,即连接点是程序类中客观存在的事务。AOP 通过切点定位到特定的连接点。类比:连接点相当于数据库中的记录,切点相当于查询条件。切点和连接点不是一对一的关系,一个切点匹配多个连接点,切点通过 org.springframework.aop.Pointcut 接口进行描述,它使用类和方法作为连接点的查询条件。

代码实例

这里使用的是AspectJ的注解方式 ,整个工程依赖的包有:

spring-beans-4.1.1.RELEASE.jar
spring-context-4.1.1.RELEASE.jar
spring-core-4.1.1.RELEASE.jar
spring-expression-4.1.1.RELEASE.jar
commons-logging-1.2.jar
spring-aop-4.1.1.RELEASE.jar
aspectjrt.jar
aspectjweaver.jar
com.springsource.org.aopalliance-1.0.0.jar

接口

1
2
3
4
5
6
7
8
package spring.AOP.impl;

public interface Calculator {
public int add(int i ,int b);
public int sub(int i ,int b);
public int mul(int i ,int b);
public int div(int i ,int b);
}

实现类

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
package spring.AOP.impl;

import org.springframework.stereotype.Component;

@Component
public class CalculatorImpl implements Calculator {

@Override
public int add(int i, int b) {

return i+b;
}

@Override
public int sub(int i, int b) {

return i-b;
}

@Override
public int mul(int i, int b) {

return i*b;
}

@Override
public int div(int i, int b) {

return i/b;
}

}

切面类

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package spring.AOP.impl;

import java.util.Arrays;
import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

//把这个类声明成切面 :把该类放入IOC 容器中、再声明为一个切面
/**
*
* @Order:可以指定切面的优先级
*
*/
@Order(1)
@Aspect
@Component
public class LoggingAspect {
/**
* @Before:声明该方法是一个前置通知:在目标方法开始执行之前执行
* @param joinPoint:获取参数
*/
//第一个* 代表任何返回值 第二个*代表此类名下的所有方法 只要符合声明里的方法都能触发前置通知 ..代表任意参数
@Before("execution(public * spring.AOP.impl.Calculator.*(..))")
public void beforMethod(JoinPoint joinPoint) {
String method = joinPoint.getSignature().getName();
List<Object> list = Arrays.asList(joinPoint.getArgs());
System.out.println("开始于"+ method +"参数"+ list);
}

/**
* @After:声明该方法是一个后置置通知:在目标方法开始执行之后执行
* 不受出错控制,程序错误依然可以输出结果
*/
@After("execution(public * spring.AOP.impl.Calculator.*(..))")
public void afterMethod() {
System.out.println("结束");
}

/**
* @AfterReturning:获取方法返回值
* @param joinpoint:获取参数
* @param result:获取返回值
*/
@AfterReturning(value="execution(public * spring.AOP.impl.Calculator.*(..))",
returning = "result")
public void afterReturning(JoinPoint joinPoint,Object result) {
System.out.println("结果:"+ result);
}

/**
* @AfterThrowing:异常通知,方法发生错误时才会生效的通知
* @param joinpoint:获取参数
* @param ex:异常
*/
@AfterThrowing(value="execution(public * spring.AOP.impl.Calculator.*(..))",
throwing = "ex")
public void afterThrowing(JoinPoint joinPoint,Exception ex) {
String method = joinPoint.getSignature().getName();
System.out.println("方法"+method+"出错,错误:"+ ex);
}

/**
* 环绕通知需要携带 ProceedingJoinPoint 类型的参数.
* 环绕通知类似于动态代理的全过程: ProceedingJoinPoint 类型的参数可以决定是否执行目标方法.
* 且环绕通知必须有返回值, 返回值即为目标方法的返回值
* 一般不使用,使用环绕通知,前面的代码需要删除
*/
@Around("execution(public * spring.AOP.impl.Calculator.*(..))")
public Object aroundMethod(ProceedingJoinPoint pdj){

Object result = null;
String methodName = pdj.getSignature().getName();

try {
//前置通知
System.out.println("开始于 " + methodName + " 参数 " + Arrays.asList(pdj.getArgs()));
//执行目标方法
result = pdj.proceed();
//返回通知
System.out.println("结果 "+ result );
} catch (Throwable e) {
//异常通知
System.out.println("方法 " + methodName + "出错,错误:" + e);
throw new RuntimeException(e);
}
//后置通知
System.out.println("结束");

return result;
}
}

配置文件

1
2
3
4
5
6
7
8
9
10
11
12
13
<?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 http://www.springframework.org/schema/context/spring-context-4.1.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="spring.AOP.impl"></context:component-scan>
<!-- 使AspectJ 注解起作用:自动为匹配的类生成代理对象 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>

主函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package spring.AOP.impl;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {

public static void main(String[] args) {

ApplicationContext ctx = new ClassPathXmlApplicationContext("ApplicationContextAOP.xml");
Calculator calculator = (Calculator) ctx.getBean(Calculator.class); //.class 是因为 @Component 没有声明名字
calculator.add(2, 3);
calculator.sub(3, 4);
calculator.div(5, 0);
}

}

结果:

开始于add参数[2, 3]
结束
结果:5
开始于sub参数[3, 4]
结束
结果:-1
开始于div参数[5, 0]
结束
方法div出错:java.lang.ArithmeticException: / by zero

关于切面表达式

在编写 AspectJ 切面时, 可以直接在通知注解中书写切入点表达式. 但同一个切点表达式可能会在多个通知中重复出现.
在 AspectJ 切面中, 可以通过 @Pointcut 注解将一个切入点声明成简单的方法. 切入点的方法体通常是空的, 因为将切入点定义与应用程序逻辑混在一起是不合理的.
切入点方法的访问控制符同时也控制着这个切入点的可见性. 如果切入点要在多个切面中共用, 最好将它们集中在一个公共的类中. 在这种情况下, 它们必须被声明为 public. 在引入这个切入点时, 必须将类名也包括在内. 如果类没有与这个切面放在同一个包中, 还必须包含包名.
其他通知可以通过方法名称引入该切入点.

代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/**
* 定义一个方法, 用于声明切入点表达式. 一般地, 该方法中再不需要添入其他的代码.
* 使用 @Pointcut 来声明切入点表达式.
* 后面的其他通知直接使用方法名来引用当前的切入点表达式.
*/
@Pointcut("execution(public * spring.AOP.impl.Calculator.*(..))")
public void declareJointPointExpression(){}

/**
* @Before:声明该方法是一个前置通知:在目标方法开始执行之前执行
* @param joinPoint:获取参数
*/
//第一个* 代表任何返回值 第二个*代表此类名下的所有方法 只要符合声明里的方法都能触发前置通知 ..代表任意参数

@Before("declareJointPointExpression()")
public void beforMethod(JoinPoint joinPoint) {
String method = joinPoint.getSignature().getName();
List<Object> list = Arrays.asList(joinPoint.getArgs());
System.out.println("开始于"+ method +"参数"+ list);
}

关于AspectJ还有基于xml配置文件的方式,这里就不多赘述了,自行学习。

原创技术分享,您的支持将鼓励我继续创作
0%