@Configuration and @Bean help you tell Spring how to create and manage objects your app needs. This makes your app organized and easy to change.
When you want to create and manage your own objects (beans) in Spring.
When you need to configure objects with special settings before using them.
When you want Spring to automatically provide objects to other parts of your app.
When you want to replace or customize default Spring beans with your own versions.
Syntax
Spring Boot
@Configuration
public class AppConfig {
@Bean
public MyService myService() {
return new MyService();
}
}
@Configuration marks a class as a source of bean definitions.
@Bean marks a method that returns an object Spring will manage.
Examples
This example creates a simple String bean named 'greeting'.
Spring Boot
@Configuration
public class AppConfig {
@Bean
public String greeting() {
return"Hello, Spring!";
}
}
This example shows a bean with a constructor argument for custom setup.
Spring Boot
@Configuration
public class AppConfig {
@Bean
public Service service() {
return new Service("custom config");
}
}
Sample Program
This example shows a configuration class that creates a MessageService bean. Spring will manage this bean and provide it when needed.
Spring Boot
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
@Configuration
public class AppConfig {
@Bean
public MessageService messageService() {
return new MessageService();
}
}
class MessageService {
public String getMessage() {
return"Hello from MessageService!";
}
}
// In a Spring Boot app, you can get the bean like this:
// ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
// MessageService service = context.getBean(MessageService.class);
// System.out.println(service.getMessage());
OutputSuccess
Important Notes
Each @Bean method creates one object managed by Spring.
@Configuration classes are like blueprints for your app's objects.
Spring automatically calls these methods and keeps the objects ready to use.
Summary
@Configuration marks a class as a place to define beans.
@Bean marks methods that create objects Spring manages.
This helps keep your app organized and flexible.
Practice
(1/5)
1. What is the main purpose of the @Configuration annotation in Spring Boot?
easy
A. To enable automatic component scanning
B. To mark a method that returns a bean instance
C. To mark a class as a source of bean definitions for the application context
D. To configure database connections automatically
Solution
Step 1: Understand the role of @Configuration
@Configuration tells Spring that this class contains methods to create beans.
Step 2: Differentiate from other annotations
@Bean marks methods, not classes; component scanning is done by @ComponentScan; database config is separate.
Final Answer:
To mark a class as a source of bean definitions for the application context -> Option C
Quick Check:
@Configuration = class for beans [OK]
Hint: Remember: @Configuration is for classes, @Bean is for methods [OK]
Common Mistakes:
Confusing @Configuration with @Bean
Thinking @Configuration enables scanning
Assuming it configures databases automatically
2. Which of the following is the correct way to define a bean method inside a @Configuration class?
easy
A. @Bean public MyBean myBean() { return new MyBean(); }
B. @Configuration public MyBean myBean() { return new MyBean(); }
C. public void createBean() { return new MyBean(); }
D. @Component public MyBean myBean() { return new MyBean(); }
Solution
Step 1: Check method signature for bean creation
Bean methods must be annotated with @Bean and return the bean object.
Step 2: Verify syntax correctness
@Bean public MyBean myBean() { return new MyBean(); } correctly uses @Bean and returns a new instance.
Final Answer:
@Bean public MyBean myBean() { return new MyBean(); } -> Option A
Hint: Bean methods need @Bean and must return the object [OK]
Common Mistakes:
Missing @Bean annotation
Using void return type
Using @Configuration on methods
3. Given the following code, what will be the output when retrieving the bean named "service" from the Spring context?
@Configuration
public class AppConfig {
@Bean
public String service() {
return "MyService";
}
}
medium
A. "service"
B. "MyService"
C. An exception is thrown
D. null
Solution
Step 1: Identify the bean method and its return value
The method named service returns the string "MyService" and is annotated with @Bean.
Step 2: Understand bean retrieval by name
Spring registers this bean with the name "service" and returns the method's return value when requested.
Final Answer:
"MyService" -> Option B
Quick Check:
Bean name returns method's return value [OK]
Hint: Bean name matches method name; returns method's return value [OK]
Common Mistakes:
Expecting bean name as output instead of return value
Thinking Spring returns null if no explicit name
Assuming exception without error in code
4. Identify the error in the following Spring configuration code:
@Configuration
public class Config {
@Bean
public void createBean() {
// no return statement
}
}
medium
A. Method must return an object, not void
B. Missing @Configuration annotation
C. Method name cannot be createBean
D. No error, code is correct
Solution
Step 1: Check method return type for @Bean
Bean methods must return the bean object; void return type is invalid.
Step 2: Confirm annotations and method name
@Configuration is present; method name can be anything; error is only return type.
Final Answer:
Method must return an object, not void -> Option A
Quick Check:
@Bean method needs return value, not void [OK]
Hint: Bean methods must return an object, never void [OK]
Common Mistakes:
Using void return type for @Bean methods
Confusing method name restrictions
Missing @Configuration annotation (not in this case)
5. You want to create two beans of the same type but with different configurations using @Bean methods inside a @Configuration class. How can you ensure Spring distinguishes between them when injecting?
hard
A. Define both beans in separate @Configuration classes only
B. Use @Primary on both methods
C. Use the same method name but different return types
D. Use different method names for each @Bean method
Solution
Step 1: Understand bean naming in Spring
By default, Spring uses the @Bean method name as the bean name to distinguish beans.
Step 2: Differentiate beans of same type
Using different method names creates distinct bean names, allowing injection by name or qualifier.
Final Answer:
Use different method names for each @Bean method -> Option D
Quick Check:
Different method names = different bean names [OK]
Hint: Different method names create distinct bean names automatically [OK]
Common Mistakes:
Using @Primary on both beans (conflicts)
Same method name with different return types (not allowed)
Thinking beans must be in separate classes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.