Example usage for org.springframework.aop.framework ProxyFactory setOptimize

List of usage examples for org.springframework.aop.framework ProxyFactory setOptimize

Introduction

In this page you can find the example usage for org.springframework.aop.framework ProxyFactory setOptimize.

Prototype

public void setOptimize(boolean optimize) 

Source Link

Document

Set whether proxies should perform aggressive optimizations.

Usage

From source file:introducitons.IntroducitonExample.java

public static void main(String[] args) {
    TargetBean target = new TargetBean();
    target.setName("Loup Garou");
    ProxyFactory pf = new ProxyFactory();
    pf.setTarget(target);//from   ww w .  ja v  a 2s  .c om

    Advisor advisor = new IsModifiedAdvisor();
    pf.addAdvisor(advisor);
    pf.setOptimize(true);
    TargetBean proxy = (TargetBean) pf.getProxy();
    IsModified proxyInterface = (IsModified) proxy;
    System.out.println("Proxy is target bean ?         : " + (proxy instanceof TargetBean));
    System.out.println("Proxy is child of  Ismodified? : " + (proxy instanceof IsModified));

    System.out.println("Has been modified interface? : " + proxyInterface.isModified());
    proxy.setName("Loup Garou");
    System.out.println("Has been modified? Loup Garou != Loup Garou : " + proxyInterface.isModified());
    proxy.setName("Za Warudo");
    System.out.println("Has been modified? : " + proxyInterface.isModified());
}

From source file:com.helpinput.spring.proxy.OptimizeTransactionProxyFactoryBean.java

public void afterPropertiesSet() {
    if (this.target == null) {
        throw new IllegalArgumentException("Property 'target' is required");
    }//from  w w  w. j a va  2s .  c om
    if (this.target instanceof String) {
        throw new IllegalArgumentException("'target' needs to be a bean reference, not a bean name as value");
    }

    ClassLoader proxyClassLoader = target.getClass().getClassLoader();

    ProxyFactory proxyFactory = new ProxyFactory();

    if (this.preInterceptors != null) {
        for (Object interceptor : this.preInterceptors) {
            proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));
        }
    }

    // Add the main interceptor (typically an Advisor).
    proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(createMainInterceptor()));

    if (this.postInterceptors != null) {
        for (Object interceptor : this.postInterceptors) {
            proxyFactory.addAdvisor(this.advisorAdapterRegistry.wrap(interceptor));
        }
    }

    proxyFactory.copyFrom(this);

    TargetSource targetSource = createTargetSource(this.target);
    proxyFactory.setTargetSource(targetSource);

    if (this.proxyInterfaces != null) {
        proxyFactory.setInterfaces(this.proxyInterfaces);
    } else if (!isProxyTargetClass()) {
        // Rely on AOP infrastructure to tell us what interfaces to proxy.
        proxyFactory.setInterfaces(
                ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass(), proxyClassLoader));
    }

    /**
     * use this option to let proxyFactory user cglib to create proxy,otherwise in dynamic script ,this is no dynamic interface
     * ? cglib ??java??
     */
    proxyFactory.setOptimize(true);
    this.proxy = proxyFactory.getProxy(proxyClassLoader);
}