Spring and Bean

Didem AĞDOĞAN
2 min readDec 7, 2024

--

When talking about Spring, the most frequently used term is, of course, “Bean.” So, let’s dive into the details of beans.

Bean

Beans are objects managed by the Spring IoC container. They can be considered reusable components. By default, every bean is a singleton, meaning it is created only once.

Bean Management

Bean management involves processes such as the creation of beans dependency Injection, lifecycle management, and scope determination. The Spring IoC Container is responsible for managing these processes.

Three main ways to define and manage beans in Spring:

  1. Annotation-Based Configuration: Beans are typically defined at the class level. Common annotations:
  • @Component: Used for general components.
  • @Service: Defines business services.
  • @Repository: Indicates the data access layer.
  • @Controller or @RestController: Specifies web controllers.

2. XML-Based Configuration: Beans are defined in applicationContext.xml or similar XML configuration files.

3. Java-Based Configuration:Beans are defined within a class annotated with @Configuration, and the @Bean annotation is used to create them.

Bean Lifecycle in Spring

The lifecycle of a bean includes:

  1. Creation: The object is created by Spring.
  2. Dependency Resolution: Dependencies are injected via @Autowired or constructor/setter/field injection.
  3. Initialization: Custom actions are performed using @PostConstruct or InitializingBean.
  4. Destruction: Cleanup tasks are handled using @PreDestroy or DisposableBean.

@PostConstruct

Spring calls methods annotated with @PostConstruct only once, immediately after the bean is initialized.

@PreDestroy

Before a bean is removed, Spring calls methods annotated with @PreDestroy only once. This is used for tasks like releasing resources or closing database connections.

Scopes in Spring

Spring offers six types of scopes:

  1. Singleton: The default scope. A single instance of the bean is created, and all requests for that bean return the same instance.
  2. Prototype: A new instance of the bean is created for every request.
  3. Request: A bean is created for each HTTP request (used in web projects).
  4. Session: A bean is created for each user session.
  5. Application: A single instance is created for the entire lifecycle of the ServletContext.
  6. WebSocket: A single instance is created for the lifecycle of a WebSocket.

Note:
“Beans defined with Application scope are unique to the ServletContext, while those with Singleton scope are unique to the ApplicationContext. Remember, there can be multiple ApplicationContext instances."

Managing Beans with the Same Name

If there are multiple beans with the same type or name, there are two ways to distinguish between them:

  1. Primary Beans: Use the @Primary annotation to give priority to a bean when multiple beans of the same type exist.
  2. Qualifier: Use the @Qualifier annotation to specify a custom name for the bean.

Note:
“If both @Qualifier and @Primary annotations are present, @Qualifier takes precedence."

In this article, we explored beans, one of the fundamental building blocks of Spring.

See you in the next article! 🙂

--

--

Didem AĞDOĞAN
Didem AĞDOĞAN

Written by Didem AĞDOĞAN

Software Developer, Traveller, Curious

No responses yet