Spring RegexpMethodPointcutAdvisor和NameMatchMethodPointcutAdvisor
Spring AOP中有两个PointcutAdvisor
——RegexpMethodPointcutAdvisor和 NameMatchMethodPointcutAdvisor,它们都在org.springframework.aop.support包中,
都可以过滤要拦截的方法,配置方法也大致相同,其中一个最主要的区别:
RegexpMethodPointcutAdvisor:需要加上完整的类名和方法名,例如:com.xw.methodname或com.*.methodname或.*methodname。
NameMatchMethodPointcutAdvisor:只需要方法名,不用加类名:*methodname。
1.RegexpMethodPointcutAdvisor
<!-- 将拦截器包装成Advisor-->
<bean id="runAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!-- advice属性确定处理bean-->
<property name="advice">
<bean class="spring.aop.LoggerAdvice"/>
</property>
<property name="patterns">
<list>
<!--正则表达式-->
<value>.*tst</value>
</list>
</property>
</bean>
<bean id="proxyBean_regexpMethod" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="spring.aop.ITest">
</property>
<property name="interceptorNames">
<list>
<value>runAdvisor</value>
</list>
</property>
<property name="target" ref="testproxyfactorybean"></property>
</bean>
<bean id="runAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
<!-- advice属性确定处理bean-->
<property name="advice">
<bean class="spring.aop.LoggerAdvice"/>
</property>
<property name="patterns">
<list>
<!--正则表达式-->
<value>.*tst</value>
</list>
</property>
</bean>
<bean id="proxyBean_regexpMethod" class="org.springframework.aop.framework.ProxyFactoryBean">
<property name="proxyInterfaces" value="spring.aop.ITest">
</property>
<property name="interceptorNames">
<list>
<value>runAdvisor</value>
</list>
</property>
<property name="target" ref="testproxyfactorybean"></property>
</bean>
2.NameMatchMethodPointcutAdvisor
<!-- 将拦截器包装成Advisor,该对象还确定代理对怎样的方法增加处理-->
<bean id="runAdvisor_Name" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<!-- advice属性确定处理bean-->
<property name="advice">
<!-- 此处的处理bean定义采用嵌套bean,也可引用容器的另一个bean-->
<bean class="spring.aop.LoggerAdvice"/>
</property>
<!-- patterns确定正则表达式模式-->
<property name="mappedNames">
<list>
<!-- 确定正则表达式列表-->
<value>tst</value>
</list>
</property>
</bean>
<bean id="runAdvisor_Name" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<!-- advice属性确定处理bean-->
<property name="advice">
<!-- 此处的处理bean定义采用嵌套bean,也可引用容器的另一个bean-->
<bean class="spring.aop.LoggerAdvice"/>
</property>
<!-- patterns确定正则表达式模式-->
<property name="mappedNames">
<list>
<!-- 确定正则表达式列表-->
<value>tst</value>
</list>
</property>
</bean>