`
free9277
  • 浏览: 105203 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

使用切入点匹配方法实现Spring AOP环绕通知

阅读更多

       为AOP代理指定通知时,将增强目标类/代理接口里声明的所有方法。但是大多数情况下,你只想增强部分方法。此时可以使用切入点匹配方法来解决这个问题。

    切入点(pointcut)是另一个核心的AOP概念,它通常以表达式的形式出现,能够匹配特定的程序执行点来通知应用。在Spring AOP里,使用切入点类切入点声明为Spring Bean。

 

<bean id="arithmeticCalculator"
		class="org.mahz.easyaop.calculator.impl.ArithmeticCalculatorImpl" />
<bean id="methodNameAdvisor"
	class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
	<property name="mappedNames">
		<list>
			<value>add</value>
			<value>sub</value>
		</list>
	</property>
	<property name="advice" ref="logginAroundAdvice" />
</bean>

<bean id="logginAroundAdvice" 
         class="org.mahz.easyaop.calculator.aop.LoggingAroundAdvice" />
<bean
	class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
	<property name="beanNames">
		<list>
			<value>*Calculator</value>
		</list>
	</property>
	<property name="interceptorNames">
		<list>
			<value>methodNameAdvisor</value>
		</list>
	</property>
</bean>

     执行结果:

==========================================
============ Test add Method =============
==========================================
The method add()begin with [4.0, 0.0]
4.0 + 0.0 = 4.0
The Method add() ends with 4.0
==========================================
============ Test sub Method =============
==========================================
The method sub()begin with [4.0, 0.0]
4.0 - 0.0 = 4.0
The Method sub() ends with 4.0
4.0 * 0.0 = 0.0

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics