SpringAop面向切面的思想
就是在某个方法之前和之后插入
package biz;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.springframework.aop.AfterReturningAdvice;
public class MyAfter implements AfterReturningAdvice{
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("------------后置增强----------------");
System.out.println("在"+arg3+"对象上的"+arg1.getName()+"方法");
System.out.println("传入参数"+Arrays.toString(arg2));
System.out.println("返回的结果"+arg0);
}
}
package biz;
import java.lang.reflect.Method;
import java.util.Arrays;
import org.springframework.aop.MethodBeforeAdvice;
public class MyBefore implements MethodBeforeAdvice{
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("------------前置增强-------------");
System.out.println("在"+arg2+"对象上的"+arg0.getName()+"方法");
System.out.println("传入参数"+Arrays.toString(arg1));
System.out.println("---------------------------");
}
}
<bean id="before" class="biz.MyBefore"/> <bean id="after" class="biz.MyAfter"/> <aop:config> <aop:pointcut id="cutpiont" expression="execution(public void print(String))"/> <aop:advisor pointcut-ref="cutpiont" advice-ref="before"/> <aop:advisor pointcut-ref="cutpiont" advice-ref="after"/> </aop:config>