---
title: "Spring AOP 2.0 研究"
author: "cfeers"
date: "2010-09-02T15:28:21Z"
tags: []
categories: ["企业架构"]
slug: "spring-aop-2-0"
draft: false
lastmod: "2010-09-02T15:28:21Z"
id: 753602
url: "https://cfeers.iteye.com/blog/753602"
---

我们用Hello的例子来诠释Spring AOP 2.0 的特性。  
Hello

Java代码 [![复制代码](http://snowolf.iteye.com/images/icon_copy.gif)](http://snowolf.iteye.com/blog/236264 "复制代码")

1. **public** **interface** Hello {
2. **void** sayHelloBefore();
3. **void** sayHelloAfter();
4. **void** sayHelloAround();
5. }

```
public interface Hello {
	void sayHelloBefore();

	void sayHelloAfter();

	void sayHelloAround();
}
```

SayHello

Java代码 [![复制代码](http://snowolf.iteye.com/images/icon_copy.gif)](http://snowolf.iteye.com/blog/236264 "复制代码")

01. **public** **class** SayHello **implements** Hello{
02. /\**
03. \*
04. \*/
05. **public** **void** sayHelloBefore() {
06. System.out.println("say Hello before!");
07. }
08. /\**
09. \*
10. \*/
11. **public** **void** sayHelloAfter() {
12. System.out.println("say Hello after!");
13. }
14. /\**
15. \*
16. \*/
17. **public** **void** sayHelloAround() {
18. System.out.println("say Hello around!");
19. }
20. }

```
public class SayHello implements Hello{
	/**
	 * 
	 */
	public void sayHelloBefore() {
		System.out.println("say Hello before!");
	}

	/**
	 * 
	 */
	public void sayHelloAfter() {
		System.out.println("say Hello after!");
	}

	/**
	 * 
	 */
	public void sayHelloAround() {
		System.out.println("say Hello around!");
	}
}
```

Before

Java代码 [![复制代码](http://snowolf.iteye.com/images/icon_copy.gif)](http://snowolf.iteye.com/blog/236264 "复制代码")

1. **public** **class** Before {
2. **public** **void** invoke(JoinPoint joinPoint) {
3. System.out.println("Before:" + joinPoint.getSignature().getName());
4. }
5. }

```
public class Before {
	public void invoke(JoinPoint joinPoint) {
		System.out.println("Before:" + joinPoint.getSignature().getName());
	}
}
```

After

Java代码 [![复制代码](http://snowolf.iteye.com/images/icon_copy.gif)](http://snowolf.iteye.com/blog/236264 "复制代码")

1. **public** **class** After {
2. **public** **void** invoke(JoinPoint joinPoint) {
3. System.out.println("After:" + joinPoint.getSignature().getName());
4. }
5. }

```
public class After {
	public void invoke(JoinPoint joinPoint) {
	System.out.println("After:" + joinPoint.getSignature().getName());
	}
}
```

Around

Java代码 [![复制代码](http://snowolf.iteye.com/images/icon_copy.gif)](http://snowolf.iteye.com/blog/236264 "复制代码")

1. **public** **class** Around {
2. **public** Object invoke(ProceedingJoinPoint joinPoint) **throws** Throwable {
3. System.out.println("Around:" + joinPoint.getSignature().getName());
4. **return** joinPoint.proceed();
5. }
6. }

```
public class Around {
	public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out.println("Around:" + joinPoint.getSignature().getName());
		return joinPoint.proceed();
	}
}
```

Introduction

Java代码 [![复制代码](http://snowolf.iteye.com/images/icon_copy.gif)](http://snowolf.iteye.com/blog/236264 "复制代码")

1. **public** **class** Introduction {
2. **public** Object invoke(ProceedingJoinPoint joinPoint) **throws** Throwable {
3. System.out
4. .println("Introduction:" + joinPoint.getSignature().getName());
5. **return** joinPoint.proceed();
6. }
7. }

```
public class Introduction {
	public Object invoke(ProceedingJoinPoint joinPoint) throws Throwable {
		System.out
				.println("Introduction:" + joinPoint.getSignature().getName());
		return joinPoint.proceed();
	}
}
```

Introduction 引入的作用在于是一个类在不做任何代码修改的前提下，拥有另一套方法，或者说具备另一套本领，比如我们现在就想让这个实现了Hello接口的SayHello类拥有Ok接口的方法

Java代码 [![复制代码](http://snowolf.iteye.com/images/icon_copy.gif)](http://snowolf.iteye.com/blog/236264 "复制代码")

1. **public** **interface** Ok {
2. **void** sayOk();
3. }

```
public interface Ok {
	void sayOk();
}
```

IntroductionOk  首先要解决上述接口Ok的实现

Java代码 [![复制代码](http://snowolf.iteye.com/images/icon_copy.gif)](http://snowolf.iteye.com/blog/236264 "复制代码")

1. **public** **class** IntroductionOk **implements** Ok {
2. @Override
3. **public** **void** sayOk() {
4. System.out.println("Ok!");
5. }
6. }

```
public class IntroductionOk implements Ok {
 
	@Override
	public void sayOk() {
		System.out.println("Ok!");
	}
}
```

AllTest

Java代码 [![复制代码](http://snowolf.iteye.com/images/icon_copy.gif)](http://snowolf.iteye.com/blog/236264 "复制代码")

01. **public** **class** AllTest {
02. **private** ApplicationContext app;
03. /\**
04. * @throws java.lang.Exception
05. \*/
06. @Before
07. **public** **void** setUp() **throws** Exception {
08. app = **new** ClassPathXmlApplicationContext("applicationContext.xml");
09. }
10. @Test
11. **public** **final** **void** testHello() {
12. Hello hello = (Hello) app.getBean("hello");
13. hello.sayHelloBefore();
14. hello.sayHelloAfter();
15. hello.sayHelloAround();
16. // 由于对Hello接口进行了引入，
17. // 使得实现了Hello接口的类可以具备Ok接口的功能。
18. // 同时，我们可以拦截这个方法让他执行其他我们需要执行的操作
19. hello.sayHelloIntroduction();
20. ((Ok) hello).sayOk();
21. }
22. }

```
public class AllTest {
	private ApplicationContext app;

	/**
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception {
		app = new ClassPathXmlApplicationContext("applicationContext.xml");
	}

	@Test
	public final void testHello() {
		Hello hello = (Hello) app.getBean("hello");
		hello.sayHelloBefore();
		hello.sayHelloAfter();
		hello.sayHelloAround();

		// 由于对Hello接口进行了引入，
		// 使得实现了Hello接口的类可以具备Ok接口的功能。
		// 同时，我们可以拦截这个方法让他执行其他我们需要执行的操作
		hello.sayHelloIntroduction();
		((Ok) hello).sayOk();
	}

}
```

关于表达式符号  
.\*+定义目标为包下的所有类  
+定义目标为该接口的所有实现类

Xml代码 [![复制代码](http://snowolf.iteye.com/images/icon_copy.gif)](http://snowolf.iteye.com/blog/236264 "复制代码")

01. **&lt;?xml** version="1.0" encoding="UTF-8"**?&gt;**
02. **&lt;beans**
03. xmlns="http://www.springframework.org/schema/beans"
04. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
05. xmlns:aop="http://www.springframework.org/schema/aop"
06. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
07. http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"**&gt;**
08. **&lt;bean**
09. id="hello"
10. class="org.zlex.aop.SayHello" **/&gt;**
11. &lt;!-- after --&gt;
12. **&lt;bean**
13. id="after"
14. class="org.zlex.aop.After" **/&gt;**
15. **&lt;aop:config&gt;**
16. **&lt;aop:pointcut**
17. id="afterPoint"
18. expression="execution(* org.zlex.aop.Hello.sayHelloAfter(..))" **/&gt;**
19. **&lt;aop:aspect**
20. id="afterAspect"
21. ref="after"**&gt;**
22. **&lt;aop:after**
23. method="invoke"
24. pointcut-ref="afterPoint" **/&gt;**
25. **&lt;/aop:aspect&gt;**
26. **&lt;/aop:config&gt;**
27. &lt;!-- before --&gt;
28. **&lt;bean**
29. id="before"
30. class="org.zlex.aop.Before" **/&gt;**
31. **&lt;aop:config&gt;**
32. **&lt;aop:pointcut**
33. id="beforePoint"
34. expression="execution(* org.zlex.aop.Hello.sayHelloBefore(..))" **/&gt;**
35. **&lt;aop:aspect**
36. id="beforeAspect"
37. ref="before"**&gt;**
38. **&lt;aop:before**
39. method="invoke"
40. pointcut-ref="beforePoint" **/&gt;**
41. **&lt;/aop:aspect&gt;**
42. **&lt;/aop:config&gt;**
43. &lt;!-- around --&gt;
44. **&lt;bean**
45. id="around"
46. class="org.zlex.aop.Around" **/&gt;**
47. **&lt;aop:config&gt;**
48. **&lt;aop:pointcut**
49. id="aroundPoint"
50. expression="execution(* org.zlex.aop.Hello.sayHelloAround(..))" **/&gt;**
51. **&lt;aop:aspect**
52. id="aroundAspect"
53. ref="around"**&gt;**
54. **&lt;aop:around**
55. method="invoke"
56. pointcut-ref="aroundPoint" **/&gt;**
57. **&lt;/aop:aspect&gt;**
58. **&lt;/aop:config&gt;**
59. &lt;!-- introduction --&gt;
60. &lt;!--
61. .\*+是用于包下的，不是用于接口
62. +定义目标为该接口的所有实现类
63. --**&gt;**
64. **&lt;bean**
65. id="introduction"
66. class="org.zlex.aop.Introduction" **/&gt;**
67. **&lt;bean**
68. id="introductionOk"
69. class="org.zlex.aop.IntroductionOk" **/&gt;**
70. **&lt;aop:config&gt;**
71. **&lt;aop:aspect**
72. ref="introduction"**&gt;**
73. **&lt;aop:pointcut**
74. id="introductionPoint"
75. expression="execution(* org.zlex.aop.Hello.sayHelloIntroduction(..))" **/&gt;**
76. **&lt;aop:declare-parents**
77. implement-interface="org.zlex.aop.Ok"
78. types-matching="org.zlex.aop.Hello+"
79. delegate-ref="introductionOk" **/&gt;**
80. **&lt;aop:around**
81. method="invoke"
82. pointcut-ref="introductionPoint" **/&gt;**
83. **&lt;/aop:aspect&gt;**
84. **&lt;/aop:config&gt;**
85. **&lt;/beans&gt;**

```
<?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.xsd
		http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
	<bean
		id="hello"
		class="org.zlex.aop.SayHello" />
	<!-- after -->
	<bean
		id="after"
		class="org.zlex.aop.After" />
	<aop:config>
		<aop:pointcut
			id="afterPoint"
			expression="execution(* org.zlex.aop.Hello.sayHelloAfter(..))" />
		<aop:aspect
			id="afterAspect"
			ref="after">
			<aop:after
				method="invoke"
				pointcut-ref="afterPoint" />
		</aop:aspect>
	</aop:config>
	<!-- before -->
	<bean
		id="before"
		class="org.zlex.aop.Before" />
	<aop:config>
		<aop:pointcut
			id="beforePoint"
			expression="execution(* org.zlex.aop.Hello.sayHelloBefore(..))" />
		<aop:aspect
			id="beforeAspect"
			ref="before">
			<aop:before
				method="invoke"
				pointcut-ref="beforePoint" />
		</aop:aspect>
	</aop:config>
	<!-- around -->
	<bean
		id="around"
		class="org.zlex.aop.Around" />
	<aop:config>
		<aop:pointcut
			id="aroundPoint"
			expression="execution(* org.zlex.aop.Hello.sayHelloAround(..))" />
		<aop:aspect
			id="aroundAspect"
			ref="around">
			<aop:around
				method="invoke"
				pointcut-ref="aroundPoint" />
		</aop:aspect>
	</aop:config>
	<!-- introduction -->
	<!-- 
	.*+是用于包下的，不是用于接口
	+定义目标为该接口的所有实现类
	 -->
	<bean
		id="introduction"
		class="org.zlex.aop.Introduction" />
	<bean
		id="introductionOk"
		class="org.zlex.aop.IntroductionOk" />
	<aop:config>
		<aop:aspect
			ref="introduction">
			<aop:pointcut
				id="introductionPoint"
				expression="execution(* org.zlex.aop.Hello.sayHelloIntroduction(..))" />
			<aop:declare-parents
				implement-interface="org.zlex.aop.Ok"
				types-matching="org.zlex.aop.Hello+"
				delegate-ref="introductionOk" />
			<aop:around
				method="invoke"
				pointcut-ref="introductionPoint" />
		</aop:aspect>
	</aop:config>
</beans>
```

执行查看日志：

引用

Before:sayHelloBefore  
say SayHello before!  
say Hello after!  
After:sayHelloAfter  
Around:sayHelloAround  
say Hello around!  
Introduction:sayHelloIntroduction  
say Hello introduction!  
Ok!

仔细观察

引用

Before:sayHelloBefore  
say Hello before!

在输出*say Hello before!*前输出*Before:sayHelloBefore*

引用

say Hello after!  
After:sayHelloAfter

在输出*say Hello after!*后输出*After:sayHelloAfter*  
Around比较特殊，可以用ProceedingJoinPoint调用proceed()方法

引用

Around:sayHelloAround  
say Hello around!

 

引用

Introduction:sayHelloIntroduction  
say Hello introduction!  
Ok!

同样是Hello接口，在执行了sayHelloIntroduction方法时被拦截，同时输出*say Hello introduction!*，此时还可以执行Ok的方法输出

引用

Ok!

。显然，Hello的实现类多了Ok接口的本领。  
Spring Beans 结构图如下：  
![](http://cfeers.iteye.com/upload/attachment/97607/96d51eeb-0867-3445-bafc-5c41b87fd9d3.jpg "点击查看原始大小图片")  
如此这样，就可以控制AOP了，当前资料少，实例也少，继续研究！希望能做出来点实际的东西。