Added fallback approach to try to create a JDK proxy when CGLIB one fails

without this change when a CGLIB proxy fails to be created an exception is thrown
with this change we're trying to save the situation by trying to create a JDK proxy. If that also won't work then we're throwing an exception.

fixes #684
This commit is contained in:
Marcin Grzejszczak
2017-08-22 12:52:35 +02:00
parent 17df781524
commit 58f3d0fce0
2 changed files with 91 additions and 5 deletions

View File

@@ -22,6 +22,9 @@ import java.util.concurrent.Executor;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aop.framework.AopConfigException;
import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
@@ -39,6 +42,8 @@ import org.springframework.util.ReflectionUtils;
*/
class ExecutorBeanPostProcessor implements BeanPostProcessor {
private static final Log log = LogFactory.getLog(ExecutorBeanPostProcessor.class);
private final BeanFactory beanFactory;
ExecutorBeanPostProcessor(BeanFactory beanFactory) {
@@ -60,14 +65,28 @@ class ExecutorBeanPostProcessor implements BeanPostProcessor {
boolean classFinal = Modifier.isFinal(bean.getClass().getModifiers());
boolean cglibProxy = !methodFinal && !classFinal;
Executor executor = (Executor) bean;
ProxyFactoryBean factory = new ProxyFactoryBean();
factory.setProxyTargetClass(cglibProxy);
factory.addAdvice(new ExecutorMethodInterceptor(executor, this.beanFactory));
factory.setTarget(bean);
return factory.getObject();
try {
return createProxy(bean, cglibProxy, executor);
} catch (AopConfigException e) {
if (cglibProxy) {
if (log.isDebugEnabled()) {
log.debug("Exception occurred while trying to create a proxy, falling back to JDK proxy", e);
}
return createProxy(bean, false, executor);
}
throw e;
}
}
return bean;
}
Object createProxy(Object bean, boolean cglibProxy, Executor executor) {
ProxyFactoryBean factory = new ProxyFactoryBean();
factory.setProxyTargetClass(cglibProxy);
factory.addAdvice(new ExecutorMethodInterceptor(executor, this.beanFactory));
factory.setTarget(bean);
return factory.getObject();
}
}
class ExecutorMethodInterceptor implements MethodInterceptor {

View File

@@ -0,0 +1,67 @@
package org.springframework.cloud.sleuth.instrument.async;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.springframework.aop.framework.AopConfigException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.util.ClassUtils;
import static org.assertj.core.api.BDDAssertions.then;
import static org.assertj.core.api.BDDAssertions.thenThrownBy;
/**
* @author Marcin Grzejszczak
*/
@RunWith(MockitoJUnitRunner.class)
public class ExecutorBeanPostProcessorTests {
@Mock BeanFactory beanFactory;
@Test
public void should_create_a_cglib_proxy_by_default() throws Exception {
Object o = new ExecutorBeanPostProcessor(this.beanFactory)
.postProcessAfterInitialization(new Foo(), "foo");
then(o).isInstanceOf(Foo.class);
then(ClassUtils.isCglibProxy(o)).isTrue();
}
class Foo implements Executor {
@Override public void execute(Runnable command) {
}
}
@Test
public void should_create_jdk_proxy_when_cglib_fails_to_be_done() throws Exception {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
Object o = new ExecutorBeanPostProcessor(this.beanFactory)
.postProcessAfterInitialization(service, "foo");
then(o).isInstanceOf(ScheduledExecutorService.class);
then(ClassUtils.isCglibProxy(o)).isFalse();
}
@Test
public void should_throw_exception_when_it_is_not_possible_to_create_any_proxy() throws Exception {
ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor();
ExecutorBeanPostProcessor bpp = new ExecutorBeanPostProcessor(this.beanFactory) {
@Override Object createProxy(Object bean, boolean cglibProxy,
Executor executor) {
throw new AopConfigException("foo");
}
};
thenThrownBy(() -> bpp.postProcessAfterInitialization(service, "foo"))
.isInstanceOf(AopConfigException.class)
.hasMessage("foo");
}
}