简介
AOP是Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
主要功能
日志记录,性能统计,安全控制,事务处理,异常处理等等。
主要意图
将日志记录,性能统计,安全控制,事务处理,异常处理等代码从业务逻辑代码中划分出来,通过对这些行为的分离,我们希望可以将它们独立到非指导业务逻辑的方法中,进而改变这些行为的时候不影响业务逻辑的代码。
概念实现
AspectJ是AOP的一个很悠久的实现,它能够和 Java 配合起来使用。
Aspect (切面): Aspect 声明类似于 Java 中的类声明,在 Aspect 中会包含着一些 Pointcut 以及相应的 Advice。
Joint point (连接点):表示在程序中明确定义的点,典型的包括方法调用,对类成员的访问以及异常处理程序块的执行等等,它自身还可以嵌套其它 joint point。
Pointcut (切点):表示一组 joint point,这些 joint point 或是通过逻辑关系组合起来,或是通过通配、正则表达式等方式集中起来,它定义了相应的 Advice 将要发生的地方。
Advice (通知):Advice 定义了在 pointcut 里面定义的程序点具体要做的操作,它通过 before、after 和 around 来区别是在每个 joint point 之前、之后还是代替执行的代码。
- 前置通知(Before):在目标方法被调用之前调用通知功能。
- 后置通知(After):在目标方法完成之后调用通知,无论该方法是否发生异常。
- 后置返回通知(After-returning):在目标方法成功执行之后调用通知。
- 后置异常通知(After-throwing):在目标方法抛出异常后调用通知。
- 环绕通知(Around):通知包裹了被通知的方法,在被通知的方法调用之前和调用之后执行自定义的行为。
Introduction (引入):添加方法或字段到被通知的类。 Spring允许引入新的接口到任何被通知的对象。Spring中要使用Introduction, 可通过DelegatingIntroductionInterceptor来实现通知,通过DefaultIntroductionAdvisor来配置Advice和代理类要实现的接口。
Target Object (目标对象):包含连接点的对象,被通知或被代理对象。
AOP Proxy (AOP代理):AOP框架创建的对象,包含通知。 在Spring中,AOP代理可以是JDK动态代理或者CGLIB代理。
Weaving (织入):织入描述的是把切面应用到目标对象来创建新的代理对象的过程。 Spring AOP 的切面是在运行时被织入,原理是使用了动态代理技术。Spring支持两种方式生成代理对象:JDK动态代理和CGLib,默认的策略是如果目标类是接口,则使用JDK动态代理技术,否则使用Cglib来生成代理。
实现Spring的API
实现org.springframework.aop包下的接口
MethodBeforeAdvice 方法前
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class LogApiBeforeUtil implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("before-api");
}
}
AfterReturningAdvice 方法返回后
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class LogApiAfterUtil implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("after-api");
}
}
xml文件配置
<?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"
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.xsd">
<bean id="logApiUtil" class="com.laq.util.LogApiBeforeUtil"></bean>
<!-- 实现Spring-API方式 -->
<aop:config>
<!-- 切入点 -->
<aop:pointcut id="log_pointcut" expression="execution(* com.laq.service.impl..*.update*(..))"/>
<!-- 通知 -->
<aop:advisor advice-ref="logApiUtil" pointcut-ref="log_pointcut"></aop:advisor>
</aop:config>
</beans>
自定义类
自定义切面类
public class LogXmlUtil {
public void before() {
System.out.println("before-xml");
}
public void after() {
System.out.println("after-xml");
}
public void around() {
System.out.println("around-xml");
}
}
配置xml
<?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"
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.xsd">
<!-- 自定义类 -->
<bean id="logUtil" class="com.laq.util.LogXmlUtil"></bean>
<aop:config>
<!-- 切面类 -->
<aop:aspect ref="logUtil">
<!-- 切入点 -->
<aop:pointcut id="log_piontcut" expression="execution(* com.laq.service.impl..*.add*(..))"/>
<!-- 通知到方法 -->
<aop:before method="before" pointcut-ref="log_piontcut"></aop:before>
</aop:aspect>
</aop:config>
</beans>
注解方式
自定义类
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LogAnnotationUtil {
@Before(value = "execution(* com.laq.service.impl..*.*(..))")
public void before() {
System.out.println("before-annotation");
}
@After(value = "execution(* com.laq.service.impl..*.*(..))")
public void after() {
System.out.println("after-annotation");
}
}
配置xml
<?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"
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.xsd">
<!-- 注册bean -->
<bean id="logAnnotationUtil" class="com.laq.util.LogAnnotationUtil"></bean>
<!-- 开启注解 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
</beans>