终于看到spring引人注目的两大块了IOC和AOP,还是为了让自己更清楚些,所以在此记录下,aop的四种增强,以备后续观摩。
先写一个Person接口
package shamrock.service;
public interface Person {
public String sayHello(String name);
}
创建一个Chinese类实现Person接口
package shamrock.serviceImpl;
import org.springframework.stereotype.Component;
import shamrock.service.Person;
@Component
public class Chinese implements Person {
@Override
public String sayHello(String name) {
// TODO Auto-generated method stub
System.out.println("sayHello调用");
return name+" hello";
}
}
创建增强处理类FourAdviceTest
package shamrock.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import edu.emory.mathcs.backport.java.util.Arrays;
@Aspect
public class FourAdviceTest {
@Around("execution(* shamrock.serviceImpl.*.*(..))")
public Object processTx(ProceedingJoinPoint jp) throws Throwable{
System.out.println("Around增强:执行目标方法之前,模拟开始事务");
Object[] args=jp.getArgs();
if(args!=null&&args.length>0&&args[0].getClass()==String.class){
args[0]="被改变的参数";
}
Object rvt = jp.proceed(args);
System.out.println("Around增强,执行目标方法之后,模拟事务结束");
return rvt+"新增的内容";
}
@Before("execution(* shamrock.serviceImpl.*.*(..))")
public void authority(JoinPoint jp){
System.out.println("Before增强,模拟执行权限");
System.out.println("Before增强,被植入的增强处理目标方法:"+jp.getSignature().getName());
System.out.println("Before增强,目标方法的参数为:"+Arrays.toString(jp.getArgs()));
System.out.println("Before增强,被植入增强处理的目标对象:"+jp.getTarget());
}
@AfterReturning(pointcut="execution(* shamrock.serviceImpl.*.*(..))",returning="rvt")
public void log(JoinPoint jp,Object rvt){
System.out.println("AfterReturning增强:获取目标方法返回值:"+rvt);
System.out.println("AfterReturning增强:模拟记录日志功能");
System.out.println("AfterReturning增强:被植入增强处理的目标方法为:"+jp.getSignature().getName());
System.out.println("AfterReturning增强:目标方法的参数为:"+Arrays.toString(jp.getArgs()));
System.out.println("AfterReturning增强:被植入增强处理的目标对象为:"+jp.getTarget());
}
@After("execution(* shamrock.serviceImpl.*.*(..))")
public void release(JoinPoint jp){
System.out.println("After增强:模拟方法结束后的释放资源。。。");
System.out.println("After增强:被植入增强处理的目标方法为:"+jp.getSignature().getName());
System.out.println("After增强:目标方法的参数为:"+Arrays.toString(jp.getArgs()));
System.out.println("After增强:被植入增强处理的目标对象为:"+jp.getTarget());
}
}
配置spring.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">
<context:component-scan base-package="shamrock.aop,shamrock.serviceImpl">
<context:include-filter type="annotation" expression="org.aspectj.lang.annotation.Aspect"/>
</context:component-scan>
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-autoproxy>
</beans>
创建运行程序
package shamrock.run;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import shamrock.serviceImpl.Chinese;
public class Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext act = new ClassPathXmlApplicationContext("spring.xml");
Chinese ch = (Chinese) act.getBean("chinese");
String str=ch.sayHello("观音");
System.out.println("-----"+str+"------");
}
}
运行结果如下
Around增强:执行目标方法之前,模拟开始事务
Before增强,模拟执行权限
Before增强,被植入的增强处理目标方法:sayHello
Before增强,目标方法的参数为:[被改变的参数]
Before增强,被植入增强处理的目标对象:shamrock.serviceImpl.Chinese@12e3194
sayHello调用
Around增强,执行目标方法之后,模拟事务结束
After增强:模拟方法结束后的释放资源。。。
After增强:被植入增强处理的目标方法为:sayHello
After增强:目标方法的参数为:[被改变的参数]
After增强:被植入增强处理的目标对象为:shamrock.serviceImpl.Chinese@12e3194
AfterReturning增强:获取目标方法返回值:被改变的参数 hello新增的内容
AfterReturning增强:模拟记录日志功能
AfterReturning增强:被植入增强处理的目标方法为:sayHello
AfterReturning增强:目标方法的参数为:[被改变的参数]
AfterReturning增强:被植入增强处理的目标对象为:shamrock.serviceImpl.Chinese@12e3194
-----被改变的参数 hello新增的内容------
到此aop四种增强处理的程序写完,四种增强处理的优先级可以是进入时优先级高的先织入,退出时优先级高的后执行,从运行结果中,我们可以得知
进入时增强处理的优先级出低到高是:Before《Aorund
退出时增强处理的优先级从低到高是:Around《After《AfterReturning