---
title: "  实现AOP------Spring AOP"
author: "xiaowei2002"
date: "2015-08-12T16:36:07Z"
tags: []
categories: ["企业架构"]
slug: "aop-spring-aop"
draft: false
lastmod: "2015-08-12T16:36:07Z"
id: 2233264
url: "https://xiaowei2002.iteye.com/blog/2233264"
---

AOP系列文章:  
      Spring AOP:[http://ray-yui.iteye.com/blog/2024759](http://ray-yui.iteye.com/blog/2024759)  
               CGLIB:[http://ray-yui.iteye.com/blog/2026426](http://ray-yui.iteye.com/blog/2026426)  
          Javassist:[http://ray-yui.iteye.com/blog/2029261](http://ray-yui.iteye.com/blog/2029261)

什么是AOP?  
      AOP是对传统面向对象开发的一种有效的补充,在AOP中概念非常多,请容许笔者省略数千字的概念,只举出一个示例,**面向对象中的类就好比是士兵,我们编写每个类就等同于为每个士兵增加装备和训练士兵的体能,从而达到可以上战场打仗的目的,然而有一天AOP这个魔法师出现了,打开了一个魔法门,告诉每个士兵(类),不用再每个士兵单独这样训练了(编码),从我这个魔法门(切面)走过就能增强士兵自身的能力**,而这种方式,就是AOP,面向切面编程

AOP的实现方式:  
      Spring中有AOP的实现,但Spring的AOP更多是借鉴了AspectJ的方式,**SpringAOP是基于动态代理的,所以只是方法级别的连接点模型,无法做到例如字段或构造函数的接入点,无法让我们创建更细粒度的通知**,若然读者有需要更精确的AOP,请考虑使用AspectJ或JBoss的AOP实现

**SpringAOP使用(基于XML)**

Java代码 [![复制代码](http://ray-yui.iteye.com/images/icon_copy.gif)](http://ray-yui.iteye.com/blog/2024759 "复制代码") [![收藏代码](http://ray-yui.iteye.com/images/icon_star.png)![](http://ray-yui.iteye.com/images/spinner.gif)]()

01. package com.accentrix.ray;
02. import org.aspectj.lang.ProceedingJoinPoint;
03. package com.accentrix.ray;
04. import org.aspectj.lang.ProceedingJoinPoint;
05. /*
06. * 声明切面(Aspect)
07. \*/
08. public class Advice {
09. // 前置执行方法
10. public void beforeMethod() {
11. System.out.println("method before execute");
12. }
13. // 后置执行方法
14. public void afterMethod() {
15. System.out.println("method after execute");
16. }
17. // 抛出异常时方法
18. public void exceptionMethod() {
19. System.out.println("method throw exception execute");
20. }
21. // 环绕通知
22. public void aroundMethod(ProceedingJoinPoint joinPoint) {
23. /*
24. * 环绕通知就类似Filter,在一个方法中包含开始,执行,结束,抛出异常 ,
25. * 甚至可以不调用joinPoint的proceed方法执行真实逻辑 亦可以多次调用
26. * ,全由开发者业务逻辑决定
27. \*/
28. try {
29. System.out.println("before");
30. joinPoint.proceed();
31. System.out.println("after");
32. } catch (Throwable e) {
33. e.printStackTrace();
34. System.out.println("throw exception");
35. }
36. }
37. // 接受参数通知
38. public void hasParamterAfterMethod(String name) {
39. System.out.println("before method " + name);
40. }
41. }

```
package com.accentrix.ray;

import org.aspectj.lang.ProceedingJoinPoint;
package com.accentrix.ray;

import org.aspectj.lang.ProceedingJoinPoint;

/*
 * 声明切面(Aspect)
 */
public class Advice {

	// 前置执行方法
	public void beforeMethod() {
		System.out.println("method before execute");
	}

	// 后置执行方法
	public void afterMethod() {
		System.out.println("method after execute");
	}

	// 抛出异常时方法
	public void exceptionMethod() {
		System.out.println("method throw exception execute");
	}

	// 环绕通知
	public void aroundMethod(ProceedingJoinPoint joinPoint) {
		/*
		 * 环绕通知就类似Filter,在一个方法中包含开始,执行,结束,抛出异常 ,
		 * 甚至可以不调用joinPoint的proceed方法执行真实逻辑 亦可以多次调用
		 * ,全由开发者业务逻辑决定
		 */
		try {
			System.out.println("before");
			joinPoint.proceed();
			System.out.println("after");
		} catch (Throwable e) {
			e.printStackTrace();
			System.out.println("throw exception");
		}
	}

	// 接受参数通知
	public void hasParamterAfterMethod(String name) {
		System.out.println("before method " + name);
	}
}
```

 

Xml代码 [![复制代码](http://ray-yui.iteye.com/images/icon_copy.gif)](http://ray-yui.iteye.com/blog/2024759 "复制代码") [![收藏代码](http://ray-yui.iteye.com/images/icon_star.png)![](http://ray-yui.iteye.com/images/spinner.gif)]()

01. &lt;!-- 声明Bean --&gt;
02. **&lt;bean** id="advice" class="com.accentrix.ray.Advice" **/&gt;**
03. &lt;!-- 配置AOP --&gt;
04. **&lt;aop:config&gt;**
05. &lt;!-- 引用注册的Bean ID --&gt;
06. **&lt;aop:aspect** ref="advice"**&gt;**
07. &lt;!-- 声明切入点,过滤service下的所有类型和所有方法,不限制参数和返回类型 --&gt;
08. **&lt;aop:pointcut** expression="execution(* com.accentrix.ray.service.\*.\*(..))"
09. id="servicePoincut" **/&gt;**
10. &lt;!-- 引用servicePoincut的切入点,调用advice的beforeMethod方法进行前置处理 --&gt;
11. **&lt;aop:before** pointcut-ref="servicePoincut" method="beforeMethod" **/&gt;**
12. &lt;!-- 引用servicePoincut的切入点,调用advice的afterMethod方法进行后置处理 --&gt;
13. **&lt;aop:after** pointcut-ref="servicePoincut" method="afterMethod" **/&gt;**
14. &lt;!-- 引用servicePoincut的切入点,调用advice的exceptionMethod方法进行异常处理 --&gt;
15. **&lt;aop:after-throwing** pointcut-ref="servicePoincut"
16. method="exceptionMethod" **/&gt;**
17. &lt;!-- 引用servicePoincut的切入点,调用advice的aroundMethod方法进行环绕处理 --&gt;
18. **&lt;aop:around** pointcut-ref="servicePoincut" method="aroundMethod" **/&gt;**
19. &lt;!-- 使用如下pointcut即可为advice传递调用真实业务时传递的参数 --&gt;
20. **&lt;aop:after**
21. pointcut="execution(* com.accentrix.ray.service.\*.\*(String)) and args(name)"
22. method="hasParamterAfterMethod" **/&gt;**
23. **&lt;/aop:aspect&gt;**
24. **&lt;/aop:config&gt;**

```
<!-- 声明Bean -->
<bean id="advice" class="com.accentrix.ray.Advice" />

<!-- 配置AOP -->
<aop:config>
	<!-- 引用注册的Bean ID -->
	<aop:aspect ref="advice">
		<!-- 声明切入点,过滤service下的所有类型和所有方法,不限制参数和返回类型 -->
		<aop:pointcut expression="execution(* com.accentrix.ray.service.*.*(..))"
			id="servicePoincut" />

		<!-- 引用servicePoincut的切入点,调用advice的beforeMethod方法进行前置处理 -->
		<aop:before pointcut-ref="servicePoincut" method="beforeMethod" />

		<!-- 引用servicePoincut的切入点,调用advice的afterMethod方法进行后置处理 -->
		<aop:after pointcut-ref="servicePoincut" method="afterMethod" />

		<!-- 引用servicePoincut的切入点,调用advice的exceptionMethod方法进行异常处理 -->
		<aop:after-throwing pointcut-ref="servicePoincut"
			method="exceptionMethod" />

		<!-- 引用servicePoincut的切入点,调用advice的aroundMethod方法进行环绕处理 -->
		<aop:around pointcut-ref="servicePoincut" method="aroundMethod" />

		<!-- 使用如下pointcut即可为advice传递调用真实业务时传递的参数 -->
		<aop:after
			pointcut="execution(* com.accentrix.ray.service.*.*(String)) and args(name)"
			method="hasParamterAfterMethod" />
	</aop:aspect>
</aop:config>
```

      **SpringAOP基于Annotation方式**

Xml代码 [![复制代码](http://ray-yui.iteye.com/images/icon_copy.gif)](http://ray-yui.iteye.com/blog/2024759 "复制代码") [![收藏代码](http://ray-yui.iteye.com/images/icon_star.png)![](http://ray-yui.iteye.com/images/spinner.gif)]()

1. &lt;!-- 需要在applicationContext.xml中增加如下配置 --&gt;
2. **&lt;aop:aspectj-autoproxy** **/&gt;**

```
<!-- 需要在applicationContext.xml中增加如下配置 -->
<aop:aspectj-autoproxy />
```

 

Java代码 [![复制代码](http://ray-yui.iteye.com/images/icon_copy.gif)](http://ray-yui.iteye.com/blog/2024759 "复制代码") [![收藏代码](http://ray-yui.iteye.com/images/icon_star.png)![](http://ray-yui.iteye.com/images/spinner.gif)]()

01. package com.accentrix.ray;
02. import org.aspectj.lang.ProceedingJoinPoint;
03. import org.aspectj.lang.annotation.After;
04. import org.aspectj.lang.annotation.AfterThrowing;
05. import org.aspectj.lang.annotation.Around;
06. import org.aspectj.lang.annotation.Aspect;
07. import org.aspectj.lang.annotation.Before;
08. import org.aspectj.lang.annotation.Pointcut;
09. @Aspect
10. public class AnnotationAdvice {
11. // 声明切入点
12. @Pointcut("execution(* com.accentrix.ray.service.\*.\*(..))")
13. public void myPointcut() {
14. }
15. // 可声明多个切入点
16. @Pointcut("execution(* com.accentrix.ray.service.\*.\*(String) and args(name)")
17. public void hasParamterPointcut(String name) {
18. }
19. // 注解形式前置通知
20. @Before("myPointcut()")
21. public void beforeMethod() {
22. System.out.println("before method execute");
23. }
24. // 注解形式后置通知
25. @After("myPointcut()")
26. public void afterMethod() {
27. System.out.println("after method execute");
28. }
29. // 注解形式异常通知
30. @AfterThrowing("myPointcut()")
31. public void exceptionMethod() {
32. System.out.println("method throw exception execute");
33. }
34. // 注解形式环绕通知
35. @Around("myPointcut()")
36. public void aroundMethod(ProceedingJoinPoint joinPoint) {
37. try {
38. System.out.println("before");
39. joinPoint.proceed();
40. System.out.println("after");
41. } catch (Throwable e) {
42. e.printStackTrace();
43. System.out.println("throw exception");
44. }
45. }
46. // 注解形式传递参数给通知
47. @Before("hasParamterPointcut(name)")
48. public void hasParamterBeforeMethod(String name) {
49. System.out.println("before method execute " + name);
50. }
51. }

```
package com.accentrix.ray;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class AnnotationAdvice {

	// 声明切入点
	@Pointcut("execution(* com.accentrix.ray.service.*.*(..))")
	public void myPointcut() {

	}

	// 可声明多个切入点
	@Pointcut("execution(* com.accentrix.ray.service.*.*(String) and args(name)")
	public void hasParamterPointcut(String name) {

	}

	// 注解形式前置通知
	@Before("myPointcut()")
	public void beforeMethod() {
		System.out.println("before method execute");
	}

	// 注解形式后置通知
	@After("myPointcut()")
	public void afterMethod() {
		System.out.println("after method execute");
	}

	// 注解形式异常通知
	@AfterThrowing("myPointcut()")
	public void exceptionMethod() {
		System.out.println("method throw exception execute");
	}

	// 注解形式环绕通知
	@Around("myPointcut()")
	public void aroundMethod(ProceedingJoinPoint joinPoint) {
		try {
			System.out.println("before");
			joinPoint.proceed();
			System.out.println("after");
		} catch (Throwable e) {
			e.printStackTrace();
			System.out.println("throw exception");
		}
	}

	// 注解形式传递参数给通知
	@Before("hasParamterPointcut(name)")
	public void hasParamterBeforeMethod(String name) {
		System.out.println("before method execute " + name);
	}
}
```

总结:**AOP对面向对象的士兵式编程提供了更好的补充**,亦在广大的开源框架有显著的效果,例如Spring Security就以AOP为核心,在业务程序开发当中AOP亦到处存在,例如拦截并缓存关于菜单和数据字典的数据,例如声明式事务,而Annotation的出现更加为快速便捷开发提供了很好的支持,从上面两种方式对比就得出Annotation的声明更加简洁方便,**所以在业务当中有需要使用自定义的AOP建议采用Annotation的方式,但XML文件的方式亦必须掌握,因为我们时常需要使用到其他框架提供的切面**