2. Authoring @Configuration classes

The central artifact in Spring JavaConfig is the @Configuration-annotated class. These classes consist principally of @Bean-annotated methods that define instantiation, configuration, and initialization logic for objects that will be managed by the Spring IoC container.

2.1. @Configuration

Annotating a class with the @Configuration indicates that the class may be used by JavaConfig as a source of bean definitions. The simplest possible @Configuration class would read as follows:

@Configuration
public class ApplicationConfig {
}
            

An application may make use of just one @Configuration-annotated class, or many. @Configuration can be considered the equivalent of XML's <beans/> element. Like <beans/>, it provides an opportunity to explicitly set defaults for all enclosed bean definitions.

@Configuration(defaultAutowire = Autowire.BY_TYPE, defaultLazy = Lazy.FALSE)
public class ApplicationConfig {
    // bean definitions follow
}
            

Because the semantics of the attributes to the @Configuration annotation are 1:1 with the attributes to the <beans/> element, this documentation defers to the beans-definition section of Chapter 3, IoC from the Core Spring documentation. See also the Javadoc for @Configuration for details on each of the available annotation attributes.

[Tip]Tip
Jump to Section 3.1, “ Bootstrapping applications with JavaConfigApplicationContext ” to see how @Configuration classes are used to create a Spring Application Context.