---
title: "Spring AOP笔记"
author: "freeglad"
date: "2009-03-09T17:35:00Z"
tags: []
slug: "spring-aop"
draft: false
lastmod: "2009-03-09T17:35:00Z"
id: 586397
url: "https://freeglad.iteye.com/blog/586397"
---

最近研究了下spring的aop，从spring2开始，spring支持基于XML和annotation的两种配置，先来看看基于XML配置的实现。

**基于XML配置的实现：**

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;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"  
xmlns:tx="http://www.springframework.org/schema/tx"  
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"&gt;

&lt;bean id="log" class="com.inter.GetLog"&gt;&lt;/bean&gt;

&lt;!--  
注意这里：expression="execution(* com.inter.\*.print\*(..))"  
括号里面第一个\*号代表返回值 接下来 com.inter.\*. 是你要切入的代码的大概路径，这里为什么用大概路径来形容呢  
因为这里的意思是符合以com.inter的路径都会作为选择的对象，也不详细介绍，查下就明白了， print\*(..)是指  
方法名以print开头的都符合，括号里面的 .. 表示参数是随意的都可以。  
\--&gt;  
&lt;aop:config&gt;  
&lt;aop:aspect ref="log"&gt;  
&lt;aop:pointcut id="**printMethods**" expression="execution(* com.inter.\*.print\*(..))"/&gt;  
&lt;aop:around method="**getLog**" pointcut-ref="**printMethods**"/&gt;  
&lt;/aop:aspect&gt;  
&lt;/aop:config&gt;

&lt;aop:config&gt;  
&lt;aop:aspect ref="log"&gt;  
&lt;aop:pointcut id="**dealException**" expression="execution(* com.inter.\*.excep\*(..))"/&gt;  
&lt;aop:around method="**getLog**" pointcut-ref="**dealException**"/&gt;  
&lt;/aop:aspect&gt;  
&lt;/aop:config&gt;

&lt;aop:config&gt;  
&lt;aop:aspect ref="log"&gt;  
&lt;aop:pointcut id="**before**" expression="execution(* com.inter.\*.bef\*(..))"/&gt;  
&lt;aop:before method="**before**" pointcut-ref="**before**"/&gt;  
&lt;/aop:aspect&gt;  
&lt;/aop:config&gt;

&lt;!--要织入代码的bean--&gt;  
&lt;bean id="print" class="com.inter.SystemPrint"&gt;&lt;/bean&gt;

&lt;/beans&gt;

**SystemPrin的实现类：**

public class SystemPrint implements Print{

public String print(String name){  
String result="hello " + name;  
System.out.println(result);  
return result;  
}

public String before(String name){  
String result="before " + name;  
System.out.println(result);  
return result;  
}

public void exception(String name) throws Exception{  
System.out.println(name);  
throw new Exception();  
}  
}

**下面是所要织入的代码，也就是我们要用来打印和处理异常：**

import org.aspectj.lang.ProceedingJoinPoint;  
public class GetLog {  
public void getLog(**ProceedingJoinPoint** joinpoint) throws Throwable {  
System.out.println("===begin===");  
try{  
joinpoint.proceed();  
}  
catch(Exception e)  
{  
System.out.println("log exception!");  
}  
System.out.println("===end=====");  
}

public void before(**JoinPoint** joint) throws Throwable  
{  
System.out.println("===begin===");  
System.out.println("before!");  
}

}

**编写一个测试类，分别测试上面两个方法，可以得到如下的结果：**

===begin===  
hello ding  
\===end=====  
\-----------------  
\===begin===  
laoding  
log exception!  
\===end=====  
\-----------------  
\===begin===  
before!  
before beforeing

**下面来看看基于Annotation的实现：**

&lt;?xml version="1.0" encoding="UTF-8"?&gt;  
&lt;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"  
xmlns:tx="http://www.springframework.org/schema/tx"  
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd  
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd  
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"&gt;

&lt;!--基于@AspectJ切面的驱动器，如果没有这句话 切面的代码是不会执行的，可以试下--&gt;  
&lt;aop:aspectj-autoproxy/&gt;

&lt;bean id="myAspect" class="com.inter.MyAspect"&gt;&lt;/bean&gt;  
&lt;bean id="hello" class="com.inter.HelloImpl"&gt;&lt;/bean&gt;

&lt;/beans&gt;

**HelloImpl类的实现：**

public class HelloImpl implements Hello {  
public String hello(String name)  
{  
String say = "Hello," + name;

System.out.println(say);  
return say;  
}  
}

**切面的实现：**

//首先这是注释这个类就是切面  
@Aspect  
public class MyAspect {  
/\*这里是注释要切入的方法，AfterReturning是表示方法返回以后进行切入，我这里  
选这个的话是因为日志一般都是在方法执行完成后记录，当然你可以拿Before来试\*/  
@Around("execution(* com.inter.\*.hello(..))")  
public void doLog(ProceedingJoinPoint joinpoint) throws Throwable{  
String result = (String)joinpoint.proceed();  
System.out.println("---doLog"+result);  
}  
}

**然后编写一个测试类，得到如下测试结果：**

\-----------------  
Hello,xiao  
\---doLogHello,xiao