In Spring, the @Configuration annotation declares that a class defines beans through Java code. Its @Bean methods are called by the container to create, configure and register objects, giving you a type-safe, refactorable alternative to XML configuration.
Spring Boot applications are built almost entirely on this mechanism: @SpringBootApplication includes @SpringBootConfiguration, which is a specialised @Configuration.
When the application context starts, it scans for classes that carry @Configuration (it is meta-annotated with @Component), parses their annotations, and registers a bean definition for each @Bean method. The beans are then created, wired and managed like any other.
What the annotation gives you compared with XML:
@Profile and @ConditionalOnPropertyThe bean name defaults to the method name (userRepository, userService). Override it with @Bean("customName"). Parameters of a @Bean method are injected from the container, so userService receives the repository bean.
Beans are singletons unless you say otherwise.
Spring infers a public close() or shutdown() method as the destroy callback by default for @Bean methods. Use @PostConstruct and @PreDestroy on the bean class itself where you control it (these are jakarta.annotation annotations in Spring 6 and later).
Use @Value for a single value, or bind a group with @ConfigurationProperties.
${mail.port:25} uses 25 when the property is not defined. For more than a few values, a @ConfigurationProperties class is easier to maintain and validate.
@Import adds specific configuration classes.@ComponentScan registers annotated components from a package.@ImportResource("classpath:legacy-beans.xml") mixes in old XML definitions.@Profile("dev") limits a class or bean to a profile.By default (proxyBeanMethods = true) Spring subclasses the configuration with CGLIB so that calling one @Bean method from another returns the existing singleton.
Set @Configuration(proxyBeanMethods = false) when beans are connected through method parameters. It avoids the proxy, speeds up startup and suits native images. In that mode pool() called directly would create a new object.
In Spring Boot you do not do this yourself: SpringApplication.run starts the context and component scanning discovers your @Configuration classes.
Both register a class as a bean, but @Configuration also enables CGLIB proxying of @Bean methods (unless disabled) so inter-bean calls return singletons. A @Component with @Bean methods works in lite mode.
Yes, and large applications should. Split by concern (web, persistence, security) and combine them through component scanning or @Import.
Yes, by default each @Bean method produces one singleton per application context. Add @Scope for prototype or request scope.
No. Java configuration covers everything XML did. Use @ImportResource only to reuse existing XML files.
@Configuration marks a class as a Java-based source of bean definitions. Declare beans with @Bean methods, inject collaborators through parameters, organise code with @Import and @ComponentScan, and turn off proxying with proxyBeanMethods = false when you do not need inter-bean calls.