AOP(Aspect-Oriented Programming):面向切面编程,是对传统OOP(面向对象编程)的补充。
AOP主要编程对象是切面(aspect),而切面模块化横切关注点
在应用AOP编程时,需要定义公共功能,但可以明确定义这个功能在哪里,以什么方式应用,并且不必须改受影响的类,这样横切关注点就被模块化到特殊的对象(切面)里。
AOP的好处:
每个事物逻辑位于一个位置,代码不分散,便于维护和升级
业务模块更简洁,只包含核心业务代码
AOP术语:
切面(Aspect):横切关注点(跨越应用程序多个模块的功能)被模块化到特殊的对象
通知(Advice):切面必须完成的工作
目标(Target):被通知的对象
代理(Proxy):向目标对象应用通知之后创建的对象
连接点(JoinPoint):程序执行的某个特定位置,如类某个方法调用前、调用后、方法抛出异常后等。连接点由两个信息确定:方法表示的程序执行点相对点表示的方位。
切点(pointcut):每个类都有多个连接点,即连接点是程序中客观存在的事务。AOP通过切点定位到特殊的连接点。切点和连接点不是一对一的关系,一个切点匹配多个连接点,切点通过org.springframwork.aop.Pointcut接口进行描述,使用类和方法作为连接点的查询条件。
AspctJ: AOP框架,可以使用基于AspectJ注解或基于XML配置AOP
AspectJ支持5种类型的通知:
@Before 前置通知
@After 后置通知,无论该方法是否出现异常,访问不到方法的返回值
@AfterReturning 返回通知,在方法返回结果后执行,可以访问到方法的返回值
@AfterThrowing 异常通知,在方法抛出异常后执行
@Around 环绕通知
利用方法签名编写AspectJ切入点表达式
execution * spring.aop.CalculatorIml.*(…)
第一个代表任意修饰符和返回值,第二个代表任意方法,”…”匹配任意数量参数
要声明Aspectj切面,需要在IOC容器中将切面声明为bean实例
切面类:
@Aspect
@Component
public class LogginAspect {
//声明该方法是前置通知,在目标方法执行之前执行
@Before("execution(public int spring.aop.CalculatorIml.add(int, int))")
public void before() {
System.out.println("before");
}
}
还需要配置:
<!-- 使Aspectj注解起作用,自动为匹配类生产代理 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
返回通知:
@AfterReturning(value="execution(public int spring.aop.CalculatorIml.add(int, int))",
returning="result")
public void Returning(int result) {
System.out.println("returning :" + result);
}
异常通知:
@AfterThrowing(value="execution(public double spring.aop.CalculatorIml.chu(int, int))",
throwing="ex")
public void Throwing(Exception ex) {
System.out.println("ex");
System.out.println("throwing :" +ex );
}
//目标类try/catch异常后异常通知接收不到
切面的重用切点表达式
使用Pointcut来声明切入点表达式
定义一个方法,用于声明切入点表达式,一般的,该方法中不再需要添加其他代码:
@Pointcut("execution(public int spring.aop.CalculatorIml.add(int, int))")
public void JoinPointExpression() {}
//声明该方法是前置通知,在目标方法执行之前执行
@Before("JoinPointExpression()")
public void before() {
System.out.println("before");
}
XML配置文件方式配置AOP:
<bean id="calculator" class="spring.aop.CalculatorIml"></bean>
<bean id="loggion" class="spring.aop.LogginAspect"></bean>
<!-- 配置AOP -->
<aop:config>
<!-- 配置切点表达式 -->
<aop:pointcut expression="execution(public int spring.aop.CalculatorIml.add(int, int))" id="beforePoint"/>
<!-- 配置切面及通知 -->
<aop:aspect ref="loggion" order="1">
<!-- 修改aop:before 元素标签实现不同的通知 -->
<aop:before method="before" pointcut-ref="beforePoint"/>
</aop:aspect>
</aop:config>