What is the use of @Configuration and @Bean annotations in Spring?

@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.

Key Points

Example

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();
    }
}

Interview Tip

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?

More Spring Framework - Level I interview questions

All Spring Framework - Level I questions