Category: java
Spring Boot, a Bean
Published on 26 May 2026
Explanation
In Spring Boot, a Bean is
an object managed by the Spring
IoC (Inversion of Control) container.
Code:
@Component
public class UserService {
}
Explanation
Singleton Bean: Only one instance is
created for the entire application. This
is the default bean scope in
Spring.
Code:
@Component
@Scope("singleton")
public class UserService {
}
Explanation
Prototype Bean: A new object is
created every time the bean is
requested from the Spring container.
Code:
@Component
@Scope("prototype")
public class UserService {
}
Explanation
Request Bean: A new bean instance
is created for every HTTP request
in a web application.
Code:
@Component
@Scope(value =
WebApplicationContext.SCOPE_REQUEST)
public class RequestBean {
}
Explanation
Beans can also be created using
stereotypes and @Bean annotation
inside configuration
classes.
Code:
@Service
public class OrderService {
}
@Configuration
public class AppConfig {
@Bean
public UserService userService() {
return new UserService();
}
}