经典AOP

来源:互联网 发布:qq国际版for ubuntu 编辑:程序博客网 时间:2024/06/05 08:52
 代理工厂Bean--------》通知
  四种通知
 前置通知:MethodBeforeAdvice
 后置通知  AfterReturningAdvice
 环绕通知  MethodInterceptor
 异常通知  ThrowsAdvice


案例 : ①建一个Subject普通接口

             ②建一个Subject的实现类RealSubject

public interface Subject {    public void say();    public String says();    public void sayss() throws MyException;    public void sayt();}
public class RealSubject implements Subject {    public void say() {        System.out.println("say");    }}

 配置Bean:

  <!--前置-->    <bean id="dao" class="cn.bdqn.lianxi.RealSubject"></bean>    <bean id="before" class="cn.bdqn.lianxi.MyBefore"></bean>    <bean id="myprofxy" class="org.springframework.aop.framework.ProxyFactoryBean">    <property name="target" ref="dao"></property>    <property name="interceptorNames" value="before"></property></bean>    <!--后置-->    <bean id="after" class="cn.bdqn.lianxi.MyAfter"></bean>    <bean id="myprofxyafter" class="org.springframework.aop.framework.ProxyFactoryBean">        <property name="target" ref="dao"></property>        <property name="interceptorNames" value="after"></property>    </bean>    <!--环绕-->    <bean id="inter" class="cn.bdqn.lianxi.MyInter"></bean>    <bean id="myprofxyinter" class="org.springframework.aop.framework.ProxyFactoryBean">        <property name="target" ref="dao"></property>        <property name="interceptorNames" value="inter"></property>    </bean>    <!--异常-->    <bean id="exception" class="cn.bdqn.lianxi.MyThrows"></bean>    <bean id="myprofxythrows" class="org.springframework.aop.framework.ProxyFactoryBean">        <property name="target" ref="dao"></property>        <property name="interceptorNames" value="exception"></property>    </bean>
测试类:
     
//aop前置增强@Testpublic void test04(){    ApplicationContext apx=new ClassPathXmlApplicationContext("applicationContextaopp.xml");    Subject service= (Subject)apx.getBean("myprofxy");    service.say();}//aop后置增强@Testpublic void test05(){    ApplicationContext apx=new ClassPathXmlApplicationContext("applicationContextaopp.xml");    Subject service= (Subject)apx.getBean("myprofxyafter");    service.say();    String says = service.says();    System.out.println(says);}//aop环绕增强@Testpublic void test06(){    ApplicationContext apx=new ClassPathXmlApplicationContext("applicationContextaopp.xml");    Subject service= (Subject)apx.getBean("myprofxyinter");    service.say();    String says = service.says();    System.out.println(says);}//aop异常增强@Testpublic void test07(){    ApplicationContext apx=new ClassPathXmlApplicationContext("applicationContextaopp.xml");    Subject service= (Subject)apx.getBean("myprofxythrows");    service.say();    String says = service.says();    System.out.println(says);}

            


  代理工厂Bean--------》顾问  PointcutAdvisor 接口 
   -----> NameMatchMethodPointcutAdvisor 名称 匹配  方法   切入点  顾问
   ----->RegexpMethodPointcutAdvsisor



包装,选择性的增强目标对象的部分方法
  
 注意事项:如果业务类型有接口,Spring作为一个AOP思想的实现框架,默认底层使用的JDK动态代理,如果我不想使用默认的JDK动态
代理,我们可以强制有接口的情况使用cglib动态代理,加多一个属性,proxyTargetClass ="true' 。然后直接导致的效果是,在测试
方法中我们可以直接向实现类强转,不会报错。
 
    通知Advice是Spring提供的一种切面(Aspect)。但其功能过于简单,只能
将切面织入到目标类的所有目标方法中,无法完成将切面织入到指定目标方法中。


顾问Advisor是Spring提供的另一种切面。其可以完成更为复杂的切面织入功能,能选择性的将增强切面中的部分方法。PointcutAdvisor是顾问的一种,可以指定具体的切入点。顾问将通知进行了包装,会根据不同的通知类型,在不同的时间点,将切面织入到不同的切入点。
PointcutAdvisor接口有两个较为常用的实现类:
 *:NameMatchMethodPointcutAdvisor 名称匹配方法切入点顾问
 *: RegexpMethodPointcutAdvisor 正则表达式匹配方法切入点顾问
      <property name="pattern" value=".*do.*"></property>  表示方法全名(包名,接口名,方法名)
    运算符       名称      意义
     .           点号       表示任意单个字符
     +           加号       表示前一个字符出现一次或者多次
     *           星号       表示前一个字符出现0次或者多次


  名称匹配方法切入点顾问  NameMatchMethodPointcutAdvisor
   <!-- 03.配置顾问   advisor 包装  advice-->
   
<!--名称匹配方法切入点顾问--><bean id="daoo" class="cn.bdqn.lianxi.RealSubject"></bean><bean id="beforee" class="cn.bdqn.lianxi.MyBefore"></bean><bean id="beforeAdvice" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">    <property name="advice" ref="beforee"></property>    <property name="mappedNames" value="say"></property></bean><bean id="myprofxybefore" class="org.springframework.aop.framework.ProxyFactoryBean">    <property name="target" ref="daoo"></property>    <property name="interceptorNames" value="beforeAdvice"></property></bean>

测试类:

//aop名字匹配切入点顾问增强@Testpublic void test08(){    ApplicationContext apx=new ClassPathXmlApplicationContext("applicationContextaopp.xml");    Subject service= (Subject)apx.getBean("myprofxybefore");    service.say();    String says = service.says();    System.out.println(says);}

   

  正则匹配方法切入点顾问  RegexpMethodPointcutAdvisor

Bean:

<bean id="daooo" class="cn.bdqn.lianxi.RealSubject"></bean><bean id="beforeee" class="cn.bdqn.lianxi.MyBefore"></bean><bean id="Advisors" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">   <property name="advice" ref="beforeee"></property>    <property name="patterns" value=".*t"></property></bean><bean id="aaaa" class="org.springframework.aop.framework.ProxyFactoryBean">    <property name="target" ref="daooo"></property>    <property name="interceptorNames" value="Advisors"></property>    <property name="proxyTargetClass" value="true"></property></bean>

// 正则匹配方法切入点顾问@Testpublic void test09(){    ApplicationContext apx=new ClassPathXmlApplicationContext("applicationContextaopp.xml");   Subject  service= (RealSubject)apx.getBean("aaaa");    service.say();    String says = service.says();    System.out.println(says);    service.sayt();}

原创粉丝点击