AOP概念:
AOP的全称是Aspect Orient Programming,即面向切面编程。是对OOP(Object Orient Programming)的一种补充,用于处理一些具有横切性质的服务。常常用于日志输出、安全控制等。
AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善。OOP引入封装、继承、多态等概念来建立一种对象层次结构,用于模拟公共行为的一个集合。不过OOP允许开发者定义纵向的关系,但并不适合定义横向的关系,例如日志功能。日志代码往往横向地散布在所有对象层次中,而与它对应的对象的核心功能毫无关系对于其他类型的代码,如安全性、异常处理和透明的持续性也都是如此,这种散布在各处的无关的代码被称为横切(cross cutting),在OOP设计中,它导致了大量代码的重复,而不利于各个模块的重用。
AOP技术恰恰相反,它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的公共行为封装到一个可重用模块,并将其命名为"Aspect",即切面。所谓"切面",简单说就是那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块之间的耦合度,并有利于未来的可操作性和可维护性。
Spring架构对Aop的支持:
Spring中AOP代理由Spring的IOC容器负责生成、管理,其依赖关系也由IOC容器负责管理。因此,AOP代理可以直接使用容器中的其它bean实例作为目标,这种关系可由IOC容器的依赖注入提供。Spring创建代理的规则为:
(1) 默认使用Java动态代理来创建AOP代理,这样就可以为任何接口实例创建代理了.
(2) 当需要代理的类不是代理接口的时候,Spring会切换为使用CGLIB代理,也可强制使用CGLIB.
纵观AOP其实总结性的来讲的话就是,把系统之中的公共服务进行抽取完后进行横切,在不增加代码的前提下增加系统的功能。在实际的开发运用过程之中通常只需要做三步就行:
1、定义普通业务组件
2、定义切入点,一个切入点可能横切多个业务组件
3、定义增强处理,增强处理就是在AOP框架为普通业务组件织入的处理动作
即 代理对象的方法 = 增强处理 + 被代理对象的方法;
简单代码段实例分析:(在实际使用AOP的过程中除了必备的SPring开发工具包之外还需要进行导入的还有aopalliance.jar 以及 aspectjweaver.jar);
使用AOP XML方式进行的实现:
1)首先进行定义一个接口:
public interface ITest {
public void sayHi();
public void doSomeThing();
}
2)来两个简单的接口的实现类:
public class TestImpl1 implements ITest{
@Override
public void printHelloWorld() {
System.out.println("Enter Test1.printHelloWorld()");
}
@Override
public void doPrint() {
System.out.println("Enter Test1.doPrint()");
}
}
public class TestImpl2 implements ITest{
@Override
public void printHelloWorld() {
System.out.println("Enter Test2.printHelloWorld()");
}
@Override
public void doPrint() {
System.out.println("Enter Test2.doPrint()");
}
}
3)、 进行声明横切关注点,打印系统的当前时间:
public class TimeHandler {
public void printTime()
{
System.out.println("The system's CurrentTime = " + System.currentTimeMillis());
}
}
4)、进行aop的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-4.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<bean id="helloWorldImpl1" class="com.example.aopdemo.TestImpl1" />
<bean id="helloWorldImpl2" class="com.example.aopdemo.TestImpl2" />
<bean id="timeHandler" class="com.example.aopdemo.TimeHandler" />
<bean id="logHandler" class="com.example.aopdemo.LogHandler" />
<aop:config>
<aop:aspect id="time" ref="timeHandler">
<aop:pointcut id="addAllMethod" expression="execution(* com.example.aopdemo.ITest.*(..))" />
<aop:before method="printTime" pointcut-ref="addAllMethod" />
<aop:after method="printTime" pointcut-ref="addAllMethod" />
</aop:aspect>
</aop:config>
</beans>
5)、Test for AOP:
public class TestAop{
public static void main(String[] args){
ApplicationContext ctx = new ClassPathXmlApplicationContext("aop.xml");
ITest hw1 = (ITest)ctx.getBean("TestImpl1");
ITest hw2 = (ITest)ctx.getBean("TestImpl2");
hw1.printHelloWorld();
System.out.println();
hw1.doPrint();
System.out.println();
hw2.printHelloWorld();
System.out.println();
hw2.doPrint();
}
}
执行结果如下:
CurrentTime = 1446129611993
Enter Test1.printHelloWorld()
CurrentTime = 1446129611993
CurrentTime = 1446129611994
Enter Test1.doPrint()
CurrentTime = 1446129611994
CurrentTime = 1446129611994
Enter Test2.printHelloWorld()
CurrentTime = 1446129611994
CurrentTime = 1446129611994
Enter Test2.doPrint()
CurrentTime = 1446129611994
注意点:如果定义了多个关注点。如何进行规划他们的调用顺序呢。解决方法有两种:
(1)aspect里面有一个order属性,order属性的数字就是横切关注点的顺序
(2)Spring默认以aspect的定义顺序作为织入顺序