spring自己的实现aop的几种方式
///========================方式一(根据通知类型的不同常用的有四种情况before,after,around,throws)=================================
第一种情况:
java代码org.springframework.aop.MethodBeforeAdvice
//////////////////////////////////////
public class TestBeforeAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object target)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("TestBeforeAdvice先执行");
}
}
////////////////////////////////////////
xml配置
class="com.tfsoftware.tfww.aop.TestBeforeAdvice">
class="com.tfsoftware.tfww.daoimpl.EmployeeDaoImpl">
class="org.springframework.aop.framework.ProxyFactoryBean">
com.tfsoftware.tfww.dao.EmployeeDao
testBeforeAdvice
第二种情况:
java代码org.springframework.aop.AfterReturningAdvice
//////////////////////////////////////
public class TestAfterAdvice implements AfterReturningAdvice{
public void afterReturning(Object returnValue, Method method,
Object[] args, Object target) throws Throwable {
// TODO Auto-generated method stub
System.out.println("TestAfterAdvice后执行");
}
}
////////////////////////////////////////
xml配置
class="com.tfsoftware.tfww.aop.TestAfterAdvice">
class="com.tfsoftware.tfww.daoimpl.EmployeeDaoImpl">
class="org.springframework.aop.framework.ProxyFactoryBean">
com.tfsoftware.tfww.dao.EmployeeDao
testBeforeAdvice
第三种情况org.aopalliance.intercept.MethodInterceptor
public class TestMethodInterceptor implements MethodInterceptor{
public Object invoke(MethodInvocation arg0) throws Throwable {
// TODO Auto-generated method stub
System.out.println("111111");
Object object = arg0.proceed();
System.out.println("222222");
return object;
}
}
第四种情况:
import java.lang.reflect.Method;
import org.springframework.aop.ThrowsAdvice;
public class TestThrowsAdvice implements ThrowsAdvice {
public void afterThrowing(Throwable throwable){
System.out.println("TestThrowsAdvice");
}
public void afterThrowing(Method method,Object[] args,Object object,Throwable throwable){
System.out.println("TestThrowsAdvice");
}
}
以上几种方式都没有定义到pointcut,这时目标对象的所有方法都可调用
///=========================方式二=====================
class="com.tfsoftware.tfww.aop.TestAfterAdvice">
class="com.tfsoftware.tfww.daoimpl.EmployeeDaoImpl">
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
get*
class="org.springframework.aop.framework.ProxyFactoryBean">
com.tfsoftware.tfww.dao.EmployeeDao
testPointcutAdvisor
方式二与一的区别主要是在配置上(一没有用切入点只用了通知)
方式二的正则表达式做法
经常使用的正则表达式符号
. 匹配任何单个字符
+ 匹配前一个字符一次或多次
* 匹配前一个字符0次或多次
\ 匹配任何正则表达式
第一种方式
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
.*get.*
第二种方式
class="com.tfsoftware.tfww.aop.TestBeforeAdvice">
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
.*get.*
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
testPointcutAdvisor
*Service
第三种方式(与方式二的区别是bean的配置上)
class="com.tfsoftware.tfww.aop.TestBeforeAdvice">
class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
.*get.*
class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator">
阅读(1318) | 评论(0) | 转发(0) |