[1] 导入jar包
com.springsource.net.sf.cglib-2.2.0.jar com.springsource.org.aopalliance-1.0.0.jar com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar commons-logging-1.1.3.jar hamcrest-core-1.3.jar junit-4.12.jar spring-aop-4.0.0.RELEASE.jar spring-aspects-4.0.0.RELEASE.jar spring-beans-4.0.0.RELEASE.jar spring-context-4.0.0.RELEASE.jar spring-core-4.0.0.RELEASE.jar spring-expression-4.0.0.RELEASE.jar
[2] 创建被代理类
由于我们加入了cglib的支持,所以允许被代理类不实现任何接口。但是必须加入IOC容器,否则Spring没法控制和操作。
[3] 创建切面类
@Aspect
@Component
public class LogAspect {
@Before(value=”execution(* com.atguigu.spring.aop.target.Calculator.*(..))”)
public void doBeforeLog() {
System.out.println(“[aop log]method begin”);
}
@AfterReturning(value=”execution(* com.atguigu.spring.aop.target.Calculator.*(..))”)
public void doSuccessLog() {
System.out.println(“[aop log]method successfully end”);
}
@AfterThrowing(value=”execution(* com.atguigu.spring.aop.target.Calculator.*(..))”)
public void doExceptionLog() {
System.out.println(“[aop log]method ended with exception”);
}
@After(value=”execution(* com.atguigu.spring.aop.target.Calculator.*(..))”)
public void doAfterLog() {
System.out.println(“[aop log]method finally end”);
}
}
[4] 配置Spring配置文件
[5] junit测试