Lifecycle, bean types and Component scan

Tram Ho

1. The life cycle of the bean

1.1. Bean life cycle

In the previous post we have a quick look at what bean is, today we will go a little deeper.

The life cycle of the bean is understood that from the time the bean is created until it dies, there will be different events. The bean’s life cycle can be described by the following diagram.

It may seem long and confusing, but it sort of includes the following steps:

  • IoC container creates bean by calling constructor (can inject bean dependency here)
  • Call the setter methods to inject beans with setter based injection
  • Other constructors are called (not much concern)
  • @PostConstructor is called
  • Init method is called

The bean will then be ready to go. If the bean is then deprecated, it is destroyed:

  • Call @PreDestroy
  • Destroy beans like normal objects

1.2. @PostConstructor and @PreDestroy

These are two quite important events with the bean, you can hook a method into it to execute when the event occurs:

  • @PostConstruct is after the bean has been initialized
  • @PreDestroy was before the bean was destroyed

We use the above two annotations to mark a certain method, that method will be automatically called when the bean event occurs.

As the above example code, I attach @PostConstruct to the testRun() method. This method is called when the Car bean is completely created and initialized. And before the Car is destroyed, the same stopEngine should be called.

In practice, the above two annotations do the following tasks:

  • @PostConstruct used to perform some tasks when instantiating a bean
  • @PreDestroy performs tasks to clean up the bean after it is used

2. Types of beans

Rather, called scopes, they are classified based on the number of beans created. The bean consists of 5 scopes:

  • Singleton (default): The IoC container only creates exactly 1 object from this bean class
  • Prototype: return a separate bean object for each use.
  • Request: create a bean for each request
  • Session: create each bean for each session
  • Global session: create each bean for each global session (this doesn’t quite understand?)

In the above 5 scopes we are only interested in the first two scopes. Usually you will rarely touch the prototype bean, but I also write it here.

For the singleton bean, there is no need to mark anything, it is the default. If you want to specify a class as the prototype bean, use @Scope as follows.

To clarify the prototype bean, for example bean X is used by two other beans, A, B:

  • If X is a singleton bean, then only one X object is created. A and B share X.
  • If X is the prototype bean, then 2 X is created for the other 2 beans using X for A and X for B.

3. How to define a bean

There are 3 ways to define a class as a bean:

  • Declared in XML file
  • Use annotation on class
  • Use @Configuration and @Bean

Depending on each specific case that used accordingly. For example in this series, I do not discuss bean configuration in depth with XML (Spring Boot was born, not to configure?).

3.1. Use XML, annotations

But simple, you should also know that Spring used XML to configure beans as follows. It was quite extreme so people used a better alternative.

Consequently, bean configuration based on annotations such as @Component . Specifically, as the previous post said, just mark @Component on top of the class, IoC will know and create the bean from that class.

In addition, there are other more specific annotations such as @Service , @Repository , @Controller , … also include @Component , so their effect is also to create bean.

3.3. Use @Bean inside @Configuration

This method is used for the bean that needs to perform many complex operations to initialize, or has many beans related to each other. Therefore, instead of individually initializing each class as each bean, collect the beans to be re-initialized and put in the containing class @Configuration .

Usually, classes marked with @Configuration have the suffix of Config.

When Spring finds @Configuration class, it will create bean of this class first (because @Configuration is also @Component ). During creation, the initialization logic is also executed, to prepare it for internal @Bean creation.

Spring Boot will then find the methods marked @Bean inside @Configuration to create the bean. Often these beans are short and immediately return the object, not for Spring Boot to create.

Beans are also included in the ApplicationContext as usual.

However, not all marked classes can generate beans. That must be conditionally the IoC’s component scan must find it. We will move on to the component scan in a moment.

4. Component scan

4.1. How component scan works

When the Spring Boot application starts running, it will find all the classes marked as beans in the program and create the bean. The process of finding these beans is called component scan.

Component scan will find all classes in packages of the same level or lower packages

Therefore, the class marked @SpringBootApplication containing the main method will be the starting place. Spring Boot will look down from this package (original package) to create beans.

Because the default folder structure of Spring Boot it so, so from the original package DemoApplication.java is com.tonghoangvu.demo , it will find:

  • Classes at the same level, find DemoApplication class, create bean
  • Look down the lower packages like com.tonghoangvu.demo.components and com.tonghoangvu.demo.controllers , find more classes like ChinaEngine , Car , UserController ( Engine.java is the interface).

Therefore, by default all classes declared as beans can be found.

4.2. Custom search package

In case you just want Spring Boot to find beans in a specific package, for example, just look in the components directory, there are two ways as follows.


Today’s article is finished, and it is considered that most of the theory part of Spring Boot is gone. From the next article we will learn about more practical things in Spring Boot.

Happy New Year, remember to upvote and clip if you like it. Happy Vietnamese new year

Share the news now

Source : Viblo