Spring中的AOP 实现
一、AOP 概念
Joinpoint:它定义在哪里加入你的逻辑功能,对于Spring AOP,Jointpoint指的就是Method。
Advice:特定的Jointpoint处运行的代码,对于Spring AOP 来讲,有Before advice、AfterreturningAdvice、ThrowAdvice、AroundAdvice(MethodInteceptor)等。
Pointcut:一组Joinpoint,就是说一个Advice可能在多个地方织入,
Aspect:这个我一直迷惑,它实际是Advice和Pointcut的组合,但是Spring AOP 中的Advisor也是这样一个东西,但是Spring中为什么叫Advisor而不叫做Aspect。
Weaving:将Aspect加入到程序代码的过程,对于Spring AOP,由ProxyFactory或者ProxyFactoryBean负责织入动作。
Target:这个很容易理解,就是需要Aspect功能的对象。
Introduction:引入,就是向对象中加入新的属性或方法,一般是一个实例一个引用对象。当然如果不引入属性或者引入的属性做了线程安全性处理或者只读属性,则一个Class一个引用也是可以的(自己理解)。Per-class lifecycle or per-instance life cycle
二、AOP 种类
1、静态织入:指在编译时期就织入Aspect代码,AspectJ好像是这样做的。
2、动态织入:在运行时期织入,Spring AOP属于动态织入,动态织入又分静动两种,静则指织入过程只在第一次调用时执行;动则指根据代码动态运行的中间状态来决定如何操作,每次调用Target的时候都执行(性能较差)。
三、Spring AOP 代理原理
Spring AOP 是使用代理来完成的,Spring 会使用下面两种方式的其中一种来创建代理:
1、JDK动态代理,特点只能代理接口,性能相对较差,需要设定一组代理接口。
2、CGLIB 代理,可代理接口和类(final method除外),性能较高(生成字节码)。
四、Spring AOP 通知类型
1、BeforeAdvice:前置通知需实现MethodBeforeAdvice,但是该接口的Parent是BeforeAdvice,致于什么用处我想可能是扩展性需求的设计吧。或者Spring未来也并不局限于Method的JoinPoint(胡乱猜测)。BeforeAdvice可以修改目标的参数,也可以通过抛出异常来阻止目标运行。
2、AfterreturningAdvice:实现AfterreturningAdvice,我们无法修改方法的返回值,但是可以通过抛出异常阻止方法运行。
3、AroundAdvice:Spring 通过实现MethodInterceptor(aopalliance)来实现包围通知,最大特点是可以修改返回值,当然它在方法前后都加入了自己的逻辑代码,因此功能异常强大。通过MethodInvocation.proceed()来调用目标方法(甚至可以不调用)。
4、ThrowsAdvice:通过实现若干afterThrowing()来实现。
5、IntroductionInterceptor:Spring 的默认实现为DelegatingIntroductionInterceptor
五、Spring AOP Pointcut
以上只是Advice,如果不指定切入点,Spring 则使用所有可能的Jointpoint进行织入(当然如果你在Advice中进行方法检查除外)。因此切入点在AOP中扮演一个十分重要的角色。Spring 2.0 推荐使用AspectJ的Annocation的切入点表达式来定义切入点,或者使用<aop:xxx/>来定义AOP,这方面本篇不做考虑。
1、Pointcut:它是Spring AOP Pointcut的核心,定义了getClassFilter()和getMethodMatcher()两个方法。
2、ClassFilter:定义了matches(Class cls)一个方法。
3、MethodMatcher() 定义了matches(Method,Class),isRuntime(),matches(Mathod,Class,Object[])三个方法,如果isRuntime()返回true则表示为动态代理(实际是动态代理的动态代理),则调用第三个方法(每访问一次调用一次),否则调用第一个方法(并且只调用一次)
4、Spring AOP 静态切入点的几个实现。
ComposablePointcut 太复杂一个切入点无法表达就用这个,union MethodMatcher和ClassFilter或者intersection MethodMatcher、ClassFilter和Pointcut。为什么不实现union Pointcut? 而只能通过Pointcuts类对Pointcut进行union操作。
ControlFlowPointcut 想对程序的运行过程进行追踪就用这个
DynamicMatchMatcherPointcut 想用动态AOP 就用这个
JdkRegexpMethodPointcut 想使用正则表达式就用这个
Perl5RegexpMethodPointcut
NameMatchMethodPointcut 想用方法名字来匹配就用这个
StaticMethodMatcherPointcut 静态切入点就用这个
没有人反对你直接实现Pointcut:)。
六、Spring AOP 中的Advisor其实就是Aspect
1、 PointcutAdvisor
其实一般使用DefaultPointcutAdvisor就足够了,给它Advice和Pointcut。
当然如果想少写那么几行代码也可以使用NameMatchMethodPointcutAdvisor,RegexpMethodPointcutAdvisor等。
更多Advisor可以查看API文档。
2、 IntroductionAdvisor
默认实现为DefaultIntroductionAdvisor。
七、AOP ProxyFactory
使用代码实现AOP 可使用ProxyFactory
声明式AOP 可使用ProxyFactoryBean
ProxyFactoryBean 需要设定 target,interceptorNames(可以是Advice或者Advisor,注意顺序)
对接口代理需设置proxyInterfaces
八、自动代理
BeanNameAutoProxyCreator
- <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames"><value>jdk*,onlyJdk</value></property>
- <property name="interceptorNames">
- <list>
- <value>myInterceptor</value>
- </list>
- </property>
- </bean>
<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"><value>jdk*,onlyJdk</value></property> <property name="interceptorNames"> <list> <value>myInterceptor</value> </list> </property> </bean>
DefaultAdvisorAutoProxyCreator
- <bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
- <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor">
- <property name="transactionInterceptor" ref="transactionInterceptor"/>
- </bean>
- <bean id="customAdvisor" class="com.mycompany.MyAdvisor"/>
- <bean id="businessObject1" class="com.mycompany.BusinessObject1">
- <!-- Properties omitted -->
- </bean>
- <bean id="businessObject2" class="com.mycompany.BusinessObject2"/>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/> <bean class="org.springframework.transaction.interceptor.TransactionAttributeSourceAdvisor"> <property name="transactionInterceptor" ref="transactionInterceptor"/> </bean> <bean id="customAdvisor" class="com.mycompany.MyAdvisor"/> <bean id="businessObject1" class="com.mycompany.BusinessObject1"> <!-- Properties omitted --> </bean> <bean id="businessObject2" class="com.mycompany.BusinessObject2"/>
九、Spring 2.0 AOP
Spring 2.0 的APO, 它引入了一种更加简单并且更强大的方式来定义切面。
首先建一个普通Java项目:com.longthsoft.learn.spring
把 spring.jar, commons-logging.jar, cglib-nodep-...jar, aspectjweaver.jar, aspectjrt.jar 放到 Build Path 下.
以止 lib 除了 spring 外, 其他的都可以在 spring 下载包的 lib 中找到
下面编码开始:
让我们先写两个简单的类:
- package com.longthsoft.learn.spring.models;
- public class A {
- public void sayHello() {
- System.out.println("Hello, I'm a");
- }
- }
- package com.longthsoft.learn.spring.models;
- public class B {
- public void sayHi() {
- System.out.println("Hi, I'm b");
- }
- }
没什么实际的东西, 只是小A和小B在打招呼
接下来把他们交给Spring吧(有点残忍)。
- <?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-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
- <bean id="a" class="com.longthsoft.learn.spring.models.A" />
- <bean id="b" class="com.longthsoft.learn.spring.models.B" />
- </beans>
接下来写个Boot
- package com.longthsoft.learn.spring;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.longthsoft.learn.spring.models.A;
- import com.longthsoft.learn.spring.models.B;
- public final class Boot {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext.xml");
- A a = (A) ctx.getBean("a");
- a.sayHello();
- B b = (B) ctx.getBean("b");
- b.sayHi();
- }
- }
嘿, 这里的运行结果不帖了, 大家脑子里闪过即可。 圣诞到了, 小A小B 介绍完自己之后,也应该说句 "Merry Christmas" Spring 说, 既然你们交给我, 这等 routine 就不用再麻烦了, 直接一并处理掉。 于是:
- package com.longthsoft.learn.spring;
- import org.aspectj.lang.annotation.AfterReturning;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Pointcut;
- @Aspect
- public class SimpleAspect {
- @Pointcut("execution(* com.longthsoft.learn.spring.models.*.say*())")
- public void simplePointcut() { }
- @AfterReturning(pointcut="simplePointcut()")
- public void simpleAdvice() {
- System.out.println("Merry Christmas");
- }
- }
然后修改一下配置文件
- <?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-2.0.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
- <aop:aspectj-autoproxy />
- <bean id="a" class="com.longthsoft.learn.spring.models.A" />
- <bean id="b" class="com.longthsoft.learn.spring.models.B" />
- <bean id="simpleAspect" class="com.longthsoft.learn.spring.SimpleAspect" />
- </beans>
OK, 运行一下:
Hello, I'm a
Merry Christmas
Hi, I'm b
Merry Christmas







评论排行榜