What is a bean in Spring?
sobes.tech AI
Answer from AI
In Spring, a bean is an object managed by the Spring IoC container (Inversion of Control). The container creates, configures, and manages the lifecycle of these objects, allowing dependency injection and application configuration.
Beans are usually declared using annotations (@Component, @Service, @Repository) or in XML/Java configuration. Spring automatically scans and creates bean instances, which can then be used in the application through dependency injection.
Example of bean declaration with annotation:
@Service
public class UserService {
// business logic
}
Spring will create an instance of UserService and inject it where needed, for example, into a controller.
Thus, a bean is a fundamental building block of a Spring application, ensuring modularity and manageability of components.