spring AOP 概念
spring AOP 如何获取目标方法的参数,如何在目标方法执行前,修改目标方法的参数。如何在目标方法执行后,修改目标方法的返回结果。
spring AOP 基于注解的实现
需要依赖的jar
aopalliance-1.0.jar,aspectjrt-1.8.7.jar,cglib-nodep-3.2.0.jar,aspectjweaver-1.8.7.jar
被代理类
package com.sky.lp.AAtestAop;
import org.springframework.stereotype.Component;
@Component
public class Human implements Sleepable{
@Override
public String sleep(String str) {
System.out.println("睡觉了!梦中自有颜如玉!");
return str + "+lp";
// throw new RuntimeException("被代理类抛错");
}
}
被代理类实现的接口
package com.sky.lp.AAtestAop;
public interface Sleepable {
public String sleep(String str);
}
切面
package com.sky.lp.AAtestAop;
import java.util.Date;
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.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Component
@Aspect
public class SleepHelper {
//切点
@Pointcut("execution(* com.sky.lp.AAtestAop.Human.*(..))")
public void sleeppoint() {
}
//环绕通知
@Around("sleeppoint()")
public Object around(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("进入around通知" + new Date().getTime());
Object object = pjp.proceed();// 执行目标方法
System.out.println("退出around通知" + new Date().getTime());
return object;
}
//前置通知
@Before("sleeppoint()")
public void before() {
System.out.println("这里是Before通知" + new Date().getTime());
}
//在连接点正常执行完成后执行,如果连接点抛出异常,则不会执行
@AfterReturning("sleeppoint()")
public void afterReturning() {
System.out.println("这里是afterReturning通知" + new Date().getTime());
}
//在连接点抛出异常后执行,不抛出异常则不执行
@AfterThrowing("sleeppoint()")
public void afterThrowing() {
System.out.println("这里是afterThrowing通知" + new Date().getTime());
}
//在连接点执行完成后执行,不管是正常执行完成,还是抛出异常,都会执行返回通知中的内容
@After("sleeppoint()")
public void after() {
System.out.println("这里是After通知" + new Date().getTime());
}
}
测试类
package com.sky.lp.AAtestAop;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext appCtx = new ClassPathXmlApplicationContext("spring-servlet.xml");
Sleepable sleeper = (Sleepable)appCtx.getBean("human");
String str = sleeper.sleep("lp");
System.out.println("结果:" + str);
}
}
测试类运行结果
进入around通知1447752935590
这里是Before通知1447752935590
睡觉了!梦中自有颜如玉!
退出around通知1447752935590
这里是After通知1447752935590
这里是afterReturning通知1447752935590
结果:lp+lp
spring-servlet.xml 配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
xmlns:aop="http://www.springframework.org/schema/aop"
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/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-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/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<!-- 启动包扫描功能,以便注册带有@Controller、@Service、@repository、@Component等注解的类成为spring的bean -->
<context:annotation-config />
<context:component-scan base-package="com.sky.lp.AAtestAop" />
<!-- <aop:aspectj-autoproxy proxy-target-class="true "/> -->
<!-- proxy-target-class 默认值为 false,设置为 true 意思是 强制使用 CGLIB 动态代理 -->
<!-- 如果不指定 proxy-target-class ,spring会根据 被代理类 是否实现了接口 进行判断 使用jdk动态代理还是使用CGLIB动态代理。 -->
<!-- 如果 被代理类实现了 接口,则使用JDK动态代理,如果没有实现接口,则使用CGLIB动态代理。 -->
<aop:aspectj-autoproxy/>
</beans>