spring笔记-ProxyFactory(AOP)

https://www.jianshu.com/p/6a7432a1aed8

接上篇讲了一下AopProxy和AopProxyFactory

类图关系


spring笔记-ProxyFactory(AOP)_第1张图片

1.ProxyCreatorSupport

ProxyCreatorSupport继承自AdvisedSupport,
AdvisedSupport提供了属性支持,ProxyCreatorSupport则集成了AopProxyFactory,提供了创建AopProxy功能,组合了AdvisedSupport和AopProxyFactory的功能

/**
 * Base class for proxy factories.
 * Provides convenient access to a configurable AopProxyFactory.
 *
 */
@SuppressWarnings("serial")
public class ProxyCreatorSupport extends AdvisedSupport {

    private AopProxyFactory aopProxyFactory;
    /**
     * Create a new ProxyCreatorSupport instance.
     */
    public ProxyCreatorSupport() {
        this.aopProxyFactory = new DefaultAopProxyFactory();
    }
    /**
     * Subclasses should call this to get a new AOP proxy. They should not
     * create an AOP proxy with {@code this} as an argument.
     */
    protected final synchronized AopProxy createAopProxy() {
        if (!this.active) {
            activate();
        }
        return getAopProxyFactory().createAopProxy(this);
    }
}

2.ProxyFactory

ProxyFactory继承了ProxyCreatorSupport,进一步屏蔽了AopProxyFactory和AdvisedSupport,直接提供getProxy接口,感觉感觉就是自身提供配置属性后就能产生代理对象

代码示例1-ProxyFactory的使用

    @Test
    public void testRemoveAdvisorByReference() {
        TestBean target = new TestBean();
        ProxyFactory pf = new ProxyFactory(target);
        NopInterceptor nop = new NopInterceptor();
        CountingBeforeAdvice cba = new CountingBeforeAdvice();
        Advisor advisor = new DefaultPointcutAdvisor(cba);
        pf.addAdvice(nop);
        pf.addAdvisor(advisor);
        ITestBean proxied = (ITestBean) pf.getProxy();
        proxied.setAge(5);
    }

3.ProxyFactoryBean

ProxyFactory是通过显示编程的方式创建对象,ProxyFactoryBean则是通过配置的方式来创建

代码示例2-ProxyFactoryBean的使用

    
        custom
        666
    

    

    
        org.springframework.tests.sample.beans.ITestBean
        
        debugInterceptor
    

参考:
https://blog.csdn.net/u011734144/article/details/73436666

你可能感兴趣的:(spring笔记-ProxyFactory(AOP))