Determine FactoryBean object type via generics
For the particular use case detailed in SPR-8514, with this change we
now attempt to determine the object type of a FactoryBean through its
generic type parameter if possible.
For (a contrived) example:
@Configuration
public MyConfig {
@Bean
public FactoryBean<String> fb() {
return new StringFactoryBean("foo");
}
}
The implementation will now look at the <String> generic parameter
instead of attempting to instantiate the FactoryBean in order to call
its #getObjectType() method.
This is important in order to avoid the autowiring lifecycle issues
detailed in SPR-8514. For example, prior to this change, the following
code would fail:
@Configuration
public MyConfig {
@Autowired Foo foo;
@Bean
public FactoryBean<String> fb() {
Assert.notNull(foo);
return new StringFactoryBean("foo");
}
}
The reason for this failure is that in order to perform autowiring,
the container must first determine the object type of all configured
FactoryBeans. Clearly a chicken-and-egg issue, now fixed by this
change.
And lest this be thought of as an obscure bug, keep in mind the use case
of our own JPA support: in order to configure and return a
LocalContainerEntityManagerFactoryBean from a @Bean method, one will
need access to a DataSource, etc -- resources that are likely to
be @Autowired across @Configuration classes for modularity purposes.
Note that while the examples above feature methods with return
types dealing directly with the FactoryBean interface, of course
the implementation deals with subclasses/subinterfaces of FactoryBean
equally as well. See ConfigurationWithFactoryBeanAndAutowiringTests
for complete examples.
There is at least a slight risk here, in that the signature of a
FactoryBean-returing @Bean method may advertise a generic type for the
FactoryBean less specific than the actual object returned (or than
advertised by #getObjectType for that matter). This could mean that an
autowiring target may be missed, that we end up with a kind of
autowiring 'false negative' where FactoryBeans are concerned. This is
probably a less common scenario than the need to work with an autowired
field within a FactoryBean-returning @Bean method, and also has a clear
workaround of making the generic return type more specific.
Issue: SPR-8514
This commit is contained in:
@@ -21,6 +21,9 @@ import java.lang.reflect.Constructor;
|
||||
import java.lang.reflect.InvocationTargetException;
|
||||
import java.lang.reflect.Method;
|
||||
import java.lang.reflect.Modifier;
|
||||
import java.lang.reflect.ParameterizedType;
|
||||
import java.lang.reflect.Type;
|
||||
import java.lang.reflect.WildcardType;
|
||||
import java.security.AccessController;
|
||||
import java.security.PrivilegedAction;
|
||||
import java.security.PrivilegedActionException;
|
||||
@@ -68,6 +71,7 @@ import org.springframework.beans.factory.config.DependencyDescriptor;
|
||||
import org.springframework.beans.factory.config.InstantiationAwareBeanPostProcessor;
|
||||
import org.springframework.beans.factory.config.SmartInstantiationAwareBeanPostProcessor;
|
||||
import org.springframework.beans.factory.config.TypedStringValue;
|
||||
import org.springframework.core.GenericTypeResolver;
|
||||
import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.core.ParameterNameDiscoverer;
|
||||
@@ -651,7 +655,9 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
|
||||
}
|
||||
|
||||
/**
|
||||
* This implementation checks the FactoryBean's <code>getObjectType</code> method
|
||||
* This implementation attempts to query the FactoryBean's generic parameter metadata
|
||||
* if present to determin the object type. If not present, i.e. the FactoryBean is
|
||||
* declared as a raw type, checks the FactoryBean's <code>getObjectType</code> method
|
||||
* on a plain instance of the FactoryBean, without bean properties applied yet.
|
||||
* If this doesn't return a type yet, a full creation of the FactoryBean is
|
||||
* used as fallback (through delegation to the superclass's implementation).
|
||||
@@ -660,14 +666,34 @@ public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFac
|
||||
* it will be fully created to check the type of its exposed object.
|
||||
*/
|
||||
@Override
|
||||
protected Class getTypeForFactoryBean(String beanName, RootBeanDefinition mbd) {
|
||||
FactoryBean fb = (mbd.isSingleton() ?
|
||||
protected Class<?> getTypeForFactoryBean(String beanName, RootBeanDefinition mbd) {
|
||||
Class<?> objectType = null;
|
||||
String factoryBeanName = mbd.getFactoryBeanName();
|
||||
String factoryMethodName = mbd.getFactoryMethodName();
|
||||
if (factoryBeanName != null && factoryMethodName != null) {
|
||||
// Try to obtain the FactoryBean's object type without instantiating it at all.
|
||||
BeanDefinition fbDef = getBeanDefinition(factoryBeanName);
|
||||
if (fbDef instanceof AbstractBeanDefinition) {
|
||||
Class<?> fbClass = ((AbstractBeanDefinition)fbDef).getBeanClass();
|
||||
if (ClassUtils.isCglibProxyClass(fbClass)) {
|
||||
// CGLIB subclass methods hide generic parameters. look at the superclass.
|
||||
fbClass = fbClass.getSuperclass();
|
||||
}
|
||||
Method m = ReflectionUtils.findMethod(fbClass, factoryMethodName);
|
||||
objectType = GenericTypeResolver.resolveReturnTypeArgument(m, FactoryBean.class);
|
||||
if (objectType != null) {
|
||||
return objectType;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
FactoryBean<?> fb = (mbd.isSingleton() ?
|
||||
getSingletonFactoryBeanForTypeCheck(beanName, mbd) :
|
||||
getNonSingletonFactoryBeanForTypeCheck(beanName, mbd));
|
||||
|
||||
if (fb != null) {
|
||||
// Try to obtain the FactoryBean's object type from this early stage of the instance.
|
||||
Class objectType = getTypeForFactoryBean(fb);
|
||||
objectType = getTypeForFactoryBean(fb);
|
||||
if (objectType != null) {
|
||||
return objectType;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user