spring自动代理

spring默认提供的几种auto-proxy: 

 

  • BeanNameAutoProxyCreator   :  可以配置需要被进行auto-proxy的bean names列表,它控制的是需要代理的bean列表
  • InfrastructureAdvisorAutoProxyCreator  
  • DefaultAdvisorAutoProxyCreator :  将对应匹配的advisor,自动添加到spring的bean。它控制的是advisor的匹配,所有的bean都会被自动代理

再思考一下我自己的需求:
1.  允许和BeanNameAutoProxyCreator指定对应的bean names和inteceptorNames,而不是自动代理所有的bean。

 

2.  如果原始对象是proxyFactoryBean,配置的并行加载拦截器是基于同一个proxyFactoryBean
3.  多次的融合机制,是可以进行合并处理。 因为不同的框架会自定义配置一份代理拦截,需要将两份拦截器进行合并处理。

自动代理:基于后处理BeanPostProcessor完成代理.

 代理机制不同

     基于ProxyFactoryBean代理:先有被代理对象,将被代理对象传递给代理对象,为其生成代理.

        自动代理:基于BeanPostProcessor代理.在生成类的过程中产生一个代理对象,返回的就是代理对象本身.

这是第一种:

[java]  view plain  copy
 print ?
  1. package cn.happy.spring17auto01;  
  2.   
  3. /** 
  4.  * Created by linlin on 2017/7/31. 
  5.  */  
  6. public interface IDoSome {  
  7.     public void adTd();  
  8.     public void adTd1();  
  9.     public void add2();  
  10.   
  11.   
  12. }  

[java]  view plain  copy
 print ?
  1. package cn.happy.spring17auto01;  
  2.   
  3. /** 
  4.  * Created by linlin on 2017/7/31. 
  5.  */  
  6. public class IDoSomes implements IDoSome {  
  7.     public void adTd() {  
  8.         System.out.println("=========1=======");  
  9.     }  
  10.     public void adTd1() {  
  11.         System.out.println("=========2=======");  
  12.     }  
  13.     public void add2() {  
  14.         System.out.println("=========3=======");  
  15.     }  
  16.   
  17. }  

[java]  view plain  copy
 print ?
  1. package cn.happy.spring17auto01;  
  2.   
  3. import org.springframework.aop.MethodBeforeAdvice;  
  4.   
  5. import java.lang.reflect.Method;  
  6.   
  7. /** 
  8.  * Created by linlin on 2017/7/31. 
  9.  */  
  10. public class MyBeforeAdvice implements MethodBeforeAdvice{  
  11.     public void before(Method method, Object[] objects, Object o) throws Throwable {  
  12.         System.out.println("=========before=======");  
  13.     }  
  14. }  

