Bean
In Spring, a Bean is an object that is instantiated, assembled, and managed by the Spring IoC container. The IoC container is responsible for creating and managing the lifecycle of these Beans. Some key characteristics of Beans in Spring:
-
Instantiation: The IoC container is responsible for instantiating Beans. It creates the objects based on the configuration, which can be XML, Java annotations, or a combination of both.
-
Dependency Injection: The IoC container is also responsible for injecting the necessary dependencies into a Bean. This is the “Inversion of Control” part, where the container manages the dependencies instead of the Bean itself.
-
Lifecycle Management: The IoC container manages the entire lifecycle of a Bean, from creation to destruction. It can perform tasks like initializing and destroying Beans as needed.
-
Scope: Beans can have different scopes, such as singleton (one instance per Spring IoC container), prototype (a new instance for each request), request, session, and application. The scope determines how the IoC container manages the Bean instances.
-
Configuration: Beans are configured either through XML configuration files or Java-based configuration using annotations like
@Configuration
and@Bean
.
Beans are the fundamental building blocks of any Spring application. They encapsulate the business logic and are wired together by the IoC container to create a complete application. The container’s ability to manage Beans and their dependencies is a core part of the Spring framework’s architecture and functionality.