一、什么是AOP
AOP(Aspect Orient Programming),也就是面向切面编程。可以这样理解,面向对象编程(OOP)是从静态角度考虑程序结构,面向切面编程(AOP)是从动态角度考虑程序运行过程。
二、AOP的作用
常常通过 AOP 来处理一些具有横切性质的系统性服务,如事物管理、安全检查、缓存、对象池管理等,AOP 已经成为一种非常常用的解决方案。
三、AOP实现原理
AOP 实际上是由目标类的代理类实现的。AOP 代理其实是由 AOP 框架动态生成的一个对象,该对象可作为目标对象使用。AOP 代理包含了目标对象的全部方法,但 AOP 代理中的方法与目标对象的方法存在差异,AOP 方法在特定切入点添加了增强处理,并回调了目标对象的方法。
四、Spring中对AOP的支持
Spring 中 AOP 代理由 Spring 的 IoC 容器负责生成、管理,其依赖关系也由 IoC 容器负责管理。因此,AOP 代理可以直接使用容器中的其他 Bean 实例作为目标,这种关系可由 IoC 容器的依赖注入提供。Spring 默认使用 Java 动态代理来创建 AOP 代理, 这样就可以为任何接口实例创建代理了。当需要代理的类不是代理接口的时候, Spring 自动会切换为使用 CGLIB 代理,也可强制使用 CGLIB。
AOP 编程其实是很简单的事情。纵观 AOP 编程, 其中需要程序员参与的只有三个部分:
五、Spring中AOP的实现
Spring 有如下两种选择来定义切入点和增强处理。
1. 基于Annotation的配置
(1)首先启用 Spring 对 @AspectJ 切面配置的支持。
<?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.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 打开spring的Annotation支持 --> <context:annotation-config/> <!-- 设定spring去哪些包中找Annotation --> <context:component-scan base-package="com.hh.spring"/> <!-- 如果希望实现Annotation的AOP需要先设置如下标签 --> <aop:aspectj-autoproxy/> </beans>
当启动了@AspectJ 支持后,只要在 Spring 容器中配置一个带@Aspect 注释的 Bean, Spring 将会自动识别该 Bean 并作为切面处理。
@Component("logAspect") //让这个切面类被Spring所管理 @Aspect //声明这个类是切面类 public class LogAspect {
/** * execution(* com.hh.spring.dao.*.add*(..)) * 第一个*表示任意返回值 * 第二个*表示com.hh.spring.dao这个包中的所有类 * 第三个*表示以add开头的所有方法 * (..)表示任意参数 * ||表示其它方法 * 这样,dao包中的所有类的add开头和delete开头的方法执行之前,都会先执行这个logStart方法 */ @Before("execution(* com.hh.spring.dao.*.add*(..)) ||" + "execution(* com.hh.spring.dao.*.delete*(..))") public void logStart(JoinPoint jp){ System.out.println(jp.getTarget());//得到执行的对象 System.out.println(jp.getSignature().getName());//得到执行的方法 Logger.info("方法执行前加入日志"); }
(4)定义After增强处理
//方法调用完成之后执行 @After("execution(* com.hh.spring.dao.*.add*(..)) ||" + "execution(* com.hh.spring.dao.*.delete*(..))") public void logEnd(){ Logger.info("方法执行后加入日志"); }
//函数调用中执行 @Around("execution(* com.hh.spring.dao.*.add*(..)) ||" + "execution(* com.hh.spring.dao.*.delete*(..))") public void logAround(ProceedingJoinPoint pjp) throws Throwable{ Logger.info("在Around中加入日志"); pjp.proceed();//执行程序 Logger.info("结束Around"); }
访问目标方法最简单的做法是定义增强处理方法时将第一个参数定义为 JoinPoint 类型,当该增强处理方法被调用时,该 JoinPoint 参数就代表了织入增强处理的连接点。JoinPoint 里包含了如下几个常用方法。
2.基于 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" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <!-- 打开spring的Annotation支持 --> <context:annotation-config/> <!-- 设定spring去哪些包中找Annotation --> <context:component-scan base-package="com.hh.spring"/> <!-- AOP用XML方式更简单,而IOC用Annotation更简单 --> <aop:config> <aop:aspect id="myLogAspect" ref="logAspect"> <!-- 在哪些位置加入 --> <aop:pointcut expression="execution(* com.hh.spring.dao.*.add*(..))" id="logPointCut"/> <aop:before method="logBefore" pointcut-ref="logPointCut"/> <aop:before method="logAfter" pointcut-ref="logPointCut"/> </aop:aspect> </aop:config> </beans>
参考文章:
http://blog.csdn.net/a906998248/article/details/7514969