簡介
面向切面編程(AOP)提供另外一種角度來思考程序結構,通過這種方式彌補了面向對象編程(OOP)的不足。 除了類(classes)以外,AOP提供了 切面。切面對關注點進行模塊化,例如橫切多個類型和對象的事務管理。 (這些關注點術語通常稱作 橫切(crosscutting) 關注點。)
Spring的一個關鍵的組件就是 AOP框架。 盡管如此,Spring IoC容器并不依賴于AOP,這意味著你可以自由選擇是否使用AOP,AOP提供強大的中間件解決方案,這使得Spring IoC容器更加完善。
Spring 2.0 AOP:
Spring 2.0 引入了一種更加簡單并且更強大的方式來自定義切面,用戶可以選擇使用基于模式(schema-based)的方式或者使用@AspectJ注解。 對于新的應用程序,如果用戶使用Java 5開發,我們推薦用戶使用@AspectJ風格,否則可以使用基于模式的風格。 這兩種風格都完全支持通知(Advice)類型和AspectJ的切入點語言,雖然實際上仍然使用Spring AOP進行織入(Weaving)。
本章主要討論Spring 2.0對基于模式和基于@AspectJ的AOP支持。 Spring 2.0完全保留了對Spring 1.2的向下兼容性,下一章 將討論Spring 1.2 API所提供的底層的AOP支持。
Spring中所使用的AOP:
提供聲明式企業服務,特別是為了替代EJB聲明式服務。 最重要的服務是 聲明性事務管理(declarative transaction management) , 這個服務建立在Spring的抽象事務管理(transaction abstraction)之上。
允許用戶實現自定義的切面,用AOP來完善OOP的使用。
實例
我們經常會用到的有如下幾種
1、基于代理的AOP
2、純簡單java對象切面
3、@Aspect注解形式的
4、注入形式的Aspcet切面
下面我們就一個一個來應用吧.
下面先寫一下幾個基本的類。
接口類:
1
2
3
4
5
6
7
8
9
10
|
/** * 定義一個接口 */ public interface Sleepable { /** * 睡覺方法 */ void sleep(); } |
實現類:
1
2
3
4
5
6
7
8
9
10
11
|
/** * 本人實現睡覺接口 */ public class ChenLliNa implements Sleepable { @Override public void sleep() { // TODO Auto-generated method stub System.out.println( "乖,該睡覺了!" ); } } |
增強類:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/** * 定義一個睡眠的增強 同時實現前置 和后置 */ public class SleepHelper implements MethodBeforeAdvice, AfterReturningAdvice { @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable { System.out.println( "睡覺前要敷面膜" ); } @Override public void before(Method method, Object[] args, Object target) throws Throwable { System.out.println( "睡覺后要做美夢" ); } } |
一、基于代理的AOP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
<!-- 創建一個增強 advice --> < bean id = "sleepHelper" class = "com.tgb.springaop.aspect.SleepHelper" /> < bean id = "lina" class = "com.tgb.springaop.service.impl.ChenLliNa" /> <!-- 定義切點 匹配所有的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 name = "pointcut" ref = "sleepPointcut" /> </ bean > <!-- 定義代理對象 --> < bean id = "linaProxy" class = "org.springframework.aop.framework.ProxyFactoryBean" > < property name = "target" ref = "lina" /> < property name = "interceptorNames" value = "sleepHelperAdvisor" /> <!-- <property name="proxyInterfaces" value="com.tgb.springaop.service.Sleepable"/> --> </ bean > |
如配置文件中:
pattern屬性指定了正則表達式,他匹配所有的sleep方法
使用org.springframework.aop.support.DefaultPointcutAdvisor的目的是為了使切點和增強結合起來形成一個完整的切面
最后配置完后通過org.springframework.aop.framework.ProxyFactoryBean產生一個最終的代理對象。
二、純簡單java對象切面
純簡單java對象切面這話怎么說呢,在我看來就是相對于第一種配置,不需要使用代理,,而是通過spring的內部機制去自動掃描,這時候我們的配置文件就該如下修改:
1
2
3
4
5
6
7
8
9
10
11
12
13
|
<!-- 創建一個增強 advice --> < bean id = "sleepHelper" class = "com.tgb.springaop.aspect.SleepHelper" /> <!-- 目標類 --> < bean id = "lina" class = "com.tgb.springaop.service.impl.ChenLliNa" /> <!-- 配置切點和通知--> < bean id = "sleepAdvisor" class = "org.springframework.aop.support.RegexpMethodPointcutAdvisor" > < property name = "advice" ref = "sleepHelper" ></ property > < property name = "pattern" value = ".*sleep" /> </ bean > <!-- 自動代理配置 --> < bean class = "org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator" /> |
是不是相對于第一種簡單了許多,不用再去配置代理了。
三、@Aspect注解形式
根據我們的經驗也知道,注解的形式相對于配置文件是簡單一些的,這時候需要在已有的方法或類上家注解:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/** * 通過注解的方式 添加增強 */ @Aspect @Component public class SleepHelper03 { /*@Pointcut("execution(* com.tgb.springaop.service.impl..*(..))")*/ @Pointcut ( "execution(* *.sleep(..))" ) public void sleeppoint(){} @Before ( "sleeppoint()" ) public void beforeSleep(){ System.out.println( "睡覺前要敷面膜" ); } @AfterReturning ( "sleeppoint()" ) public void afterSleep(){ System.out.println( "睡覺后要做美夢" ); } |
配置文件中只需寫:
1
2
3
4
5
6
7
|
<!--掃描包 --> < context:component-scan base-package = "com.tgb" annotation-config = "true" /> <!-- ASPECTJ注解 --> < aop:aspectj-autoproxy proxy-target-class = "true" /> <!-- 目標類 --> < bean id = "lina" class = "com.tgb.springaop.service.impl.ChenLliNa" /> |
四、注入形式的Aspcet切面
個人感覺這個是最簡單的也是最常用的,也是最靈活的。配置文件如下:
1
2
3
4
5
6
7
8
9
10
|
<!-- 目標類 --> < bean id = "lina" class = "com.tgb.springaop.service.impl.ChenLliNa" /> < bean id = "sleepHelper" class = "com.tgb.springaop.aspect.SleepHelper02" /> < aop:config > < aop:aspect ref = "sleepHelper" > < aop:before method = "beforeSleep" pointcut = "execution(* *.sleep(..))" /> < aop:after method = "afterSleep" pointcut = "execution(* *.sleep(..))" /> </ aop:aspect > </ aop:config > |
配置文件中提到的SleepHelper02類如下:
1
2
3
4
5
6
7
8
9
10
11
12
|
/** * 通過注解的方式 添加增強 */ public class SleepHelper02 { public void beforeSleep(){ System.out.println( "睡覺前要敷面膜" ); } public void afterSleep(){ System.out.println( "睡覺后要做美夢" ); } } |
是不是看上去都很簡單呀,這樣是不是大家都會使用spring aop了?!
關于如何調用,這里寫了幾個測試類,可以看一下,基本都一樣:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
/** * 配置文件 spring_aop.xml 通過代理 */ @Test public void test(){ ApplicationContext ct = new ClassPathXmlApplicationContext( "spring_aop.xml" ); Sleepable sleeper =(Sleepable) ct.getBean( "linaProxy" ); sleeper.sleep(); } /** * 配置文件 spring_aop_01.xml 簡答的java對象 */ @Test public void test01(){ ApplicationContext ct = new ClassPathXmlApplicationContext( "spring_aop_01.xml" ); Sleepable sleeper = (Sleepable)ct.getBean( "lina" ); sleeper.sleep(); } /** * 配置文件 spring_aop_03.xml 通過aspect注解 */ @Test public void test03(){ ApplicationContext ct = new ClassPathXmlApplicationContext( "spring_aop_03.xml" ); Sleepable sleeper = (Sleepable)ct.getBean( "lina" ); sleeper.sleep(); } /** * 配置文件 spring_aop_02.xml 通過apsect配置文件 * @author 陳麗娜 * @version 2015年5月31日上午10:09:37 */ @Test public void test02(){ ApplicationContext ct = new ClassPathXmlApplicationContext( "spring_aop_02.xml" ); Sleepable sleeper = (Sleepable)ct.getBean( "lina" ); sleeper.sleep(); } |
通過測試類可以看出,不管以什么樣的方式來實現aop他們的使用都是沒有差別的,這幾個測試類的結果都是一樣的: