---
title: "Spring的AOP_XXXXXX"
author: "fojian"
date: "2009-11-15T19:17:53Z"
tags: []
categories: ["研发管理"]
slug: "spring-aop-xxxxxx"
draft: false
lastmod: "2009-11-15T19:18:38Z"
id: 517665
url: "https://fojian.iteye.com/blog/517665"
---

1、相关概念：  
1)Aspect(切面)：指横切性关注点的抽象即为切面。与类相似，只是两者的关注点不同，类是对物体特征的抽象，而切面是横切性关注点的抽象。  
2)Joinpoint(连接点)：指一些被拦截到的点。在Spring中，这些点指的是方法，因为Spring只支持方法类型的连接点，实际上连接点还可以是field或类构造器。  
3)Advice(通知)：指拦截到连接点之后所要做的事情，分为前置通知，后置通知，异常通知，最终通知和环绕通知。  
4)Pointcut(切入点)：指对那些Jointpoint进行拦截的定义。  
5)Target(目标对象)：代理要实现的目标对象。  
6)Weave(织入)：指将Aspects应用到Target对象并导致proxy对象创建的过程。  
7)Introduction(引入)：在不修改类代码的前提下，Introduction可以在运行期间为类动态地添加一些方法或Field.

 

2、使用Spring的注解方式实现AOP  
首先要在XML文件中添加以下语句：  
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans ……  
       ……   
       xmlns:aop="[http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop)"       
       ……  
       [http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop)                  [http://www.springframework.org/schema/aop/spring-aop-2.5.xsd](http://www.springframework.org/schema/aop/spring-aop-2.5.xsd)"&gt;  
        
 &lt;aop:aspectj-autoproxy/&gt;  
         
 ……

&lt;/beans&gt;  
然后就可以将类声明为切面，在切面中设置切入点，通知等，如：  
@Aspect [//@Aspect]()声明一个切面  @Component 将切面交给Spring管理

```
public class MyInterceptor {
	@Pointcut("execution(* com.spring.aop.service.impl.PersonserviceBean.*(..))")
	private void anyMethod(){}//声明一个切入点
	
	@Before("anyMethod() && args(name)")
	public void doAccessCheck(String name){//设置前置通知
		System.out.println("here is before advice!"+name);
	}
	
	@AfterReturning(pointcut="anyMethod()",returning="result")
	public void doAfterReturning(String result){//设置后置通知
		System.out.println("here is AfterReturning advice! "+result);
	}
	
	@After("anyMethod()")
	public void doAfter(){//设置最终通知
		System.out.println("here is After advice!");
	}
	
	@Around("anyMethod()")//设置环绕通知，必须有下面的参数话和调用proceed()方法
	public Object doAroundAdvice(ProceedingJoinPoint pjp)throws Throwable{
		System.out.println("go into the method!");
		Object result = pjp.proceed();
		System.out.println("drop back the method!");
		return result;	
	}
}
```

 

3、使用Spring的XML配置文件实现AOP  
 只需要将声明为切面的Bean在配置文件中声明，在使用配置文件中的切面设置进行具体配置,如：  
&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;beans xmlns="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)"  
       xmlns:xsi="[http://www.w3.org/2001/XMLSchema-instance](http://www.w3.org/2001/XMLSchema-instance)"  
       xmlns:context="[http://www.springframework.org/schema/context](http://www.springframework.org/schema/context)"  
       xmlns:aop="[http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop)"       
       xsi:schemaLocation="[http://www.springframework.org/schema/beans](http://www.springframework.org/schema/beans)  
           [http://www.springframework.org/schema/beans/spring-beans-2.5.xsd](http://www.springframework.org/schema/beans/spring-beans-2.5.xsd)  
           [http://www.springframework.org/schema/context](http://www.springframework.org/schema/context) [http://www.springframework.org/schema/context/spring-context-2.5.xsd](http://www.springframework.org/schema/context/spring-context-2.5.xsd)  
           [http://www.springframework.org/schema/aop](http://www.springframework.org/schema/aop) [http://www.springframework.org/schema/aop/spring-aop-2.5.xsd](http://www.springframework.org/schema/aop/spring-aop-2.5.xsd)"&gt;  
        &lt;aop:aspectj-autoproxy/&gt;  
        
        &lt;bean id="aspetbean" class="com.fojian.service.MyInterceptor"/&gt;&lt;!--将需要做切面的Bean交Spring管理--&gt;  
        &lt;aop:config&gt;  
         &lt;aop:aspect id="asp" ref="aspetbean"&gt;&lt;!--声明切面--&gt;  
          &lt;aop:pointcut id="mycut" expression="execution(* com.fojian.service..\*.\*(..))"/&gt;&lt;!--声明切入点--&gt;  
          &lt;aop:before pointcut-ref="mycut" method="doAccessCheck"/&gt;&lt;!--声明前置通知--&gt;  
          &lt;aop:after-returning pointcut-ref="mycut" method="doAfterReturning"/&gt;&lt;!--声明后置通知--&gt;  
   &lt;aop:after-throwing pointcut-ref="mycut" method="doAfterThrowing"/&gt;&lt;!--声明例外通知--&gt;  
   &lt;aop:after pointcut-ref="mycut" method="doAfter"/&gt;&lt;!--声明最终通知--&gt;  
   &lt;aop:around pointcut-ref="mycut" method="doBasicProfiling"/&gt;&lt;!--声明环绕通知--&gt;  
         &lt;/aop:aspect&gt;  
        &lt;/aop:config&gt;  
&lt;/beans&gt;