[java]  view plain  copy
 print ?
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <beans xmlns="http://www.springframework.org/schema/beans"  
  3.        xmlns:aop="http://www.springframework.org/schema/aop"  
  4.        xmlns:p="http://www.springframework.org/schema/p"  
  5.        xmlns:context="http://www.springframework.org/schema/context"  
  6.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  7.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  8.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  9.    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">  
  10. <bean id="Idosome" class="cn.happy.spring17auto01.IDoSomes"></bean>  
  11.     <bean id="beforeAdvice" class="cn.happy.spring17auto01.MyBeforeAdvice"></bean>  
  12.     <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">  
  13.         <property name="advice" ref="beforeAdvice"></property>  
  14.         <property name="patterns" value=".*T.*"></property>  
  15. <pre code_snippet_id="2516414" snippet_file_name="blog_20170803_4_5547202" name="code" class="java">package cn.happy.spring18auto02;  
  16.   
  17. /** 
  18.  * Created by linlin on 2017/7/31. 
  19.  */  
  20. public class IDoSomes implements IDoSome {  
  21.     public void adTd() {  
  22.         System.out.println("=========1=======");  
  23.     }  
  24.     public void adTd1() {  
  25.         System.out.println("=========2=======");  
  26.     }  
  27.     public void add2() {  
  28.         System.out.println("=========3=======");  
  29.     }  
  30.   
  31. }  
  32. </pre><br>  
  33. <pre code_snippet_id="2516414" snippet_file_name="blog_20170803_9_2646073" name="code" class="java">package cn.happy.spring18auto02;  
  34.   
  35. import org.springframework.aop.MethodBeforeAdvice;  
  36.   
  37. import java.lang.reflect.Method;  
  38.   
  39. /** 
  40.  * Created by linlin on 2017/7/31. 
  41.  */  
  42. public class MyBeforeAdvice implements MethodBeforeAdvice{  
  43.     public void before(Method method, Object[] objects, Object o) throws Throwable {  
  44.         System.out.println("=========before=======");  
  45.     }  
  46. }  
  47. </pre><br>  
  48. </bean><bean id="someProxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="Idosome"></property> <property name="interceptorNames" value="beforeAdvisor"></property><property name="proxyTargetClass" value="true"></property></bean><bean  
  49.  class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean></beans>  
  50. <pre></pre>  
  51. <br>  
  52. <pre code_snippet_id="2516414" snippet_file_name="blog_20170803_6_4707387" name="code" class="java"@Test  
  53.     public void test02(){  
  54.         ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext13auto01.xml");  
  55.         cn.happy.spring17auto01.IDoSome i=(cn.happy.spring17auto01.IDoSome)ctx.getBean("Idosome");  
  56.       i.adTd1();  
  57.       i.adTd();  
  58.         i.add2();  
  59.     }</pre>  
  60. <p><br>  
  61. </p>  
  62. <p>第二种:</p><pre code_snippet_id="2516414" snippet_file_name="blog_20170803_7_5961123" name="code" class="java">package cn.happy.spring18auto02;  
  63.   
  64. /** 
  65.  * Created by linlin on 2017/7/31. 
  66.  */  
  67. public interface IDoSome {  
  68.     public void adTd();  
  69.     public void adTd1();  
  70.     public void add2();  
  71.   
  72.   
  73. }  
  74. </pre><pre code_snippet_id="2516414" snippet_file_name="blog_20170803_8_7804286" name="code" class="java">package cn.happy.spring18auto02;  
  75.   
  76. /** 
  77.  * Created by linlin on 2017/7/31. 
  78.  */  
  79. public class IDoSomes implements IDoSome {  
  80.     public void adTd() {  
  81.         System.out.println("=========1=======");  
  82.     }  
  83.     public void adTd1() {  
  84.         System.out.println("=========2=======");  
  85.     }  
  86.     public void add2() {  
  87.         System.out.println("=========3=======");  
  88.     }  
  89.   
  90. }  
  91. </pre><br>  
  92. <pre code_snippet_id="2516414" snippet_file_name="blog_20170803_9_2646073" name="code" class="java">package cn.happy.spring18auto02;  
  93.   
  94. import org.springframework.aop.MethodBeforeAdvice;  
  95.   
  96. import java.lang.reflect.Method;  
  97.   
  98. /** 
  99.  * Created by linlin on 2017/7/31. 
  100.  */  
  101. public class MyBeforeAdvice implements MethodBeforeAdvice{  
  102.     public void before(Method method, Object[] objects, Object o) throws Throwable {  
  103.         System.out.println("=========before=======");  
  104.     }  
  105. }  
  106. </pre><br>  
  107. <pre code_snippet_id="2516414" snippet_file_name="blog_20170803_10_8916318" name="code" class="java"><?xml version="1.0" encoding="UTF-8"?>  
  108. <beans xmlns="http://www.springframework.org/schema/beans"  
  109.        xmlns:aop="http://www.springframework.org/schema/aop"  
  110.        xmlns:p="http://www.springframework.org/schema/p"  
  111.        xmlns:context="http://www.springframework.org/schema/context"  
  112.        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  113.        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd  
  114.        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd  
  115.    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">  
  116. <bean id="Idosome" class="cn.happy.spring18auto02.IDoSomes"></bean>  
  117.     <bean id="beforeAdvice" class="cn.happy.spring18auto02.MyBeforeAdvice"></bean>  
  118. <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">  
  119.     <property name="beanNames" value="Idosome"></property>  
  120. <property name="interceptorNames" value="beforeAdvice"></property>  
  121. </bean>  
  122. </beans></pre><br>  
  123. <pre code_snippet_id="2516414" snippet_file_name="blog_20170803_11_9298002" name="code" class="java">  @Test  
  124.     public void test03(){  
  125.         ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext14auto02.xml");  
  126.         cn.happy.spring18auto02.IDoSome i=(cn.happy.spring18auto02.IDoSome)ctx.getBean("Idosome");  
  127.         i.adTd1();  
  128.         i.adTd();  
  129.         i.add2();  
  130.     }</pre><br>  
  131. <br>  
  132. <p></p>  
  133.      
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值