AOP(Aspect Oriented Programming),即面向切面的编程。
(一)什么是面向切面的编程
切面是指代码中一些公共的非核心模块的抽象,比如日志打印,可以将其从核心代码中剥离出来,当程序运行需要时通过切面插入公共代码即可,从而降低代码的耦合度,提高代码的可复用性,并使得我们编程的时候能够更专注于核心业务代码的编写,这种编程模式叫做面向切面的编程。
类可以继承,因此可以看做是有一个纵向的关系链,而切面可以横向在多个类的方法中插入,因此也说是横切面。Spring AOP中,切面出现的类通过IOC容器实例化并注入,从而将切面中的方法插入到核心业务代码中。
(二)面向切面的编程的一些基本概念
- 切面(aspect):代码中的一些公共模块的抽象,通常是一个类。
- 连接点(joinpoint):可能插入切面的点。
- 切入点(pointcut):(a predicate that matches join points)插入切面的连接点。
- 通知(advice):也就是拦截到连接点之后,要执行的代码,也即是要执行的切面类的方法。通知有以下几种类型:
before advice:连接点之前执行。
after return advice:连接点正常返回后执行
after throwing advice:连接点抛出异常后执行
after(final) advice:连接点正常返回或者抛出异常后都会被执行的
around advice: 连接点前后都执行
这里我们可以总结一下连接点(joinpoint)、切入点(pointcut)、通知(advice)之间的关系:通过切入点的描述找到连接点,在连接点插入相应类型的advice。
- 目标对象:目标对象是要被拦截的对象,或者要被插入代码的方法。
- AOP代理对象:是目标对象和插入的切面对象融合之后的对象。一个 AOP 代理对象是一个 JDK 动态代理对象(基于接口)或 CGLIB 代理对象(基于类)。
- 织入:切面和目标对象连接创建代理对象的过程。
(三)测试
接下来以一个实例说明,eclipse新建一个工程,工程视图如下(截图一部分),需要spring等相关jar包:
(1)核心业务接口和实现类
CoreService.java接口:
package com.spring.service;
public interface CoreService{
public void coreMethod();
public void coreBusiness();
}
(2)实现类
package com.spring.service;
public class ImpCoreService implements CoreService {
public void coreMethod(){
System.out.println("This is coreMethod");
}
public void coreBusiness(){
System.out.println("This is coreBusiness");
}
}
(3)切面类
package com.spring.aop;
public class Logger {
public void aopLogBefore(){
System.out.println("before");
}
public void aopLogAfter(){
System.out.println("after");
}
}
(4)测试主类:
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.spring.service.CoreService;
public class MainApp {
public static void main(String[] args) {
//创建Spring ApplicationContext 容器,利用配置文件Beans.xml
ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");
//容器注入ImpCoreService对象
CoreService core = (CoreService)context.getBean("ImpCoreService");
core.coreBusiness();
System.out.println();
core.coreMethod();
}
}
(5)Beans.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-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- bean 是一个被实例化,组装,并通过 Spring IoC 容器所管理的对象 -->
<!-- id用于获取对象使用 -->
<bean id="ImpCoreService" class="com.spring.service.ImpCoreService">
</bean>
<bean id="Logger" class="com.spring.aop.Logger">
</bean>
<aop:config>
<aop:aspect id="LoggerAspect" ref="Logger">
<!--aop:pointcut 描述了连接点,通过这个描述可知,这里连接点是CoreService接口中的所有方法,也可以改变描述只连接一个方法-->
<aop:pointcut id="TempPointcut" expression="execution(* com.spring.service.CoreService.*(..))" />
<!--一个前置和后置的advice实例-->
<aop:before method="aopLogBefore" pointcut-ref="TempPointcut" />
<aop:after method="aopLogAfter" pointcut-ref="TempPointcut" />
</aop:aspect>
</aop:config>
</beans>
这里通过XML配置文件来实现AOP(也可以通过注解),执行结果如下,可以看到切面对象插入到了目标对象中。或者说advice代码插入到了业务代码中。