@Configuration and @Bean are Spring annotations used for Java-based configuration. They allow developers to define and manage Spring Beans without using XML configuration files.
A configuration class can define a DataSource or Service object using @Bean, and Spring will automatically create and manage that object throughout the application lifecycle.
@Configuration
public class AppConfig {
@Bean
public UserService userService() {
return new UserService();
}
}A concise interview answer is
@Configuration marks a class as a Spring configuration class, while @Bean is used to define objects that should be managed by the Spring IoC container. Spring creates and manages these beans and makes them available for dependency injection.
Last updated: 16 July 2026
Previous question: What are the types of IoC containers in Spring?
Next question: What is the best way to inject beans in Spring, and why?