Matching Methods with Classic Spring Pointcuts

 

Pointcut is another core AOP concept, usually appearing as an expression, and allowing you to match certain program execution points to apply an advice. In classic Spring AOP, pointcuts are also declared as Spring beans by using pointcut classes.

 

Spring provides a family of pointcut classes for you to match program execution points. You can simply declare beans of these types in your bean configuration file to define pointcuts. However, if you find that the built-in pointcut classes cannot satisfy your needs, you can write your own by extending StaticMethodMatcherPointcut or DynamicMethodMatcherPointcut. The former matches execution points by the static class and method information only, while the latter matches them by the dynamic argument values as well.

 

How It Works:

we used all codes except of configuration file in previous article.

 

Method Name Pointcuts:

 

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="arithmeticCalculator" class="cn.cgw.aop.ArithmeticCalculatorImpl"/>
    <bean id="unitCalculator" class="cn.cgw.aop.UnitCalculatorImpl"/>
    
    <!-- Advice -->
    <bean id="loggingBeforeAdvice" class="cn.cgw.aop.LoggingBeforeAdvice"/>
    <bean id="loggingAfterAdvice" class="cn.cgw.aop.LoggingAfterAdvice"/>
    <bean id="loggingThrowsAdvice" class="cn.cgw.aop.LoggingThrowsAdvice"/>
    <bean id="loggingAroundAdvice" class="cn.cgw.aop.LoggingAroundAdvice"/>
    
    <!-- PointCut -->
    <bean id="methodNamePointcut" class="org.springframework.aop.support.NameMatchMethodPointcut">
    	<property name="mappedNames">
    		<list>
    			<value>add</value>
    			<value>div</value>
    		</list>
    	</property>
    </bean>
    
    <!-- Advisor -->
    <bean id="methodNameAdvisor" class="org.springframework.aop.support.DefaultPointcutAdvisor">
    	<property name="pointcut" ref="methodNamePointcut"/>
    	<property name="advice" ref="loggingAroundAdvice"/>
    </bean>
    
    <!-- AOP Proxy -->
    <bean id="arithmeticCalculatorProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    	<!--
    	<property name="proxyInterfaces">
    		<list>
    			<value>cn.cgw.aop.ArithmeticCalculator</value>
    		</list>
    	</property>
    	-->
    	<property name="target" ref="arithmeticCalculator"/>
    	<property name="interceptorNames">
    		<list>
    			<value>methodNameAdvisor</value>
    		</list>
    	</property>
    </bean>

</beans> 	

 

Regular Expression Pointcuts:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

    <bean id="arithmeticCalculator" class="cn.cgw.aop.ArithmeticCalculatorImpl"/>
    <bean id="unitCalculator" class="cn.cgw.aop.UnitCalculatorImpl"/>
    
    <!-- Advice -->
    <bean id="loggingBeforeAdvice" class="cn.cgw.aop.LoggingBeforeAdvice"/>
    <bean id="loggingAfterAdvice" class="cn.cgw.aop.LoggingAfterAdvice"/>
    <bean id="loggingThrowsAdvice" class="cn.cgw.aop.LoggingThrowsAdvice"/>
    <bean id="loggingAroundAdvice" class="cn.cgw.aop.LoggingAroundAdvice"/>
  
    <!-- Regular expression pointcut -->
    <bean id="regexpAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    	<property name="patterns">
    		<list>
    			<value>.*mul.*</value>
    			<value>.*div.*</value>
    		</list>
    	</property>
    	<property name="advice" ref="loggingAroundAdvice"/>
    </bean>
    
    <!-- AOP Proxy -->
    <bean id="arithmeticCalculatorProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
    	<!--
    	<property name="proxyInterfaces">
    		<list>
    			<value>cn.cgw.aop.ArithmeticCalculator</value>
    		</list>
    	</property>
    	-->
    	<property name="target" ref="arithmeticCalculator"/>
    	<property name="interceptorNames">
    		<list>			
    			<value>regexpAdvisor</value>
    		</list>
    	</property>
    </bean>

</beans>

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值