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
This commit is contained in:
Chris Beams
2011-05-21 01:20:33 +00:00
parent 5b2c7c4e58
commit 95b1dbadb0
4 changed files with 206 additions and 3 deletions

View File

@@ -232,6 +232,37 @@ import org.springframework.stereotype.Component;
* }
* }</pre>
*
* <h3>With nested {@code @Configuration} classes</h3>
* {@code @Configuration} classes may be nested within one another as follows:
* <pre class="code">
* &#064;Configuration
* public class AppConfig {
* &#064;Inject DataSource dataSource;
*
* &#064;Bean
* public MyBean myBean() {
* return new MyBean(dataSource);
* }
*
* &#064;Configuration
* static class DatabaseConfig {
* &#064;Bean
* DataSource dataSource() {
* return new EmbeddedDatabaseBuilder().build();
* }
* }
* }</pre>
*
* When bootstrapping such an arrangement, only {@code AppConfig} need be registered
* against the application context. By virtue of being a nested {@code @Configuration}
* class, {@code DatabaseConfig} <em>will be registered automatically</em>. This avoids
* the need to use an {@code @Import} annotation when the relationship between
* {@code AppConfig} {@code DatabaseConfig} is already implicitly clear.
*
* <p>Note also that nested {@code @Configuration} classes can be used to good effect
* with the {@code @Profile} annotation to provide two options of the same bean to the
* enclosing {@code @Configuration} class.
*
* <h2>Configuring lazy initialization</h2>
* <p>By default, {@code @Bean} methods will be <em>eagerly instantiated</em> at container
* bootstrap time. To avoid this, {@code @Configuration} may be used in conjunction with

View File

@@ -16,6 +16,8 @@
package org.springframework.context.annotation;
import static org.springframework.context.annotation.ConfigurationClassUtils.isConfigurationCandidate;
import java.io.IOException;
import java.util.Collections;
import java.util.Comparator;
@@ -157,6 +159,17 @@ class ConfigurationClassParser {
}
protected void doProcessConfigurationClass(ConfigurationClass configClass, AnnotationMetadata metadata) throws IOException {
// recursively process any member (nested) classes first
for (String memberClassName : metadata.getMemberClassNames()) {
MetadataReader reader = this.metadataReaderFactory.getMetadataReader(memberClassName);
AnnotationMetadata memberClassMetadata = reader.getAnnotationMetadata();
if (isConfigurationCandidate(memberClassMetadata)) {
processConfigurationClass(new ConfigurationClass(reader, null));
}
}
// process any @PropertySource annotations
Map<String, Object> propertySourceAttributes =
metadata.getAnnotationAttributes(org.springframework.context.annotation.PropertySource.class.getName());
if (propertySourceAttributes != null) {
@@ -169,6 +182,7 @@ class ConfigurationClassParser {
this.propertySources.push(ps);
}
// process any @ComponentScan annotions
Map<String, Object> componentScanAttributes = metadata.getAnnotationAttributes(ComponentScan.class.getName());
if (componentScanAttributes != null) {
// the config class is annotated with @ComponentScan -> perform the scan immediately
@@ -188,12 +202,14 @@ class ConfigurationClassParser {
}
}
// process any @Import annotations
List<Map<String, Object>> allImportAttribs =
AnnotationUtils.findAllAnnotationAttributes(Import.class, metadata.getClassName(), true);
for (Map<String, Object> importAttribs : allImportAttribs) {
processImport(configClass, (String[]) importAttribs.get("value"), true);
}
// process any @ImportResource annotations
if (metadata.isAnnotated(ImportResource.class.getName())) {
String[] resources = (String[]) metadata.getAnnotationAttributes(ImportResource.class.getName()).get("value");
Class<?> readerClass = (Class<?>) metadata.getAnnotationAttributes(ImportResource.class.getName()).get("reader");
@@ -205,6 +221,8 @@ class ConfigurationClassParser {
configClass.addImportedResource(resource, readerClass);
}
}
// process individual @Bean methods
Set<MethodMetadata> beanMethods = metadata.getAnnotatedMethods(Bean.class.getName());
for (MethodMetadata methodMetadata : beanMethods) {
configClass.addBeanMethod(new BeanMethod(methodMetadata, configClass));

View File

@@ -79,12 +79,11 @@ abstract class ConfigurationClassUtils {
}
if (metadata != null) {
if (metadata.isAnnotated(Configuration.class.getName())) {
if (isFullConfigurationCandidate(metadata)) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
return true;
}
else if (metadata.isAnnotated(Component.class.getName()) ||
metadata.hasAnnotatedMethods(Bean.class.getName())) {
else if (isLiteConfigurationCandidate(metadata)) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
return true;
}
@@ -92,6 +91,20 @@ abstract class ConfigurationClassUtils {
return false;
}
public static boolean isConfigurationCandidate(AnnotationMetadata metadata) {
return isFullConfigurationCandidate(metadata) || isLiteConfigurationCandidate(metadata);
}
public static boolean isFullConfigurationCandidate(AnnotationMetadata metadata) {
return metadata.isAnnotated(Configuration.class.getName());
}
public static boolean isLiteConfigurationCandidate(AnnotationMetadata metadata) {
return metadata.isAnnotated(Component.class.getName()) ||
metadata.hasAnnotatedMethods(Bean.class.getName());
}
/**
* Determine whether the given bean definition indicates a full @Configuration class.
*/