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);
}
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());
}
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);
}
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();
}
}
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 —
* 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;
}
Aggregations