Search in sources :

Example 11 with TargetSource

use of org.springframework.aop.TargetSource in project spring-framework by spring-projects.

the class ProxyFactoryBean method newPrototypeInstance.

/**
	 * Create a new prototype instance of this class's created proxy object,
	 * backed by an independent AdvisedSupport configuration.
	 * @return a totally independent proxy, whose advice we may manipulate in isolation
	 */
private synchronized Object newPrototypeInstance() {
    // but will have an independent copy.
    if (logger.isTraceEnabled()) {
        logger.trace("Creating copy of prototype ProxyFactoryBean config: " + this);
    }
    ProxyCreatorSupport copy = new ProxyCreatorSupport(getAopProxyFactory());
    // The copy needs a fresh advisor chain, and a fresh TargetSource.
    TargetSource targetSource = freshTargetSource();
    copy.copyConfigurationFrom(this, targetSource, freshAdvisorChain());
    if (this.autodetectInterfaces && getProxiedInterfaces().length == 0 && !isProxyTargetClass()) {
        // Rely on AOP infrastructure to tell us what interfaces to proxy.
        copy.setInterfaces(ClassUtils.getAllInterfacesForClass(targetSource.getTargetClass(), this.proxyClassLoader));
    }
    copy.setFrozen(this.freezeProxy);
    if (logger.isTraceEnabled()) {
        logger.trace("Using ProxyCreatorSupport copy: " + copy);
    }
    return getProxy(copy.createAopProxy());
}
Also used : TargetSource(org.springframework.aop.TargetSource) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource)

Example 12 with TargetSource

use of org.springframework.aop.TargetSource in project spring-framework by spring-projects.

the class AbstractAutoProxyCreator method postProcessBeforeInstantiation.

@Override
public Object postProcessBeforeInstantiation(Class<?> beanClass, String beanName) throws BeansException {
    Object cacheKey = getCacheKey(beanClass, beanName);
    if (beanName == null || !this.targetSourcedBeans.contains(beanName)) {
        if (this.advisedBeans.containsKey(cacheKey)) {
            return null;
        }
        if (isInfrastructureClass(beanClass) || shouldSkip(beanClass, beanName)) {
            this.advisedBeans.put(cacheKey, Boolean.FALSE);
            return null;
        }
    }
    // The TargetSource will handle target instances in a custom fashion.
    if (beanName != null) {
        TargetSource targetSource = getCustomTargetSource(beanClass, beanName);
        if (targetSource != null) {
            this.targetSourcedBeans.add(beanName);
            Object[] specificInterceptors = getAdvicesAndAdvisorsForBean(beanClass, beanName, targetSource);
            Object proxy = createProxy(beanClass, beanName, specificInterceptors, targetSource);
            this.proxyTypes.put(cacheKey, proxy.getClass());
            return proxy;
        }
    }
    return null;
}
Also used : TargetSource(org.springframework.aop.TargetSource) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource)

Example 13 with TargetSource

use of org.springframework.aop.TargetSource in project spring-framework by spring-projects.

the class AbstractAopProxyTests method testProxyIsBoundBeforeTargetSourceInvoked.

@Test
public void testProxyIsBoundBeforeTargetSourceInvoked() {
    final TestBean target = new TestBean();
    ProxyFactory pf = new ProxyFactory(target);
    pf.addAdvice(new DebugInterceptor());
    pf.setExposeProxy(true);
    final ITestBean proxy = (ITestBean) createProxy(pf);
    Advised config = (Advised) proxy;
    // This class just checks proxy is bound before getTarget() call
    config.setTargetSource(new TargetSource() {

        @Override
        public Class<?> getTargetClass() {
            return TestBean.class;
        }

        @Override
        public boolean isStatic() {
            return false;
        }

        @Override
        public Object getTarget() throws Exception {
            assertEquals(proxy, AopContext.currentProxy());
            return target;
        }

        @Override
        public void releaseTarget(Object target) throws Exception {
        }
    });
    // Just test anything: it will fail if context wasn't found
    assertEquals(0, proxy.getAge());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TargetSource(org.springframework.aop.TargetSource) HotSwappableTargetSource(org.springframework.aop.target.HotSwappableTargetSource) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DebugInterceptor(org.springframework.aop.interceptor.DebugInterceptor) FileNotFoundException(java.io.FileNotFoundException) SQLException(java.sql.SQLException) MarshalException(java.rmi.MarshalException) IOException(java.io.IOException) UndeclaredThrowableException(java.lang.reflect.UndeclaredThrowableException) LockedException(test.mixin.LockedException) Test(org.junit.Test)

Aggregations

TargetSource (org.springframework.aop.TargetSource)13 SingletonTargetSource (org.springframework.aop.target.SingletonTargetSource)5 Test (org.junit.Test)3 ProxyFactory (org.springframework.aop.framework.ProxyFactory)3 Method (java.lang.reflect.Method)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UndeclaredThrowableException (java.lang.reflect.UndeclaredThrowableException)1 MarshalException (java.rmi.MarshalException)1 SQLException (java.sql.SQLException)1 MBeanServerConnection (javax.management.MBeanServerConnection)1 JMXConnector (javax.management.remote.JMXConnector)1 MethodInvocation (org.aopalliance.intercept.MethodInvocation)1 AopInvocationException (org.springframework.aop.AopInvocationException)1 TargetClassAware (org.springframework.aop.TargetClassAware)1 DebugInterceptor (org.springframework.aop.interceptor.DebugInterceptor)1 AbstractLazyCreationTargetSource (org.springframework.aop.target.AbstractLazyCreationTargetSource)1 HotSwappableTargetSource (org.springframework.aop.target.HotSwappableTargetSource)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1