Skip to content

Automatically detect and register nested @Configuration classes [SPR-8186] #12837

@spring-projects-issues

Description

@spring-projects-issues

Chris Beams opened SPR-8186 and commented

The following is now possible in XML as of Spring 3.1 M1:

<beans>
    <bean id="foo" class="Foo">
        <constructor-arg ref="dataSource"/>
    </bean>
    <beans profile="p1">
        <bean id="dataSource" class="..."/>
    </beans>
    <beans profile="p2">
        <bean id="dataSource" class="..."/>
    </beans>
</beans>

The following is a sketch of the equivalent to the above.

@Configuration
public interface DataSourceConfig {
	@Bean DataSource dataSource();
}

@Configuration
public abstract class Config implements DataSourceConfig {

	@Bean
	public Foo foo() {
		return new Foo(dataSource());
	}

	
	@Profile("p1")
	@Configuration
	static class Profile1Config extends Config {
		@Override @Bean
		public DataSource dataSource() {
			return ...;
		}
	}

	@Profile("p2")
	@Configuration
	static class Profile2Config extends Config {
		@Override @Bean
		public DataSource dataSource() {
			return ...;
		}
	}
}

If nested configuration classes are automatically detected, then bootstrapping is as easy as:

ApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
Foo foo = ctx.getBean(Foo.class);
// ...

If they are not, then one must register (or @Import) Config, Profile1Config, and Profile2Config, either of which is awkward.


Attachments:

Issue Links:

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions