Spring AOP:面向切面编程的深入实践
1. Spring AOP 基础示例
在 Spring AOP 中,我们可以通过定义切入点和通知来实现对方法的拦截和增强。以下是一个简单的示例,展示了如何让魔术师读取志愿者的想法:
<aop:config>
<aop:aspect ref="magician">
<aop:pointcut id="thinking"
expression="execution(* com.springinaction.springidol.Thinker.thinkOfSomething(String)) and args(thoughts)" />
<aop:before
pointcut-ref="thinking"
method="interceptThoughts"
arg-names="thoughts" />
</aop:aspect>
</aop:config>
在这个示例中,切入点 thinking
定义了要拦截的方法 Thinker.thinkOfSomething(String)
,并将参数命名为 thoughts
。 aop:before
通知指定了在切入点匹配的方法执行前调用