Search in sources :

Example 6 with TargetSource

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

the class LazyCreationTargetSourceTests method testCreateLazy.

@Test
public void testCreateLazy() {
    TargetSource targetSource = new AbstractLazyCreationTargetSource() {

        @Override
        protected Object createObject() {
            return new InitCountingBean();
        }

        @Override
        public Class<?> getTargetClass() {
            return InitCountingBean.class;
        }
    };
    InitCountingBean proxy = (InitCountingBean) ProxyFactory.getProxy(targetSource);
    assertEquals("Init count should be 0", 0, InitCountingBean.initCount);
    assertEquals("Target class incorrect", InitCountingBean.class, targetSource.getTargetClass());
    assertEquals("Init count should still be 0 after getTargetClass()", 0, InitCountingBean.initCount);
    proxy.doSomething();
    assertEquals("Init count should now be 1", 1, InitCountingBean.initCount);
    proxy.doSomething();
    assertEquals("Init count should still be 1", 1, InitCountingBean.initCount);
}
Also used : TargetSource(org.springframework.aop.TargetSource) Test(org.junit.Test)

Example 7 with TargetSource

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

the class PrototypeBasedTargetSourceTests method testSerializability.

@Test
public void testSerializability() throws Exception {
    MutablePropertyValues tsPvs = new MutablePropertyValues();
    tsPvs.add("targetBeanName", "person");
    RootBeanDefinition tsBd = new RootBeanDefinition(TestTargetSource.class);
    tsBd.setPropertyValues(tsPvs);
    MutablePropertyValues pvs = new MutablePropertyValues();
    RootBeanDefinition bd = new RootBeanDefinition(SerializablePerson.class);
    bd.setPropertyValues(pvs);
    bd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.registerBeanDefinition("ts", tsBd);
    bf.registerBeanDefinition("person", bd);
    TestTargetSource cpts = (TestTargetSource) bf.getBean("ts");
    TargetSource serialized = (TargetSource) SerializationTestUtils.serializeAndDeserialize(cpts);
    assertTrue("Changed to SingletonTargetSource on deserialization", serialized instanceof SingletonTargetSource);
    SingletonTargetSource sts = (SingletonTargetSource) serialized;
    assertNotNull(sts.getTarget());
}
Also used : TargetSource(org.springframework.aop.TargetSource) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.Test)

Example 8 with TargetSource

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

the class MBeanServerConnectionFactoryBean method createLazyConnection.

/**
	 * Creates lazy proxies for the {@code JMXConnector} and {@code MBeanServerConnection}
	 */
private void createLazyConnection() {
    this.connectorTargetSource = new JMXConnectorLazyInitTargetSource();
    TargetSource connectionTargetSource = new MBeanServerConnectionLazyInitTargetSource();
    this.connector = (JMXConnector) new ProxyFactory(JMXConnector.class, this.connectorTargetSource).getProxy(this.beanClassLoader);
    this.connection = (MBeanServerConnection) new ProxyFactory(MBeanServerConnection.class, connectionTargetSource).getProxy(this.beanClassLoader);
}
Also used : TargetSource(org.springframework.aop.TargetSource) AbstractLazyCreationTargetSource(org.springframework.aop.target.AbstractLazyCreationTargetSource) ProxyFactory(org.springframework.aop.framework.ProxyFactory) JMXConnector(javax.management.remote.JMXConnector) MBeanServerConnection(javax.management.MBeanServerConnection)

Example 9 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 10 with TargetSource

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

the class AopProxyUtils method ultimateTargetClass.

/**
	 * Determine the ultimate target class of the given bean instance, traversing
	 * not only a top-level proxy but any number of nested proxies as well &mdash;
	 * as long as possible without side effects, that is, just for singleton targets.
	 * @param candidate the instance to check (might be an AOP proxy)
	 * @return the ultimate target class (or the plain class of the given
	 * object as fallback; never {@code null})
	 * @see org.springframework.aop.TargetClassAware#getTargetClass()
	 * @see Advised#getTargetSource()
	 */
public static Class<?> ultimateTargetClass(Object candidate) {
    Assert.notNull(candidate, "Candidate object must not be null");
    Object current = candidate;
    Class<?> result = null;
    while (current instanceof TargetClassAware) {
        result = ((TargetClassAware) current).getTargetClass();
        Object nested = null;
        if (current instanceof Advised) {
            TargetSource targetSource = ((Advised) current).getTargetSource();
            if (targetSource instanceof SingletonTargetSource) {
                nested = ((SingletonTargetSource) targetSource).getTarget();
            }
        }
        current = nested;
    }
    if (result == null) {
        result = (AopUtils.isCglibProxy(candidate) ? candidate.getClass().getSuperclass() : candidate.getClass());
    }
    return result;
}
Also used : SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) SingletonTargetSource(org.springframework.aop.target.SingletonTargetSource) TargetSource(org.springframework.aop.TargetSource) TargetClassAware(org.springframework.aop.TargetClassAware)

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