看看Pointcut的层次
Spring 提供了整合Advice和Pointcut的接口。
这样我们只要提供PointcutAdvisor, 就能够知道在那个类的那些方法里面做什么事情了。
现在我们还需要另外一个类帮助我们实现AOP。( org.springframework.aop.framework.ProxyFactoryBean)
<!----><bean class="com.javalobby.tnt.spring.aop.InterceptorA" name="interceptorA"><!----><bean class="com.javalobby.tnt.spring.aop.BeforeAdviceA" name="beforeAdviceA"><!----><bean class="com.javalobby.tnt.spring.aop.ExampleController" name="myRawController"></bean></bean></bean>
<!----><bean class="org.springframework.aop.framework.ProxyFactoryBean" name="myController"> <property ref="myRawController" name="target"> <property name="interceptorNames"> <list> <value>beforeAdviceA</value> <value>beforeAdviceA</value> </list> </property></property></bean>
这样我们就对所有方法上面实现了beforeAdviceA,beforeAdviceA拦截。如果我们需要对这个类的特定方法进行拦截,如下
<!----><bean class="com.javalobby.tnt.spring.aop.InterceptorA" name="interceptorA"><!----><bean class="com.javalobby.tnt.spring.aop.BeforeAdviceA" name="beforeAdviceA"><!----><bean class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor" name="pointcut.advisor1"> <property ref="interceptorA" name="advice"> <property value="handleRequestInternal" name="mappedName"></property><!----><bean class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor" name="pointcut.advisor2"> <property ref="beforeAdviceA" name="advice"> <property value="handleRequestInternal" name="mappedName"></property><!----><bean class="com.javalobby.tnt.spring.aop.ExampleController" name="myRawController"></bean></property></bean></property></bean></bean></bean><!----><bean class="org.springframework.aop.framework.ProxyFactoryBean" name="myController"> <property ref="myRawController" name="target"> <property name="interceptorNames"> <list> <value>pointcut.advisor1</value> <value>pointcut.advisor2</value> </list> </property></property></bean>我们就实现了对myRawController的handleRequestInternal方法进行拦截。
If you don&apost need direct (non-AOP&aposd) access to your bean, then it may be better for the simplicity of the file to just use an anonymous inner bean, rather than declaring the bean seperately to the proxy:
<!----><bean class="org.springframework.aop.framework.ProxyFactoryBean" name="myController"> <property name="target"><bean class="com.javalobby.tnt.aop.ExampleController"></bean> <property name="interceptorNames"> <list> <value>beforeAdviceA</value> <value>interceptorA</value> </list> </property></property></bean>
详情请参考那篇文章。