1 AOP简介
AOP(Aspect-Oriented Programming, 面向切面编程):是一种新的方法论,是对传统OOP(Object-Oriented Programming, 面向对象编程)的补充。
AOP的主要编程对象是切面(aspect),而切面模块化横切关注点。
在应用AOP编程时,仍然需要定义公共功能,但可以明确的定义这个功能在哪里,以什么方式应用,并且不需要修改受影响的类。这样一来横切关注点就模块化到特殊的对象(切面)里。
AOP的好处:①每个事物逻辑位于一个位置,代码不分散,便于维护和升级。②业务模块更简洁,只包含核心业务代码。
2 AOP术语
- 切面(Aspect):横切关注点(跨越应用程序多个模块的功能)被模块化的特殊对象。
- 通知(Advice):切面必须要完成的工作。
- 目标(Target):被通知的对象。
- 代理(Proxy):项目表对象应用通知之后创建的对象。
- 连接点(JoinPoint):程序执行的某个特定位置:如类某个方法调用前、调用后、方法抛出异常后等。连接点由两个信息确定:执行点(方法),方位(方法执行前的位置)。
- 切点(pointcut):每个类都拥有多个连接点,连接点是程序类中客观存在的事务。AOP通过切点定位到特定的连接点。(连接点相当于数据库中的记录,切点相当于查询条件)切点和连接点不是一一对应的关系,一个切点匹配多个连接点,切点通过
org.springframwork.aop.Pointcut
进行接口描述,它使用类和方法作为连接点的查询条件。
3 AspectJ
Java社区里最完整最流行的AOP框架。
要在Spring IOC容器中启用AspectJ,只要在Bean配置文件中定义一个空的XML元素<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
。当Spring IOC容器侦测到Bean配置文件中的<aop:aspectj-autoproxy>
元素时,会自动为AspectJ切面匹配的Bean创建代理。
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--配置自动扫描的包-->
<context:component-scan base-package="com.zc.spring.aop.impl"></context:component-scan>
<!--使AspectJ注解起作用:自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
前置通知
- 前置通知:在方法执行之前执行的通知。
- 前置通知使用@Before注解,并将切入点表达式的值作为注解值。
后置通知
- 后置通知是在连接点完成之后执行的,即连接点返回结果或者抛出异常的时候。
- 一个切面可以包括一个或者多个通知。
package com.zc.spring.aop.impl;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
import org.aspectj.lang.annotation.Aspect;
import java.util.Arrays;
import java.util.List;
//把这个类声明为一个切面:需要把该类放入到IOC容器中,再声明为一个切面
@Aspect
@Component
public class LoggingAspect {
//声明该方法是一个前置通知:在目标方法开始之前执行
@Before("execution(public int com.zc.spring.aop.impl.ArithmeticCalculator.add(int,int))")
public void beforeMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method "+methodName+" begins with "+args);
}
//后置通知:在目标方法执行后(无论是否发生异常),执行的通知
//在后置通知中还不能访问目标方法执行的结果。
@After("execution(* com.zc.spring.aop.impl.*.*(int,int))")
public void afterMethod(JoinPoint joinPoint){
String methodName = joinPoint.getSignature().getName();
List<Object> args = Arrays.asList(joinPoint.getArgs());
System.out.println("The method "+methodName+" ends with "+args);
}
}
测试类:
<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd">
<!--配置自动扫描的包-->
<context:component-scan base-package="com.zc.spring.aop.impl"></context:component-scan>
<!--使AspectJ注解起作用:自动为匹配的类生成代理对象-->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>
结果: