AOP
AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。OOP引入封装、继承、多态等概念来建立一种对象层次结构,用于模拟公共行为的一个集合。不过OOP允许开发者定义纵向的关系,但并不适合定义横向的关系,例如日志功能。日志代码往往横向地散布在所有对象层次中,而与它对应的对象的核心功能毫无关系对于其他类型的代码,如安全性、异常处理和透明的持续性也都是如此,这种散布在各处的无关的代码被称为横切(cross cutting),在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。
AOP技术恰恰相反,它利用一种称为”横切”的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为”Aspect”,即切面。所谓”切面”,简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。
使用”横切”技术,AOP把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点。横切关注点的一个特点是,他们经常发生在核心关注点的多处,而各处基本相似,比如权限认证、日志、事物。AOP的作用在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。
AOP核心概念
1、横切关注点
对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点
2、切面(aspect)
类是对物体特征的抽象,切面就是对横切关注点的抽象
3、连接点(joinpoint)
被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器
4、切入点(pointcut)
对连接点进行拦截的定义
5、通知(advice)
所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类
6、目标对象
代理的目标对象
7、织入(weave)
将切面应用到目标对象并导致代理对象创建的过程
8、引入(introduction)
在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段
AOP使用示例DEMO
1、 创建maven工程spring-aop,添加依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>1.8.9</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.7.4</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.2</version>
</dependency>
2、 创建需要进行AOP增强的bean对象,我们需要增强这个bean对象中的某个方法,但是不能直接修改源码,随意的改动源码会造成一定的风险,spring已经帮我完成了这一点,即AOP
/**
* Create by zhaihongwei on 2018/3/8
*
* 通过spring AOP来动态增强这个类
*/
public class TestBean {
public void test() {
System.out.println("test");
}
public void sayHello() {
System.out.println("hello");
}
}
3、 通过@Aspect注解完成对test方法的增强。
/**
* Create by zhaihongwei on 2018/3/8
*/
@Aspect// 定义切面
public class AspectJTest {
// 切点,增强test方法
@Pointcut("execution(* *.test(..))")
public void test() {
}
@Before("test()")
public void beforeTest() {
System.out.println("beforeTest");
}
@After("test()")
public void afterTest() {
System.out.println("afterTest");
}
@Around("test()")
public Object aroundTest(ProceedingJoinPoint pjp) {
System.out.println("beforeAround");
Object o = null;
try {
o = pjp.proceed();
} catch (Throwable throwable) {
throwable.printStackTrace();
}
System.out.println("afterAround");
return o;
}
}
4、 创建配置文件
<?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-3.1.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<!--
开启aop功能,需要添加aop所需要的命名空间及对应的url
proxy-target-class="false" 默认为false,使用Jdk代理,置为true时,强制使用CGLIB代理
默认情况下spring会使用jdk动态代理来创建AOP,对接口实例创建代理,
当需要代理的对象不是接口,而是类时,spring会切换为CGLIB代理,也可以使用上面的配置强制使用CGLIB代理
-->
<aop:aspectj-autoproxy proxy-target-class="false"/>
<!-- 注入bean -->
<bean id="test" class="com.zhw.study.TestBean" />
<!-- 注入切面 -->
<bean class="com.zhw.study.AspectJTest" />
</beans>
5、 测试
/**
* Hello world!
*
*/
public class App
{
public static void main( String[] args )
{
System.out.println( "Hello World!" );
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
TestBean bean = (TestBean) applicationContext.getBean("test");
bean.test();
bean.sayHello();
}
}
控制台上会打印:
beforeTest
beforeAround
test
afterTest
afterAround
hello
通过上面的实例,我们使用spring的AOP完成了对test方法的动态增强,可以看到sayHello()方法并没有被增强