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