Skip to content

Commit 48836e2

Browse files
committed
Allow parameters in FactoryBean-returning @bean methods
Prior to this change, an assumption was made in AbstractAutowireCapableBeanFactory that any factory-method would have zero parameters. This may not be the case in @bean methods. We now look for the factory-method by name in a more flexible fashion that accomodates the possibility of method parameters. There remains at least one edge cases here where things could still fail, for example a @configuration class could have two FactoryBean-returning methods of the same name, but each with different generic FactoryBean types and different parameter lists. In this case, the implementation may infer and return the wrong object type, as it currently returns the first match for the given factory-method name. The complexity cost of ensuring that this never happens is not likely worth the trouble given the very low likelihood of such an arrangement. Issue: SPR-8762
1 parent 8669997 commit 48836e2

File tree

3 files changed

+99
-10
lines changed

3 files changed

+99
-10
lines changed

org.springframework.beans/src/main/java/org/springframework/beans/factory/support/AbstractAutowireCapableBeanFactory.java

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2010 the original author or authors.
2+
* Copyright 2002-2011 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -105,6 +105,7 @@
105105
* @author Rob Harrop
106106
* @author Mark Fisher
107107
* @author Costin Leau
108+
* @author Chris Beams
108109
* @since 13.02.2004
109110
* @see RootBeanDefinition
110111
* @see DefaultListableBeanFactory
@@ -663,9 +664,10 @@ protected Class getTypeForFactoryMethod(String beanName, RootBeanDefinition mbd,
663664
*/
664665
@Override
665666
protected Class<?> getTypeForFactoryBean(String beanName, RootBeanDefinition mbd) {
666-
Class<?> objectType = null;
667+
class Holder { Class<?> value = null; }
668+
final Holder objectType = new Holder();
667669
String factoryBeanName = mbd.getFactoryBeanName();
668-
String factoryMethodName = mbd.getFactoryMethodName();
670+
final String factoryMethodName = mbd.getFactoryMethodName();
669671
if (factoryBeanName != null && factoryMethodName != null) {
670672
// Try to obtain the FactoryBean's object type without instantiating it at all.
671673
BeanDefinition fbDef = getBeanDefinition(factoryBeanName);
@@ -675,10 +677,19 @@ protected Class<?> getTypeForFactoryBean(String beanName, RootBeanDefinition mbd
675677
// CGLIB subclass methods hide generic parameters. look at the superclass.
676678
fbClass = fbClass.getSuperclass();
677679
}
678-
Method m = ReflectionUtils.findMethod(fbClass, factoryMethodName);
679-
objectType = GenericTypeResolver.resolveReturnTypeArgument(m, FactoryBean.class);
680-
if (objectType != null) {
681-
return objectType;
680+
// find the given factory method, taking into account that in the case of
681+
// @Bean methods, there may be parameters present.
682+
ReflectionUtils.doWithMethods(fbClass,
683+
new ReflectionUtils.MethodCallback() {
684+
public void doWith(Method method) throws IllegalArgumentException, IllegalAccessException {
685+
if (method.getName().equals(factoryMethodName) &&
686+
FactoryBean.class.isAssignableFrom(method.getReturnType())) {
687+
objectType.value = GenericTypeResolver.resolveReturnTypeArgument(method, FactoryBean.class);
688+
}
689+
}
690+
});
691+
if (objectType.value != null) {
692+
return objectType.value;
682693
}
683694
}
684695
}
@@ -689,9 +700,9 @@ protected Class<?> getTypeForFactoryBean(String beanName, RootBeanDefinition mbd
689700

690701
if (fb != null) {
691702
// Try to obtain the FactoryBean's object type from this early stage of the instance.
692-
objectType = getTypeForFactoryBean(fb);
693-
if (objectType != null) {
694-
return objectType;
703+
objectType.value = getTypeForFactoryBean(fb);
704+
if (objectType.value != null) {
705+
return objectType.value;
695706
}
696707
}
697708

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
* Copyright 2002-2011 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.context.annotation;
18+
19+
import static org.junit.Assert.assertNotNull;
20+
21+
import org.junit.Test;
22+
import org.springframework.beans.factory.FactoryBean;
23+
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.beans.factory.annotation.Value;
25+
import org.springframework.context.ApplicationContext;
26+
27+
28+
/**
29+
* Test case cornering the bug initially raised with SPR-8762, in which a
30+
* NullPointerException would be raised if a FactoryBean-returning @Bean method also
31+
* accepts parameters
32+
*
33+
* @author Chris Beams
34+
* @since 3.1
35+
*/
36+
public class ConfigurationWithFactoryBeanAndParametersTests {
37+
@Test
38+
public void test() {
39+
ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class, Bar.class);
40+
assertNotNull(ctx.getBean(Bar.class).foo);
41+
}
42+
}
43+
44+
@Configuration
45+
class Config {
46+
@Bean
47+
public FactoryBean<Foo> fb(@Value("42") String answer) {
48+
return new FooFactoryBean();
49+
}
50+
}
51+
52+
class Foo {
53+
}
54+
55+
class Bar {
56+
Foo foo;
57+
58+
@Autowired
59+
public Bar(Foo foo) {
60+
this.foo = foo;
61+
}
62+
}
63+
64+
class FooFactoryBean implements FactoryBean<Foo> {
65+
66+
public Foo getObject() {
67+
return new Foo();
68+
}
69+
70+
public Class<Foo> getObjectType() {
71+
return Foo.class;
72+
}
73+
74+
public boolean isSingleton() {
75+
return true;
76+
}
77+
}

org.springframework.core/src/main/java/org/springframework/core/GenericTypeResolver.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public static Class<?> resolveReturnType(Method method, Class clazz) {
112112
* if not resolvable or if the single argument is of type {@link WildcardType}.
113113
*/
114114
public static Class<?> resolveReturnTypeArgument(Method method, Class<?> genericIfc) {
115+
Assert.notNull(method, "method must not be null");
115116
Type returnType = method.getReturnType();
116117
Type genericReturnType = method.getGenericReturnType();
117118
if (returnType.equals(genericIfc)) {

0 commit comments

Comments
 (0)