Swiftorial Logo
Home
Swift Lessons
AI Tools
Learn More
Career
Resources

Spring Framework FAQ: Top Questions

14. What is the role of @Configuration and @Bean annotations?

@Configuration indicates that a class declares one or more @Bean methods. These methods are used to define beans for the Spring context programmatically.

📘 Behavior:

  • @Configuration classes are processed like XML config files.
  • @Bean methods are invoked by the container, not by user code.

📥 Example:

@Configuration
public class AppConfig {

  @Bean
  public MyService myService() {
    return new MyServiceImpl();
  }
}

🏆 Expected Output:

Spring registers MyServiceImpl as a bean in the application context.

🛠️ Use Cases:

  • Programmatic configuration for complex setups.
  • Combining with conditional logic or dynamic creation.