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代理.在生成类的过程中产生一个代理对象,返回的就是代理对象本身.
这是第一种:
- package cn.happy.spring17auto01;
- /**
- * Created by linlin on 2017/7/31.
- */
- public interface IDoSome {
- public void adTd();
- public void adTd1();
- public void add2();
- }
- package cn.happy.spring17auto01;
- /**
- * Created by linlin on 2017/7/31.
- */
- public class IDoSomes implements IDoSome {
- public void adTd() {
- System.out.println("=========1=======");
- }
- public void adTd1() {
- System.out.println("=========2=======");
- }
- public void add2() {
- System.out.println("=========3=======");
- }
- }
- package cn.happy.spring17auto01;
- import org.springframework.aop.MethodBeforeAdvice;
- import java.lang.reflect.Method;
- /**
- * Created by linlin on 2017/7/31.
- */
- public class MyBeforeAdvice implements MethodBeforeAdvice{
- public void before(Method method, Object[] objects, Object o) throws Throwable {
- System.out.println("=========before=======");
- }
- }
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
- <bean id="Idosome" class="cn.happy.spring17auto01.IDoSomes"></bean>
- <bean id="beforeAdvice" class="cn.happy.spring17auto01.MyBeforeAdvice"></bean>
- <bean id="beforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
- <property name="advice" ref="beforeAdvice"></property>
- <property name="patterns" value=".*T.*"></property>
- <pre code_snippet_id="2516414" snippet_file_name="blog_20170803_4_5547202" name="code" class="java">package cn.happy.spring18auto02;
- /**
- * Created by linlin on 2017/7/31.
- */
- public class IDoSomes implements IDoSome {
- public void adTd() {
- System.out.println("=========1=======");
- }
- public void adTd1() {
- System.out.println("=========2=======");
- }
- public void add2() {
- System.out.println("=========3=======");
- }
- }
- </pre><br>
- <pre code_snippet_id="2516414" snippet_file_name="blog_20170803_9_2646073" name="code" class="java">package cn.happy.spring18auto02;
- import org.springframework.aop.MethodBeforeAdvice;
- import java.lang.reflect.Method;
- /**
- * Created by linlin on 2017/7/31.
- */
- public class MyBeforeAdvice implements MethodBeforeAdvice{
- public void before(Method method, Object[] objects, Object o) throws Throwable {
- System.out.println("=========before=======");
- }
- }
- </pre><br>
- </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
- class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"></bean></beans>
- <pre></pre>
- <br>
- <pre code_snippet_id="2516414" snippet_file_name="blog_20170803_6_4707387" name="code" class="java"> @Test
- public void test02(){
- ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext13auto01.xml");
- cn.happy.spring17auto01.IDoSome i=(cn.happy.spring17auto01.IDoSome)ctx.getBean("Idosome");
- i.adTd1();
- i.adTd();
- i.add2();
- }</pre>
- <p><br>
- </p>
- <p>第二种:</p><pre code_snippet_id="2516414" snippet_file_name="blog_20170803_7_5961123" name="code" class="java">package cn.happy.spring18auto02;
- /**
- * Created by linlin on 2017/7/31.
- */
- public interface IDoSome {
- public void adTd();
- public void adTd1();
- public void add2();
- }
- </pre><pre code_snippet_id="2516414" snippet_file_name="blog_20170803_8_7804286" name="code" class="java">package cn.happy.spring18auto02;
- /**
- * Created by linlin on 2017/7/31.
- */
- public class IDoSomes implements IDoSome {
- public void adTd() {
- System.out.println("=========1=======");
- }
- public void adTd1() {
- System.out.println("=========2=======");
- }
- public void add2() {
- System.out.println("=========3=======");
- }
- }
- </pre><br>
- <pre code_snippet_id="2516414" snippet_file_name="blog_20170803_9_2646073" name="code" class="java">package cn.happy.spring18auto02;
- import org.springframework.aop.MethodBeforeAdvice;
- import java.lang.reflect.Method;
- /**
- * Created by linlin on 2017/7/31.
- */
- public class MyBeforeAdvice implements MethodBeforeAdvice{
- public void before(Method method, Object[] objects, Object o) throws Throwable {
- System.out.println("=========before=======");
- }
- }
- </pre><br>
- <pre code_snippet_id="2516414" snippet_file_name="blog_20170803_10_8916318" name="code" class="java"><?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:p="http://www.springframework.org/schema/p"
- xmlns:context="http://www.springframework.org/schema/context"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
- http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
- http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd ">
- <bean id="Idosome" class="cn.happy.spring18auto02.IDoSomes"></bean>
- <bean id="beforeAdvice" class="cn.happy.spring18auto02.MyBeforeAdvice"></bean>
- <bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
- <property name="beanNames" value="Idosome"></property>
- <property name="interceptorNames" value="beforeAdvice"></property>
- </bean>
- </beans></pre><br>
- <pre code_snippet_id="2516414" snippet_file_name="blog_20170803_11_9298002" name="code" class="java"> @Test
- public void test03(){
- ApplicationContext ctx=new ClassPathXmlApplicationContext("applicationContext14auto02.xml");
- cn.happy.spring18auto02.IDoSome i=(cn.happy.spring18auto02.IDoSome)ctx.getBean("Idosome");
- i.adTd1();
- i.adTd();
- i.add2();
- }</pre><br>
- <br>
- <p></p>