您的位置:首页 > 编程语言 > Java开发

Spring实现AOP的4种方式

2015-10-24 23:39 525 查看
先了解AOP的相关术语

1、通知(Advice)

通知定义了切面是什么以及何时使用。描述了切面要完成的工作和何时需要执行这个工作。

2、连接点(Joinpoint)

程序能够应用通知的一个“时机”,这些“时机”就是连接点,例如方法被调用时、异常被抛出时等等。

3、切入点(Pointcut)

通知定义了切面要发生的“故事”和时间,那么切入点就定义了“故事”发生的地点,例如某个类或方法的名称,Spring中允许我们方便的用正则表达式来指定

4、切面(Aspect)

通知和切入点共同组成了切面:时间、地点和要发生的“故事”

5、引入(Introduction)

引入允许我们向现有的类添加新的方法和属性(Spring提供了一个方法注入的功能)

6、目标(Target)

即被通知的对象,如果没有AOP,那么它的逻辑将要交叉别的事务逻辑,有了AOP之后它可以只关注自己要做的事(AOP让他做爱做的事)

7、代理(proxy)

应用通知的对象,详细内容参见设计模式里面的代理模式

8、织入(Weaving)

把切面应用到目标对象来创建新的代理对象的过程,织入一般发生在如下几个时机:

(1)编译时:当一个类文件被编译时进行织入,这需要特殊的编译器才可以做的到,例如AspectJ的织入编译器

(2)类加载时:使用特殊的ClassLoader在目标类被加载到程序之前增强类的字节代码

(3)运行时:切面在运行的某个时刻被织入,SpringAOP就是以这种方式织入切面的,原理应该是使用了JDK的动态代理技术

Spring提供了4种实现AOP的方式

经典的基于代理的AOP

自动代理的AOP

AOP标签配置到切面

@AspectJ注解驱动的切面

Spring支持五种类型的通知

Before(前) org.apringframework.aop.MethodBeforeAdvice

After-returning(返回后)

org.springframework.aop.AfterReturningAdvice

After-throwing(抛出后)

org.springframework.aop.ThrowsAdvice

Arround(周围)

org.aopaliance.intercept.MethodInterceptor

Introduction(引入)

org.springframework.aop.IntroductionInterceptor

方式一:经典的基于代理的AOP

首先写一个接口叫Sleepable,所有具有睡觉能力的东西都可以实现该接口。

package com.ghs.aop;

public interface Sleepable {
public void sleep();
}


然后写一个Human类,他实现了这个接口

package com.ghs.aop;

public class Human implements Sleepable {
@Override
public void sleep() {
System.out.println("正在睡觉!");
}
}


不过睡觉前后要做些辅助工作,最基本的是脱穿衣服,但是这些动作与纯粹的睡觉这一“业务逻辑”是不相干的,如果把

这些代码全部加入到sleep方法中,是不是有违单一职责呢?,这时候我们就需要AOP了。

编写一个SleepHelper类,它里面包含了睡觉的辅助工作,用AOP术语来说它就应该是通知了。

package com.ghs.aop;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;

public class SleepHelper implements MethodBeforeAdvice,AfterReturningAdvice{

@Override
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("睡觉前要脱衣服!");
}

@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("起床后要穿衣服!");
}
}


配置切点、通知、Human对象的代理。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd" default-autowire="byName">

<!-- 定义通知 -->
<bean id="sleepHelper" class="com.ghs.aop.SleepHelper">
</bean>

<!-- 定义切点 -->
<!-- pattern属性指定了正则表达式,它匹配所有的sleep方法 -->
<bean id="sleepPointcut" class="org.springframework.aop.support.JdkRegexpMethodPointcut">
<property name="pattern" value=".*sleep"></property>
</bean>

<!-- 定义通知者 -->
<bean id="sleepHelperAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
<property name="advice" ref="sleepHelper"></property>
<property name="pointcut" ref="sleepPointcut"></property>
</bean>

<!-- 产生代理对象 -->
<bean id="humanProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="target" ref="human"></property>
<property name="interceptorNames" value="sleepHelperAdvisor"></property>
<property name="proxyInterfaces" value="com.ghs.aop.Sleepable"></property>
</bean>

<bean id="human" class="com.ghs.aop.Human">
</bean>
</beans>


方式二:改进方式一,使用自动代理功能

Spring提供了一种自动代理的功能,能让切点跟通知自动进行匹配,修改配置文件如下:

<bean id="sleepHelper" class="test.spring.aop.bean.SleepHelper">
</bean>
<bean id="sleepAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<property name="advice" ref="sleepHelper"/>
<property name="pattern" value=".*sleep"/>
</bean>
<bean id="human" class="test.spring.aop.bean.Human">
</bean>
<bean class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>


方式三:使用AOP标签

修改配置文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">
<bean id="sleepHelper" class="com.ghs.aop.SleepHelper2"/>
<aop:config>
<aop:aspect id="simpleAspect" ref="sleepHelper">
<aop:pointcut expression="execution(* *.sleep(..))" id="sleepPointcut"/>
<aop:before method="beforeSleep" pointcut-ref="sleepPointcut"/>
<aop:after method="afterSleep" pointcut-ref="sleepPointcut"/>
</aop:aspect>
</aop:config>

<bean id="human" class="com.ghs.aop.Human"/>
</beans>


方式四:使用AspectJ提供的注解

package com.ghs.aop;

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 SleepHelper4{

@Pointcut("execution(* *.sleep(..))")
public void sleepPointcut(){
System.out.println("睡觉吧!");
}

@Before("sleepPointcut()")
public void beforeSleep(){
System.out.println("睡觉前要脱衣服!");
}

@After("sleepPointcut()")
public void afterSleep(){
System.out.println("起床后要穿衣服!");
}
}


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:tx="http://www.springframework.org/schema/tx"
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-3.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" default-autowire="byName">

<!-- 自动扫描被@Aspect标注的切面 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<bean id="human" class="com.ghs.aop.Human"/>
</beans>


示例代码:http://download.csdn.net/detail/u011983531/9211773
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  AOP spring 切面 AspectJ