一、概念
AOP(aspect Oriented Programming),面向切面编程,是OOP(Object Oriented Programming,面向对象编程)的补充和完善。OOP引入封装、多态、继承等概念来建立一种对象层次结构,对于纵向的业务关系开发比较优秀,但并不适合横向的关系,例如,日志、事务等于业务无关的这些横向功能,这些功能散落在所有对象层次中,而OOP不能很好的抽象出来,导致了大量代码的重复,不利于各个模块的复用。
但是AOP很好的解决了这些横向代码的问题,他是一种“横切”的技术,解剖开封装的对象的内部,并且那些公共行为,与业务无关的行为,却为业务模块所共同调用的逻辑封装到了一个可重用模块,称为“Aspect”,即切面。便于减少系统的重复代码,降低模块之间的耦合度,并有利未来的可操作性和可维护性。
“核心关注点”:这是业务处理的主要流程。
“横切关注点”:是与业务关系不大的部分,但是经常发生在核心关注点的多处,基本相似,比如,权限,日志,事务。
AOP就在于分离系统中的各种关注点,将核心关注点和横切关注点分离开来。
二、AOP的核心概念
Cross Cutting Concern:横切关注点。是一种独立的服务,它会遍布在系统的处理流程中。
Aspect:切面。对横切关注点的模块化。
Advice:通知。对横切关注点的具体实现
Pointcut:切入点。定义了Advice应用到哪些JoinPint上,对Spring来说是方法调用。
Weave:织入。将Advice应用到Target Object上的过程叫织入。Spring支持的是动态织入。
Target Object:目标对象。Advice被应用的对象。
Proxy: Spring AOP 默认使用的是JDK的动态代理,他的代理是运行时创建的也可以使用CGLIB代理。
三、Spring对AOP的支持(采用注解annotation方式)
步骤:
1、配置spring依赖库
* SPRING_HOME/dist/spring.jar
* SPRING_HOME/lib/jakarta-commons/commons-logging.jar
* SPRING_HOME/lib/log4j/log4j-1.2.14.jar
* SPRING_HOME/lib/aspectj/*.jar
2、将横切性关注点模块化
package com.bjpowernode.spring;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class SecurityHandler {
/**
* 定义Pointcut,Pointcut的名称为addAddMethod(),此方法没有返回值和参数
* 该方法就是一个标识,不进行调用
*/
@Pointcut("execution(* add*(..))")
private void addAddMethod(){};
/**
* 定义Advice,表示我们的Advice应用到哪些Pointcut订阅的Joinpoint上
*/
@Before("addAddMethod()")
//@After("addAddMethod()")
private void checkSecurity() {
System.out.println("-------checkSecurity-------");
}
}
3、启用aspect对Annotation的支持,并且将目标类和Aspect类配置到IOC容器中
<!-- 启用AspectJ对Annotation的支持 -->
<aop:aspectj-autoproxy/>
<bean id="userManager" class="com.bjpowernode.spring.UserManagerImpl"/>
<bean id="securityHandler" class="com.bjpowernode.spring.SecurityHandler"/>
四、采用静态配置文件方式完成AOP
<?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:tx="http://www.springframework.org/schema/tx"
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.xrq.aop.HelloWorldImpl1" />
<bean id="helloWorldImpl2" class="com.xrq.aop.HelloWorldImpl2" />
<bean id="timeHandler" class="com.xrq.aop.TimeHandler" />
<aop:config>
<aop:aspect id="time" ref="timeHandler">
<aop:pointcut id="addAllMethod" expression="execution(* com.xrq.aop.HelloWorld.*(..))" />
<aop:before method="printTime" pointcut-ref="addAllMethod" />
<aop:after method="printTime" pointcut-ref="addAllMethod" />
</aop:aspect>
</aop:config>
</beans>
如果用多个切点:用order属性标识
<aop:config>
<aop:aspect id="time" ref="timeHandler" order="1">
<aop:pointcut id="addTime" expression="execution(* com.xrq.aop.HelloWorld.*(..))" />
<aop:before method="printTime" pointcut-ref="addTime" />
<aop:after method="printTime" pointcut-ref="addTime" />
</aop:aspect>
<aop:aspect id="log" ref="logHandler" order="2">
<aop:pointcut id="printLog" expression="execution(* com.xrq.aop.HelloWorld.*(..))" />
<aop:before method="LogBefore" pointcut-ref="printLog" />
<aop:after method="LogAfter" pointcut-ref="printLog" />
</aop:aspect>
</aop:config>
推荐给大家一个博客:http://www.cnblogs.com/hongwz/p/5764917.html
另外,一些常见的切入点表达式:
http://blog.csdn.net/remote_roamer/article/details/52765336