The @Configuration annotation marks a class as a source of bean definitions for the Spring container. Methods inside it annotated with @Bean create objects that Spring manages, which is the Java-based replacement for XML bean configuration. Spring also processes a @Configuration class specially so that calls between its @Bean methods return the shared singleton instead of creating duplicates.
It is the foundation of Spring Boot, where @SpringBootApplication itself includes @Configuration through @SpringBootConfiguration.
A class annotated with @Configuration tells the ApplicationContext that it declares beans. At startup the container:
@Bean method and registers a bean definition named after the method.The annotation is meta-annotated with @Component, so configuration classes are themselves beans and can receive dependency injection, for example through constructor parameters or @Value.
Starting the context and using the bean:
The greetingService method asks for a Clock parameter, and Spring supplies the bean defined by clock(). Parameter injection like this is the recommended way to wire beans together.
Spring distinguishes two modes, and it matters for inter-bean method calls.
| Mode | Declared with | Behaviour of @Bean method calls |
|---|---|---|
| Full | @Configuration (default proxyBeanMethods = true) | The class is subclassed with CGLIB, so calling clock() from another @Bean method returns the singleton |
| Lite | @Configuration(proxyBeanMethods = false), @Component, or plain classes with @Bean methods | Calls are ordinary Java calls and create a new object each time |
If you set proxyBeanMethods = false, use parameter injection (report(Formatter formatter)) instead of calling formatter(). Spring Boot's own auto-configuration classes use proxyBeanMethods = false because it starts faster and the classes do not need the proxy. Because full mode relies on subclassing, a @Configuration class cannot be final unless proxying is disabled.
Large applications split configuration into focused classes and combine them:
@Import(OtherConfig.class) pulls another configuration class in explicitly.@ComponentScan("com.example") finds @Component, @Service and @Configuration classes.@PropertySource("classpath:app.properties") loads a properties file for the Environment.@Profile("prod") activates a class or a bean only for certain profiles.@ConditionalOnProperty and the other @Conditional variants register beans only when a condition holds (the Boot ones are in org.springframework.boot.autoconfigure.condition).@EnableXxx annotations such as @EnableScheduling or @EnableCaching are usually placed on configuration classes.Java configuration is type checked by the compiler, refactorable in an IDE, searchable, and can contain real logic such as loops or conditionals. XML is still supported, but new Spring and Spring Boot projects use annotations and Java configuration almost exclusively. Externalised values come from application.properties or YAML through @Value or @ConfigurationProperties, not from XML.
Both register a class as a bean. @Configuration additionally enables CGLIB proxying of @Bean methods by default, so calls between them return singletons. With @Component, @Bean methods run in lite mode.
Yes for your own bean definitions. You do not need it on the main class for bean methods, though @SpringBootApplication already includes it, so @Bean methods can live there. Separate classes are cleaner.
It disables the CGLIB subclass, which saves startup time and memory. You then must not rely on direct calls between @Bean methods for singleton behaviour.
Yes. It is a normal bean, so constructor injection and @Value work. Prefer constructor or @Bean method parameters.
@Configuration declares a class as a provider of beans through @Bean methods and enables Java-based configuration in place of XML. By default Spring proxies the class so inter-bean calls return singletons, and you can turn that off with proxyBeanMethods = false for faster startup when you inject dependencies through parameters.