forked from spring-projects/spring-framework
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
- Loading branch information
Showing
3 changed files
with
99 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 77 additions & 0 deletions
77
...rg/springframework/context/annotation/ConfigurationWithFactoryBeanAndParametersTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
/* | ||
* Copyright 2002-2011 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.context.annotation; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
|
||
import org.junit.Test; | ||
import org.springframework.beans.factory.FactoryBean; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.ApplicationContext; | ||
|
||
|
||
/** | ||
* Test case cornering the bug initially raised with SPR-8762, in which a | ||
* NullPointerException would be raised if a FactoryBean-returning @Bean method also | ||
* accepts parameters | ||
* | ||
* @author Chris Beams | ||
* @since 3.1 | ||
*/ | ||
public class ConfigurationWithFactoryBeanAndParametersTests { | ||
@Test | ||
public void test() { | ||
ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class, Bar.class); | ||
assertNotNull(ctx.getBean(Bar.class).foo); | ||
} | ||
} | ||
|
||
@Configuration | ||
class Config { | ||
@Bean | ||
public FactoryBean<Foo> fb(@Value("42") String answer) { | ||
return new FooFactoryBean(); | ||
} | ||
} | ||
|
||
class Foo { | ||
} | ||
|
||
class Bar { | ||
Foo foo; | ||
|
||
@Autowired | ||
public Bar(Foo foo) { | ||
this.foo = foo; | ||
} | ||
} | ||
|
||
class FooFactoryBean implements FactoryBean<Foo> { | ||
|
||
public Foo getObject() { | ||
return new Foo(); | ||
} | ||
|
||
public Class<Foo> getObjectType() { | ||
return Foo.class; | ||
} | ||
|
||
public boolean isSingleton() { | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters