-
Notifications
You must be signed in to change notification settings - Fork 38.8k
Closed
Labels
in: coreIssues in core modules (aop, beans, core, context, expression)Issues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancementA general enhancement
Milestone
Description
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:
- mylyn-context.zip (433.80 kB)
Issue Links:
- Improve support for @Configuration class hierarchies [SPR-8187] #12838 Improve support for
@Configuration
class hierarchies ("depends on") - Support introspection of nested classes with ClassMetadata [SPR-8358] #13005 Support introspection of nested classes with ClassMetadata ("depends on")
- Automatically detect and register static nested @FeatureConfiguration classes [SPR-8034] #12689 Automatically detect and register static nested
@FeatureConfiguration
classes - Revisit @Bean introspection between @Configuration classes and 'lite' beans [SPR-17206] #21739 Revisit
@Bean
introspection between@Configuration
classes and 'lite' beans
Metadata
Metadata
Assignees
Labels
in: coreIssues in core modules (aop, beans, core, context, expression)Issues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancementA general enhancement