The @Configuration annotation marks a class as a source of bean definitions for the Spring container. Methods inside it annotated with @Bean create and return objects that Spring then manages, which is the Java-based alternative to XML configuration.
It is also meta-annotated with @Component, so configuration classes are picked up by component scanning. In Spring Boot you use it for third-party beans, custom wiring and anything that needs construction logic.
A class annotated with @Configuration is processed by the container at startup. Every @Bean method is invoked once (for singleton scope) and its return value is registered under the method name.
The second method receives the Clock bean as a parameter, so Spring injects it. That is the recommended way to express dependencies between beans.
Outside Spring Boot you create the context explicitly. Inside Spring Boot, @SpringBootApplication is itself a @Configuration.
By default Spring wraps a @Configuration class in a CGLIB subclass. This guarantees that calling one @Bean method from another returns the same singleton instead of creating a new object.
If your bean methods do not call each other, set @Configuration(proxyBeanMethods = false). This skips proxy generation and speeds up startup, and Spring Boot's own auto-configuration classes use it. A class annotated with @Component and @Bean methods (lite mode) never gets this inter-method guarantee.
| Annotation | Purpose |
|---|---|
@Bean | Declares a bean from a method |
@ComponentScan | Scans packages for stereotype classes |
@Import | Pulls in other configuration classes |
@PropertySource | Loads a properties file into the environment |
@Profile | Activates a configuration only for named profiles |
@ConditionalOnProperty | Spring Boot condition on a property value |
Use @Configuration when you must build beans programmatically: classes from libraries you cannot annotate, objects that need conditional setup, or several beans that share construction logic. Use @Component or its specializations for your own classes that Spring can instantiate directly.
You can load only the configuration you need in a unit test, which keeps the context small and fast. Pass the class to @SpringJUnitConfig or @ContextConfiguration, or use @TestConfiguration to add test-only beans on top of the application's own.
A nested static class annotated with @TestConfiguration is detected automatically in Spring Boot tests and does not replace your real configuration.
Not strictly, but @Configuration gives you the full-mode guarantees with singleton semantics between bean methods. Without it, @Bean methods run in lite mode.
Yes. Since Spring 4.3 a configuration class with a single constructor receives its dependencies without @Autowired.
Spring does not create a CGLIB proxy, so direct calls between @Bean methods produce new instances. Pass dependencies as method parameters instead.
Yes. It covers everything XML did, and you can still mix both with @ImportResource.
@Configuration declares a class that defines beans via @Bean methods. Spring processes it at startup, registers the beans and keeps singletons consistent. Prefer parameter injection between bean methods, and use proxyBeanMethods = false when beans do not call each other.