Skip to content

Doc: @Bean methods on @Configuration class returned from another @Bean method do not work [SPR-14602] #19171

Closed
@spring-projects-issues

Description

@spring-projects-issues

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 its BeanDefinition#getBeanClassName is null, so ConfigurationClassUtils#checkConfigurationClassCandidate returns false.

@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 Config {
        @Bean
        public InnerConfig innerConfig() {
            return new InnerConfig();
        }
    }

    @Autowired
    URI someUri;

    @Test
    public void testUri() {
        assertNotNull(someUri);
    }
}

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:

Referenced from: commits 9b221f5, 6fe7e56

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: taskA general task

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions