1) @Configuration
In Spring Boot (and Spring Framework), we use the @Configuration annotation to indicate that a Java class contains Spring bean definitions — essentially replacing the older applicationContext.xml style configuration.
✅ What is @Configuration?
@Configuration is an annotation provided by Spring that marks a class as a source of bean definitions. It is part of Java-based configuration introduced in Spring 3.
✅ Why We Use @Configuration in Spring Boot
| Purpose | Description |
|---|---|
| 1. Java-based configuration | Allows you to define beans using Java code instead of XML. |
2. Enables @Bean methods | Methods annotated with @Bean inside a @Configuration class are registered as beans in the Spring context. |
| 3. Singleton behavior | Spring ensures that each @Bean method returns a singleton bean by default — even if the method is called multiple times. |
| 4. Component scanning | It works with @ComponentScan and @EnableAutoConfiguration (often combined in @SpringBootApplication). |
| 5. Modularity | Helps you modularize configuration by splitting it into multiple @Configuration classes. |
🔁 Compared with @Component
-
@Componenttells Spring "this is a bean". -
@Configurationtells Spring "this class defines multiple beans", and provides enhanced control over bean creation using@Bean.
✅ Behind the scenes
@Configuration is internally a specialized @Component, so Spring can automatically detect it via component scanning.
🧠 Bonus Tip: proxyBeanMethods = false (Spring 5.2+)
-
Use this if you don't need Spring to proxy
@Beanmethods. -
Improves performance, but disables method call interception between beans in the same config class.
✅ Summary
| Feature | @Configuration |
|---|---|
| Replaces | XML config (applicationContext.xml) |
| Supports | Defining beans using @Bean methods |
| Registered as | A Spring-managed component |
| Ensures | Singleton beans, method call safety |
| Used with | @ComponentScan, @SpringBootApplication, etc. |
2) @EnableAutoConfiguration
In Spring Boot, we use @EnableAutoConfiguration to automatically configure Spring application context based on the classpath contents, defined beans, and configuration properties — removing the need for most manual bean declarations.
✅ What is @EnableAutoConfiguration?
@EnableAutoConfiguration tells Spring Boot:
"Try to automatically configure beans and settings based on what’s available on the classpath and what the developer has configured."
✅ Why We Use @EnableAutoConfiguration
| Purpose | Description |
|---|---|
| ✅ Auto configuration | Automatically configures beans for components like DataSource, JPA, Kafka, RabbitMQ, Redis, Web, etc., if relevant classes are on the classpath. |
| ✅ Eliminates boilerplate | Removes the need for manual configuration (e.g., no need to define DataSource bean for Spring JDBC). |
| ✅ Conditional configuration | Uses @ConditionalOnClass, @ConditionalOnMissingBean, etc., to apply configuration only when needed. |
✅ Part of @SpringBootApplication | It is implicitly included in @SpringBootApplication, so most of the time you don’t need to add it manually. |
✅ Example
Equivalent breakdown:
✅ Real-world Usage Example
If you include spring-boot-starter-web, Spring Boot sees:
-
DispatcherServleton the classpath -
So it auto-configures:
-
A
DispatcherServletbean -
A
RequestMappingHandlerMapping -
JSON converters
-
Embedded Tomcat server
-
If you include spring-boot-starter-data-jpa, it auto-configures:
-
DataSource -
EntityManagerFactory -
TransactionManager
Without writing any XML or Java configuration.
✅ How It Works Internally
-
It loads configuration classes from:
Which looks like this:
-
These classes are conditionally applied using
@ConditionalOnClass,@ConditionalOnMissingBean, etc.
✅ Disable Specific Auto Configs
If you don’t want Spring Boot to auto-configure something:
✅ Summary
| Feature | Description |
|---|---|
| Purpose | Automatically configures Spring beans based on classpath, properties, and environment |
| Included in | @SpringBootApplication |
| Replaces | Manual bean definitions |
| Controlled by | Conditions (e.g., class presence, bean presence) |
| Config loaded from | spring.factories under META-INF |
| Can be disabled | Yes, via exclude or spring.autoconfigure.exclude |