Search in sources :

Example 1 with TargetSource

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

the class CommonAnnotationBeanPostProcessor method buildLazyResourceProxy.

/**
	 * Obtain a lazily resolving resource proxy for the given name and type,
	 * delegating to {@link #getResource} on demand once a method call comes in.
	 * @param element the descriptor for the annotated field/method
	 * @param requestingBeanName the name of the requesting bean
	 * @return the resource object (never {@code null})
	 * @since 4.2
	 * @see #getResource
	 * @see Lazy
	 */
protected Object buildLazyResourceProxy(final LookupElement element, final String requestingBeanName) {
    TargetSource ts = new TargetSource() {

        @Override
        public Class<?> getTargetClass() {
            return element.lookupType;
        }

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

        @Override
        public Object getTarget() {
            return getResource(element, requestingBeanName);
        }

        @Override
        public void releaseTarget(Object target) {
        }
    };
    ProxyFactory pf = new ProxyFactory();
    pf.setTargetSource(ts);
    if (element.lookupType.isInterface()) {
        pf.addInterface(element.lookupType);
    }
    ClassLoader classLoader = (this.beanFactory instanceof ConfigurableBeanFactory ? ((ConfigurableBeanFactory) this.beanFactory).getBeanClassLoader() : null);
    return pf.getProxy(classLoader);
}
Also used : ConfigurableBeanFactory(org.springframework.beans.factory.config.ConfigurableBeanFactory) TargetSource(org.springframework.aop.TargetSource) ProxyFactory(org.springframework.aop.framework.ProxyFactory)

Example 2 with TargetSource

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

the class FileTests method testFileHandler.

@Test
public void testFileHandler() throws Exception {
    Message<?> message = MessageBuilder.withPayload("foo").setHeader(FileHeaders.FILENAME, "foo").build();
    try {
        this.fileFlow1Input.send(message);
        fail("NullPointerException expected");
    } catch (Exception e) {
        assertThat(e, instanceOf(MessageHandlingException.class));
        assertThat(e.getCause(), instanceOf(NullPointerException.class));
    }
    DefaultFileNameGenerator fileNameGenerator = new DefaultFileNameGenerator();
    fileNameGenerator.setBeanFactory(this.beanFactory);
    Object targetFileWritingMessageHandler = this.fileWritingMessageHandler;
    if (this.fileWritingMessageHandler instanceof Advised) {
        TargetSource targetSource = ((Advised) this.fileWritingMessageHandler).getTargetSource();
        if (targetSource != null) {
            targetFileWritingMessageHandler = targetSource.getTarget();
        }
    }
    DirectFieldAccessor dfa = new DirectFieldAccessor(targetFileWritingMessageHandler);
    assertEquals(Boolean.FALSE, dfa.getPropertyValue("flushWhenIdle"));
    assertEquals(60000L, dfa.getPropertyValue("flushInterval"));
    dfa.setPropertyValue("fileNameGenerator", fileNameGenerator);
    this.fileFlow1Input.send(message);
    assertTrue(new File(tmpDir.getRoot(), "foo").exists());
    this.fileTriggerFlowInput.send(new GenericMessage<>("trigger"));
    assertTrue(this.flushPredicateCalled.await(10, TimeUnit.SECONDS));
}
Also used : TargetSource(org.springframework.aop.TargetSource) Advised(org.springframework.aop.framework.Advised) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) File(java.io.File) MessageHandlingException(org.springframework.messaging.MessageHandlingException) DefaultFileNameGenerator(org.springframework.integration.file.DefaultFileNameGenerator) Test(org.junit.Test)

Example 3 with TargetSource

use of org.springframework.aop.TargetSource in project tomee by apache.

the class SpringClassUnwrapper method getRealClass.

@Override
public Class<?> getRealClass(Object o) {
    if (AopUtils.isAopProxy(o) && (o instanceof Advised)) {
        Advised advised = (Advised) o;
        try {
            TargetSource targetSource = advised.getTargetSource();
            final Object target;
            try {
                target = targetSource.getTarget();
            } catch (BeanCreationException ex) {
                // be active on the current thread yet
                return getRealClassFromClass(targetSource.getTargetClass());
            }
            if (target == null) {
                Class<?> targetClass = AopUtils.getTargetClass(o);
                if (targetClass != null) {
                    return getRealClassFromClass(targetClass);
                }
            } else {
                return getRealClass(target);
            }
        } catch (Exception ex) {
        // ignore
        }
    } else if (ClassUtils.isCglibProxyClass(o.getClass())) {
        return getRealClassFromClass(AopUtils.getTargetClass(o));
    }
    return o.getClass();
}
Also used : TargetSource(org.springframework.aop.TargetSource) BeanCreationException(org.springframework.beans.factory.BeanCreationException) Advised(org.springframework.aop.framework.Advised) BeanCreationException(org.springframework.beans.factory.BeanCreationException)

Example 4 with TargetSource

use of org.springframework.aop.TargetSource in project disconf by knightliao.

the class PrivateMethodUtil method invokeMethod.

/**
 * spring注入对象的私有方法调用
 *
 * @param owner
 *            注入的对象
 * @param methodName
 *            私有方法名
 * @param parameterTypes
 *            私有方法参数类型
 * @param parameters
 *            私有方法参数
 * @return 私有方法返回值
 */
@SuppressWarnings({ "rawtypes" })
public static Object invokeMethod(final Object owner, final String methodName, final Class[] parameterTypes, final Object[] parameters) throws Exception {
    // get class
    final Class ownerclass = owner.getClass();
    // get property
    try {
        @SuppressWarnings("unchecked") final Method getTargetClass = ownerclass.getMethod("getTargetSource");
        final TargetSource target = (TargetSource) getTargetClass.invoke(owner, new Object[] {});
        final Class targetClass = target.getTargetClass();
        @SuppressWarnings("unchecked") final Method method = targetClass.getDeclaredMethod(methodName, parameterTypes);
        if (!method.isAccessible()) {
            method.setAccessible(true);
        }
        final Object targetInstance = target.getTarget();
        return method.invoke(targetInstance, parameters);
    } catch (NoSuchMethodException e) {
        return invokeMethod(owner, 0, methodName, parameterTypes, parameters);
    // e.printStackTrace();
    }
}
Also used : TargetSource(org.springframework.aop.TargetSource) Method(java.lang.reflect.Method)

Example 5 with TargetSource

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

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 : SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) TargetSource(org.springframework.aop.TargetSource)

Aggregations

TargetSource (org.springframework.aop.TargetSource)19 Advised (org.springframework.aop.framework.Advised)6 SingletonTargetSource (org.springframework.aop.target.SingletonTargetSource)5 Test (org.junit.jupiter.api.Test)3 ProxyFactory (org.springframework.aop.framework.ProxyFactory)3 BeanCreationException (org.springframework.beans.factory.BeanCreationException)3 Method (java.lang.reflect.Method)2 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 JMException (javax.management.JMException)2 BeansException (org.springframework.beans.BeansException)2 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)2 Lifecycle (org.springframework.context.Lifecycle)2 TrackableComponent (org.springframework.integration.support.management.TrackableComponent)2 UnableToRegisterMBeanException (org.springframework.jmx.export.UnableToRegisterMBeanException)2 File (java.io.File)1 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