Skip to content

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

Closed

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:

Activity

  1. spring-projects-issues commented on May 20, 2011

    @spring-projects-issues
    CollaboratorAuthor

    Chris Beams commented

    Author: Chris Beams <[email protected]>
    Date:   Fri May 20 13:08:29 2011 +0800
    
        Register nested @Configuration classes automatically
        
        The following is now possible:
        
        @Configuration
        public class AppConfig {
            @Inject DataSource dataSource;
        
            @Bean
            public MyBean myBean() {
                return new MyBean(dataSource);
            }
        
            @Configuration
            static class DatabaseConfig {
                @Bean
                DataSource dataSource() {
                    return new EmbeddedDatabaseBuilder().build();
                }
            }
        }
        
        public static void main(String... args) {
            AnnotationConfigApplicationContext ctx =
                new AnnotationConfigApplicationContext(AppConfig.class);
            ctx.getBean(MyBean.class);     // works
            ctx.getBean(DataSource.class); // works
        }
        
        Notice that the @Import annotation was not used and that only AppConfig
        was registered against the context. By virtue of the fact that
        DatabaseConfig is a member class of AppConfig, it is automatically
        registered when AppConfig is registered. This avoids an awkward and
        redundant @Import annotation when the relationship is already implicitly
        clear.
        
        See @Configuration Javadoc for details.
        
        Issue: SPR-8186
    
  2. added
    type: enhancement
    in: core
    on Jan 11, 2019
  3. added this to the 3.1 M2 milestone on Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    in: coretype: enhancement

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions