springframework(十)AOP之Pointcut、Advisor

来源:互联网 发布:淘宝打包 编辑:程序博客网 时间:2024/06/14 06:33
上篇的所定义的Advice都是直接织入至代理接口执行前后的,或者在执行方法过程中出现异常的时候织入。事实上还有更加细致的织入时机。Pointcut定义了感兴趣的Jointpoint(Advice的应用时机)。在spring中,使用PointcutAdvisor提供的Pointcut实例,具体结合Advice,Spring内建的Pointcut都有对应的PointcutAdvisor。


官方文档解释:在Spring里,一个advisor是一个仅仅包含一个通知对象和与之关联的切入点表达式的切面。


除了引入这种特殊形式,任何advisor都可以和任何通知一起工作。 org.springframework.aop.support.DefaultPointcutAdvisor是最常用的advisor类。

例如,它可以和: MethodInterceptor,BeforeAdvice 或者 ThrowsAdvice一起使用。


在Spring里有可能在同一个AOP代理里模糊advisor和通知类型。

例如,你可以在一个代理配置里使用一个interception环绕通知,一个异常通知和一个前置通知:Spring将负责自动创建所需的拦截器链。


我的通俗解释:就是将一个advice和对应不同的规则进行匹配,就是将advice给包装升级了,使用的时候和advice一样使用


 1、NameMatchMethodPointcoutAdvisor(这里是一个织入时机的更加细致的处理,通过名字来对应,这样很明显的一个好处就是,我可以针对要代理类中的部分方法进行织入,而不是每个方法都进行织入)

[xhtml] view plain copy
  1. <bean id="beforeAdvice" class="com.itcast.advice.LogBeforeAdvice"></bean>  
  2. <bean id="afterAdvice" class="com.itcast.advice.LogAfterAdvice"></bean>  
  3. <bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean">  
  4. <property name="target" ref="helloSpeaker"></property>  
  5. <property name="interceptorNames">  
  6.  <list>  
  7.  <value>helloAdvise</value>  
  8.  </list>  
  9. </property>  
  10. </bean>  
  11. <!--helloAdvise的名字匹配的方式来匹配要进行advice处理的方法-->  
  12. <bean id="helloAdvise" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">  
  13. <property name="mappedName" value="hello*"></property>  
  14. <property name="advice" ref="beforeAdvice"></property>  
  15. </bean>  


或者还可以定义成如下方式:

[xhtml] view plain copy
  1. <!--helloAdvise直接指定要引入的advice方法的那些类文件中的具体方法列表-->  
  2. <bean id="helloAdvise" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">  
  3.  <property name="mappedNames" >  
  4.  <list>  
  5.  <value>helloAaa</value>  
  6.  <value>helloBbb</value>  
  7.  </list>  
  8.  </property>  
  9.  <property name="advice" ref="beforeAdvice"></property>  
  10. </bean>  
 
 2、RegExpMethodPointcutAdvisor(这里是通过正则表达式来编写Pointcut的表示式)
  定义的时候可以使用的符号:
   . 符合任何单一字符
   + 符合前一个字符一次或者多次
   * 符合前一个字符零次或者多次
   /  Escape任何Regular expression使用到的符号
写法例如:
[xhtml] view plain copy
  1. <bean id="beforeAdvice" class="com.itcast.advice.LogBeforeAdvice"></bean>  
  2. <bean id="afterAdvice" class="com.itcast.advice.LogAfterAdvice"></bean>  
  3. <bean id="helloProxy" class="org.springframework.aop.framework.ProxyFactoryBean">  
  4. <property name="target" ref="helloSpeaker"></property>  
  5. <property name="interceptorNames">  
  6.  <list>  
  7.  <value>regAdvise</value>  
  8.  </list>  
  9. </property>  
  10. </bean>  
  11. <bean id="regAdvise" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
  12.    <property name="advice" ref="afterAdvice"></property>  
  13.    <property name="pattern" value=".*Bbb"></property>  
  14. </bean>  

这里是根据正则表达式来完成对于相关要进行advisor的方法。

 

总结一下:上边的几种方式的目的都是一样的就是为了给我们定义的代理类更加方便增加advice方法。手段无外乎就似乎用正则表达式、名字匹配的方法。总之就是灵活的配置,最后选择哪种,看你的具体需求。

 上边涉及的核心类文件不再重复贴出,只贴出调用验证的类:(这里只是需要获取helloProxy这个代理bean,调用他的方法看效果就ok了)

[c-sharp] view plain copy
  1. package com.itcast.aop.xml;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. import com.itcast.proxy.IHello;  
  7.   
  8. public class SpringAopMain {  
  9.   
  10.     public static void main(String[] args) {  
  11.   
  12.         ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-config.xml");  
  13.   
  14.   
  15.         /**测试advise的使用*/  
  16.         IHello hello =(IHello)ctx.getBean("helloProxy");  
  17.         hello.hiAAA("测试aaa");  
  18.    }  
  19. }  

有兴趣的可以根据上边的配置文件完成不同种类的声明方式进行组合测试。

本篇的核心点就是配置文件上的声明,配置文件上的各种声明方式,帮我们给被代理的bean赋予了不同的advice、advisor。

 

0 0
原创粉丝点击