-
Notifications
You must be signed in to change notification settings - Fork 38.5k
Doc: @Bean methods on @Configuration class returned from another @Bean method do not work [SPR-14602] #19171
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Stéphane Nicoll commented I am confused: why are you creating A @RunWith(SpringRunner.class)
@ContextConfiguration(classes=BeanDefinitionAutowireTest.Config.class)
public class BeanDefinitionAutowireTest {
@Configuration
public static class InnerConfig {
@Bean
URI someUri() {
return URI.create("/");
}
}
@Configuration
public static class AnotherConfig {
private final InnerConfig innerConfig;
@Autowired
public AnotherConfig(InnerConfig innerConfig) {
this.innerConfig = innerConfig;
System.out.println("URI --> " + innerConfig.someUri());
}
}
@Configuration
@Import({InnerConfig.class, AnotherConfig.class})
public static class Config {
}
@Autowired
URI someUri;
@Test
public void testUri() {
assertNotNull(someUri);
}
} Regardless of this, the behaviour of the container in such a (wrong) scenario can be improved, what do you think Juergen Hoeller? |
Steven Schlansker commented Hi, thanks a lot for looking at this -- the bit that I am missing is, how do I pass constructor arguments to the |
Juergen Hoeller commented We'll have to push this back for consideration in 5.0 M3 since the impact on the underlying infrastructure is non-trivial: For example, we can't dynamically create an enhanced subclass for such a configuration class (since the factory method instantiates it), and we generally have to introspect factory method return signatures to find out whether they're configuration candidates. |
Juergen Hoeller commented I'm afraid we can't reliably detect such unsupported Actually supporting such configuration classes returned from factory methods on other configuration classes incurs even more trouble, including the aforementioned lack of runtime enhancement of classes. The configuration class model is really designed for the framework to instantiate such classes, not for the instances to be provided through factory methods. Tools or IDEs may flag such invalid use of annotations, but from the core framework perspective, I'm turning this into a documentation task. |
Steven Schlansker opened SPR-14602 and commented
I have an
@Configuration
class which holds some internal configuration data. It wants to instantiate a collaborating@Configuration
class, hand off some data to it, and expects its collaborator to exist. However, it seems that an@Bean
method that returns a@Configuration
class fails because itsBeanDefinition#getBeanClassName
is null, soConfigurationClassUtils#checkConfigurationClassCandidate
returns false.I feel that this should work. If for some reason it can not, it should throw an exception or otherwise fail obviously, rather than silently discarding the definitions you expect.
(Note that this test case is a little simplified, in the real case I pass constructor arguments to
InnerConfig
, so making things static is not an option)Affects: 4.3.1
Issue Links:
@Configuration
imported via ImportBeanDefinitionRegistrar is not processed as configuration@Configuration
imported via@ImportResource
is not processedReferenced from: commits 9b221f5, 6fe7e56
The text was updated successfully, but these errors were encountered: