Search in sources :

Example 21 with TargetSource

use of cn.taketoday.aop.TargetSource in project today-framework by TAKETODAY.

the class AbstractSingletonProxyFactoryBean method afterPropertiesSet.

@Override
public void afterPropertiesSet() {
    if (this.target == null) {
        throw new IllegalArgumentException("Property 'target' is required");
    }
    if (this.target instanceof String) {
        throw new IllegalArgumentException("'target' needs to be a bean reference, not a bean name as value");
    }
    if (this.proxyClassLoader == null) {
        this.proxyClassLoader = ClassUtils.getDefaultClassLoader();
    }
    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.
        Class<?> targetClass = targetSource.getTargetClass();
        if (targetClass != null) {
            proxyFactory.setInterfaces(ClassUtils.getAllInterfacesForClass(targetClass, this.proxyClassLoader));
        }
    }
    postProcessProxyFactory(proxyFactory);
    this.proxy = proxyFactory.getProxy(this.proxyClassLoader);
}
Also used : TargetSource(cn.taketoday.aop.TargetSource) SingletonTargetSource(cn.taketoday.aop.target.SingletonTargetSource) ProxyFactory(cn.taketoday.aop.framework.ProxyFactory)

Example 22 with TargetSource

use of cn.taketoday.aop.TargetSource in project today-framework by TAKETODAY.

the class StandardProxyInvoker method dynamicAdvisedProceed.

public static Object dynamicAdvisedProceed(Object proxy, AdvisedSupport advised, TargetInvocation targetInv, Object[] args) throws Throwable {
    Object target = null;
    Object oldProxy = null;
    boolean restore = false;
    final TargetSource targetSource = advised.getTargetSource();
    try {
        if (advised.isExposeProxy()) {
            // Make invocation available if necessary.
            oldProxy = AopContext.setCurrentProxy(proxy);
            restore = true;
        }
        target = targetSource.getTarget();
        final MethodInterceptor[] interceptors = targetInv.getDynamicInterceptors(advised);
        // but just use MethodInvoker invocation of the target.
        if (ObjectUtils.isEmpty(interceptors)) {
            return targetInv.proceed(target, args);
        }
        // We need to create a DynamicStandardMethodInvocation...
        final Object retVal = new DynamicStandardMethodInvocation(proxy, target, targetInv, args, interceptors).proceed();
        assertReturnValue(retVal, targetInv.getMethod());
        return retVal;
    } finally {
        if (target != null && !targetSource.isStatic()) {
            targetSource.releaseTarget(target);
        }
        if (restore) {
            // Restore old proxy.
            AopContext.setCurrentProxy(oldProxy);
        }
    }
}
Also used : TargetSource(cn.taketoday.aop.TargetSource) MethodInterceptor(org.aopalliance.intercept.MethodInterceptor)

Aggregations

TargetSource (cn.taketoday.aop.TargetSource)22 MethodInterceptor (org.aopalliance.intercept.MethodInterceptor)8 Test (org.junit.jupiter.api.Test)7 ProxyFactory (cn.taketoday.aop.framework.ProxyFactory)6 SingletonTargetSource (cn.taketoday.aop.target.SingletonTargetSource)6 StandardBeanFactory (cn.taketoday.beans.factory.support.StandardBeanFactory)4 AdvisedSupport (cn.taketoday.aop.framework.AdvisedSupport)3 DebugInterceptor (cn.taketoday.aop.interceptor.DebugInterceptor)3 HotSwappableTargetSource (cn.taketoday.aop.target.HotSwappableTargetSource)3 ITestBean (cn.taketoday.beans.testfixture.beans.ITestBean)3 TestBean (cn.taketoday.beans.testfixture.beans.TestBean)3 FileNotFoundException (java.io.FileNotFoundException)3 IOException (java.io.IOException)3 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)3 MarshalException (java.rmi.MarshalException)3 SQLException (java.sql.SQLException)3 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)3 Assertions.assertThatIllegalStateException (org.assertj.core.api.Assertions.assertThatIllegalStateException)3 AopInvocationException (cn.taketoday.aop.AopInvocationException)2 AbstractLazyCreationTargetSource (cn.taketoday.aop.target.AbstractLazyCreationTargetSource)2