1. 建立工程,导入Spring AOP所需的jar包。
2. Spring配置文件applicationContext.xml:
<?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:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-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/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 启动@AspectJ支持 -->
<aop:aspectj-autoproxy proxy-target-class="true" />
<!-- 自动扫描指定包及其子包下的所有Bean类 -->
<context:component-scan base-package="com.huey" />
</beans>
3. 定义切面Bean
package com.huey.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
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.springframework.stereotype.Component;
/**
* 切面类,由@Aspect配置
* @author huey2672
*
*/
@Aspect
@Component
public class LogAspect {
/**
* 前置通知,由@Before配置,并指定切入点
* 前置通知在目标方法执行前被织入
*/
@Before("execution(* com.huey.service.*.*(..))")
public void doBefore() {
System.out.println("Before Advice");
}
/**
* 后置通知,由@AfterReturning配置,并指定切入点和目标方法的返回值
* 后置通知在目标方法正常执行后被织入
*/
@AfterReturning(returning="rvt", pointcut="execution(* com.huey.service.*.*(..))")
public void doAfterReturning(Object rvt) {
System.out.println("AfterReturning Advice");
System.out.println("目标方法的返回值:" + rvt);
}
/**
* 异常通知,由@AfterReturning配置,并指定切入点和目标方法抛出的异常
* 异常通知在目标方法抛出异常后被织入
* 异常通知虽然处理了目标方法抛出的异常,但不能完全处理,异常仍然向上级调用者抛出
*/
@AfterThrowing(throwing="ex", pointcut="execution(* com.huey.service.*.*(..))")
public void doAfterThrowing(Throwable ex) {
System.out.println("AfterReturning Advice");
System.out.println("目标方法中抛出的异常:" + ex);
}
/**
* 最终通知,由@After配置,并指定切入点
* 最终通知无论在目标方法正常执行还是抛出异常后都会被织入
*/
@After("execution(* com.huey.service.*.*(..))")
public void doAfter() {
System.out.println("After Advice");
}
/**
* 环绕通知,由@Around配置,并指定切入点
* 环绕通知即可以在目标方法之前织入,也可以在执行目标方法之后织入
* 环绕通知处理方法的第一个形参必须是ProceedingJoinPoint类型,
* 调用ProceedingJoinPoint的proceed方法才会执行目标方法
*/
@Around("execution(* com.huey.service.*.*(..))")
public Object doAround(ProceedingJoinPoint jp) throws Throwable {
System.out.println("Around Advice 在目标方法之前织入");
// 获取目标方法的参数,可以对其进行修改
Object[] args = jp.getArgs();
// 执行目标方法,并得到返回值,可以对其进行修改
Object rvt = jp.proceed(args);
System.out.println("Around Advice 在目标方法之后织入");
return rvt;
}
}
4. 目标类:
package com.huey.service;
import org.springframework.stereotype.Component;
/**
*
* @author Huey
*
*/
@Component("helloService")
public class HelloService {
public void sayHello(String name) {
System.out.println("Hello " + name);
}
}
5. 测试用例:
package com.huey.test;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.huey.service.HelloService;
/**
*
* @author huey2672
*
*/
public class AopTest {
private HelloService helloService;
{
// 创建Spring容器
ApplicationContext appCtx =
new ClassPathXmlApplicationContext("applicationContext.xml");
// 通过容器获得EntityServ的实例
helloService = appCtx.getBean("helloService", HelloService.class);
}
@Test
public void testAop() throws Exception {
helloService.sayHello("sugar");
}
}
6. 结果输出:
Around Advice 在目标方法之前织入
Before Advice
Hello sugar
Around Advice 在目标方法之后织入
After Advice
AfterReturning Advice
目标方法的返回值:null