Via Spring <beans> XML

As an alternative to registering @Configuration classes directly against an
AnnotationConfigApplicationContext, @Configuration classes may be
declared as normal <bean> definitions within Spring XML files:

In the example above, <context:annotation-config/> is required in order to
enable ConfigurationClassPostProcessor and other annotation-related
post processors that facilitate handling @Configuration classes.

Via component scanning

@Configuration is meta-annotated with @Component, therefore
@Configuration classes are candidates for component scanning (typically using
Spring XML's <context:component-scan/> element) and therefore may also take
advantage of @Autowired/@Inject
like any regular @Component. In particular, if a single constructor is present
autowiring semantics will be applied transparently:

Properties resolved through the Environment reside in one or more "property
source" objects, and @Configuration classes may contribute property sources to
the Environment object using the @PropertySource
annotation:

This approach is most useful when using Spring's
PropertySourcesPlaceholderConfigurer, usually enabled via XML with
<context:property-placeholder/>. See the section below on composing
@Configuration classes with Spring XML using @ImportResource,
see @Value Javadoc, and see @Bean Javadoc for details on working with
BeanFactoryPostProcessor types such as
PropertySourcesPlaceholderConfigurer.

Composing @Configuration classes

With the @Import annotation

@Configuration classes may be composed using the @Import annotation,
not unlike the way that <import> works in Spring XML. Because
@Configuration objects are managed as Spring beans within the container,
imported configurations may be injected the usual way (e.g. via constructor injection):

With Spring XML using the @ImportResource annotation

As mentioned above, @Configuration classes may be declared as regular Spring
<bean> definitions within Spring XML files. It is also possible to
import Spring XML configuration files into @Configuration classes using
the @ImportResource annotation. Bean definitions imported from
XML can be injected the usual way (e.g. using the Inject annotation):

When bootstrapping such an arrangement, only AppConfig need be registered
against the application context. By virtue of being a nested @Configuration
class, DatabaseConfigwill be registered automatically. This avoids
the need to use an @Import annotation when the relationship between
AppConfigDatabaseConfig is already implicitly clear.

Note also that nested @Configuration classes can be used to good effect
with the @Profile annotation to provide two options of the same bean to the
enclosing @Configuration class.

Configuring lazy initialization

By default, @Bean methods will be eagerly instantiated at container
bootstrap time. To avoid this, @Configuration may be used in conjunction with
the @Lazy annotation to indicate that all @Bean methods declared within
the class are by default lazily initialized. Note that @Lazy may be used on
individual @Bean methods as well.

Testing support for @Configuration classes

The Spring TestContext framework available in the spring-test module
provides the @ContextConfiguration annotation, which as of Spring 3.1 can
accept an array of @ConfigurationClass objects:

Element Detail

value

Explicitly specify the name of the Spring bean definition associated
with this Configuration class. If left unspecified (the common case),
a bean name will be automatically generated.

The custom name applies only if the Configuration class is picked up via
component scanning or supplied directly to a AnnotationConfigApplicationContext.
If the Configuration class is registered as a traditional XML bean definition,
the name/id of the bean element will take precedence.