What is @Configuration and @Bean in Spring Boot?

Why Interviewers Ask This

This question tests conceptual clarity. Interviewers want to hear a precise, confident definition before moving to more complex Spring Boot topics. It also reveals how well you can explain technical ideas to non-experts.

Answer

@Configuration marks a class as a source of bean definitions — Java-based configuration replacing XML beans. @Bean marks a method as a bean factory — the returned object is registered as a Spring bean. @Configuration public class AppConfig { @Bean public PasswordEncoder passwordEncoder() { return new BCryptPasswordEncoder(12); } @Bean @Primary public ObjectMapper objectMapper() { return JsonMapper.builder().addModule(new JavaTimeModule()).disable(WRITE_DATES_AS_TIMESTAMPS).build(); } @Bean public RestTemplate restTemplate(RestTemplateBuilder builder) { return builder.setConnectTimeout(Duration.ofSeconds(5)).setReadTimeout(Duration.ofSeconds(10)).build(); } @Bean @ConditionalOnProperty("app.feature.email", havingValue = "true") public EmailService emailService() { return new SmtpEmailService(); } @Bean @Scope("prototype") public JobProcessor jobProcessor() { return new JobProcessor(); // new instance each time } }. Method injection between @Bean methods: @Configuration is CGLib-proxied — calling another @Bean method within the config class returns the Spring-managed singleton, not a new instance: @Bean public ServiceA serviceA() { return new ServiceA(serviceB()); } @Bean public ServiceB serviceB() { return new ServiceB(); }. serviceA and anything else that calls serviceB() gets the same singleton. @Configuration vs @Component: both define beans, but @Configuration is CGLib-proxied for inter-bean calls; @Component is not. Use @Configuration for bean factory classes.

Pro Tip

Demonstrate both theoretical understanding and practical experience. Say what it is, then give an example of how you actually used it in a Spring Boot codebase.