转自:http://blog.163.com/mr_zyf/blog/static/60242161201110189522740/
1.在pom.xml文件<dependencies>中,增加spring3 aop,所需要的dependency。
xml代码:
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.11</version>
</dependency>
<dependency>
<groupId>cglib</groupId>
<artifactId>cglib</artifactId>
<version>2.1</version>
</dependency>
2. 在项目的src目录,分别生成main/java/zyf/samples/MyService.java,main/java/zyf/samples/aop/MyAdvice.java和test/java/zyf/samples/MyTest.java文件。
MyService.java代码:
package zyf.samples;
/**
* author: [email protected]
* blog: http://blog.163.com/mr_zyf
**/
public class MyService
{
public void show()
{
System.out.println( getClass().toString()+" show Hello World!" );
}
}
MyAdvice.java代码:
package zyf.samples.aop;
import org.aspectj.lang.ProceedingJoinPoint;
//定义通知
/**
* author: [email protected]
* blog: http://blog.163.com/mr_zyf
**/
public class MyAdvice{
//before通知方法
public void beforeShow(){
System.out.println( getClass().toString()+" before show" );
}
//after通知方法
public void afterShow(){
System.out.println( getClass().toString()+" after show" );
}
//afterReturn通知方法
public void afterReturnShow(){
System.out.println( getClass().toString()+" afterReturn show" );
}
//afterThrowing通知方法
public void afterThrowingShow(){
System.out.println( getClass().toString()+" afterThrowing show" );
}
//around通知方法
public void aroundShow(ProceedingJoinPoint jpoint){
try{
System.out.println( getClass().toString()+" around before show" );
//执行目标对象的连接点处的方法
jpoint.proceed();
System.out.println( getClass().toString()+" around after show" );
}catch(Throwable e){
System.out.println( getClass().toString()+" around afterThrowing show" );
}
}
}
MyTest.java代码:
package zyf.samples;
import org.junit.runner.RunWith;
import org.junit.Test;
import static org.junit.Assert.*;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
/**
* author: [email protected]
* blog: http://blog.163.com/mr_zyf
**/
@RunWith(SpringJUnit4ClassRunner.class)
//自动载入classpath:/zyf/samples/MyTest-context.xml
@ContextConfiguration
public class MyTest {
//通过myService Bean,载入对象
@Autowired
private MyService myService;
@Test
public void testService() {
myService.show();
assertTrue( true );
}
}
3.在项目的src目录,生成test/resources/zyf/samples/MyTest-context.xml,部署aspect。
xml代码:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<aop:config>
<!--author: [email protected] blog: http://blog.163.com/mr_zyf-->
<!-- 定义切面 引用通知的bean-->
<aop:aspect id="my-aspect1" ref="myAdvice">
<!-- 设置切入点 -->
<aop:pointcut id="pointcut1" expression="execution(* zyf.samples.MyService.*(..))"/>
<!--指定before通知方法为,myAdvice.beforeShow(),引用切入点pointcut1-->
<aop:before method="beforeShow" pointcut-ref="pointcut1"/>
<!--指定after通知方法为,myAdvice.afterShow(),引用切入点pointcut1-->
<aop:after method="afterShow" pointcut-ref="pointcut1"/>
<!--指定afterReturn通知方法为,myAdvice.afterReturnShow(),引用切入点pointcut1-->
<aop:after-returning method="afterReturnShow" pointcut-ref="pointcut1"/>
<!--指定afterThrowing通知方法为,myAdvice.afterThrowingShow(),引用切入点pointcut1-->
<aop:after-throwing method="afterThrowingShow" pointcut-ref="pointcut1"/>
<!--指定around通知方法为,myAdvice.aroundShow(),引用切入点pointcut1-->
<aop:around method="aroundShow" pointcut-ref="pointcut1"/>
</aop:aspect>
</aop:config>
<!-- 生成切面通知的bean -->
<bean id="myAdvice" class="zyf.samples.aop.MyAdvice"/>
<!-- 生成zyf.samples.MyService的bean -->
<bean id="myService" class="zyf.samples.MyService"></bean>
</beans>
4. 运行:mvn test。