Create enhanced FactoryBean instance before applying method interceptor (preferably via Objenesis)

Also removes "throws Exception" declaration from ThreadPoolExecutorFactoryBean's getObject method.

Issue: SPR-13095
This commit is contained in:
Juergen Hoeller
2015-06-05 00:09:42 +02:00
parent d195ad216a
commit 869b0bc7ee
3 changed files with 98 additions and 5 deletions

View File

@@ -39,12 +39,17 @@ import org.springframework.cglib.core.SpringNamingPolicy;
import org.springframework.cglib.proxy.Callback;
import org.springframework.cglib.proxy.CallbackFilter;
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.Factory;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.springframework.cglib.proxy.NoOp;
import org.springframework.cglib.transform.ClassEmitterTransformer;
import org.springframework.cglib.transform.TransformingClassGenerator;
import org.springframework.core.annotation.AnnotationUtils;
import org.springframework.objenesis.Objenesis;
import org.springframework.objenesis.ObjenesisException;
import org.springframework.objenesis.ObjenesisStd;
import org.springframework.objenesis.SpringObjenesis;
import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.ObjectUtils;
@@ -82,6 +87,10 @@ class ConfigurationClassEnhancer {
private static final Log logger = LogFactory.getLog(ConfigurationClassEnhancer.class);
private static final Objenesis cachedObjenesis = new SpringObjenesis();
private static final Objenesis nonCachedObjenesis = new ObjenesisStd(false);
/**
* Loads the specified class and generates a CGLIB subclass of it equipped with
@@ -388,9 +397,25 @@ class ConfigurationClassEnhancer {
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(factoryBean.getClass());
enhancer.setUseFactory(false);
enhancer.setNamingPolicy(SpringNamingPolicy.INSTANCE);
enhancer.setCallback(new MethodInterceptor() {
enhancer.setCallbackType(MethodInterceptor.class);
// Ideally create enhanced FactoryBean proxy without constructor side effects,
// analogous to AOP proxy creation in ObjenesisCglibAopProxy...
Class<?> fbClass = enhancer.createClass();
Objenesis objenesis = (enhancer.getUseCache() ? cachedObjenesis : nonCachedObjenesis);
Factory factory;
try {
factory = (Factory) objenesis.newInstance(fbClass);
}
catch (ObjenesisException ex) {
// Fallback to regular proxy construction on unsupported JVMs
logger.debug("Unable to instantiate enhanced FactoryBean using Objenesis, " +
"falling back to regular construction", ex);
factory = (Factory) fbClass.newInstance();
}
factory.setCallback(0, new MethodInterceptor() {
@Override
public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
if (method.getName().equals("getObject") && args.length == 0) {
@@ -399,7 +424,8 @@ class ConfigurationClassEnhancer {
return proxy.invoke(factoryBean, args);
}
});
return enhancer.create();
return factory;
}
private ConfigurableBeanFactory getBeanFactory(Object enhancedConfigInstance) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2013 the original author or authors.
* Copyright 2002-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -187,7 +187,7 @@ public class ThreadPoolExecutorFactoryBean extends ExecutorConfigurationSupport
@Override
public ExecutorService getObject() throws Exception {
public ExecutorService getObject() {
return this.exposedExecutor;
}