The @Configuration annotation marks a class as a source of bean definitions for the Spring container. Methods annotated with @Bean inside it return objects that Spring registers, manages and injects, which replaces the old XML configuration files with type-safe Java code.
A @Configuration class is also processed specially: by default Spring proxies it so that calls between @Bean methods return the same singleton instead of creating new objects each time.
A class annotated with @Configuration is a configuration class. Spring reads it at startup, calls its @Bean methods, and stores the results in the application context. The annotation is meta-annotated with @Component, so it is picked up by component scanning, which is why @SpringBootApplication finds your configuration classes automatically.
priceCalculator receives the Clock bean as a method parameter. Spring resolves parameters of @Bean methods from the container, so this is ordinary dependency injection without @Autowired.
By default every @Bean method defines a singleton. In a full @Configuration class (the default), Spring generates a CGLIB subclass so that a call from one @Bean method to another is intercepted and returns the existing bean.
Both repositories share one DataSource. Without the proxy, dataSource() would create a new object on every call.
You can switch the proxying off with proxyBeanMethods = false.
| Mode | Declaration | Inter-bean method calls | Startup cost |
|---|---|---|---|
| Full | @Configuration (default) | Intercepted, singleton preserved | CGLIB proxy created |
| Lite | @Configuration(proxyBeanMethods = false) | Plain Java calls, new object each time | No proxy, faster, GraalVM friendly |
Use lite mode when your @Bean methods get their collaborators as parameters rather than by calling each other. Spring Boot's own auto-configuration classes use it. A class with @Bean methods but no @Configuration (for example a plain @Component) also runs in lite mode, and @Bean methods there are not proxied.
Note that @Configuration classes cannot be final, and @Bean methods cannot be private or final, in full mode, because CGLIB must subclass them.
In a Spring Boot application the class is detected automatically. In plain Spring you pass it to an AnnotationConfigApplicationContext.
Large applications split configuration into focused classes and combine them.
@Import(OtherConfig.class) pulls in another configuration class explicitly.@ComponentScan registers @Component classes from a package.@PropertySource("classpath:app.properties") adds a properties file to the Environment.@Profile("prod") or @ConditionalOnProperty on a class or @Bean method makes it conditional.@EnableConfigurationProperties or @ConfigurationPropertiesScan binds typed properties classes.| Approach | Best for |
|---|---|
@Component, @Service, @Repository | Your own classes that you can annotate |
@Configuration with @Bean | Third-party classes, objects needing construction logic, or several variants of one type |
You cannot add @Component to a library class such as a DataSource or ObjectMapper, so you declare it in a @Bean method instead.
No, @Bean also works in @Component classes, but then it runs in lite mode with no inter-bean call interception. Use @Configuration when @Bean methods call each other, or just inject collaborators through parameters.
It controls whether Spring creates a CGLIB proxy of the configuration class. With true (default) calls between @Bean methods return singletons. With false startup is faster and memory use lower, but those calls become plain Java calls.
Yes. Java configuration with @Configuration and @Bean is the standard approach, and XML is only kept for legacy projects. You can still mix them with @ImportResource.
Yes. Since Spring 4.3 a single constructor is autowired without @Autowired, so you can inject other beans or an Environment through the constructor.
@Configuration turns a class into a source of bean definitions. Its @Bean methods produce container-managed objects, and the default proxying keeps singletons consistent across method calls. Use proxyBeanMethods = false when beans are wired by parameters, combine classes with @Import, and keep @Component for your own classes.