Bean autowiring in Spring framework

The nature of Spring is to create objects in its container so that when you need an object, you can call the Spring container to get that object. Therefore, Spring seeks all the ways to support us in the creation of objects in the container of the most easily.

We knew how to inject this object into another object, or we can say: put object dependencies on dependent objects, using the ref, list attribute, … In addition, Spring also helps us to place the object dependencies into the dependent object automatically using its Bean Autowiring.

With Bean Autowiring, we do not need to use the ref or list attributes anymore, … just write code that complies with Spring’s rules, and objects dependencies will be automatically injected into the dependent object.

There are three ways to use Autowiring Bean, which are:

  • byName
  • byType
  • constructor

For better understanding, I will make an example. In this example, we will require Spring automatically put the Table object into the Room object. As follows:

Bean autowiring in Spring framework

I will use Java 17 for this example:

Spring dependency is as follows:

Room class:

Table class:

Application class:

And declare the Table and Room objects in the Spring configuration file:

OK, now I will go one by one and tell you more about it!


byName

As you all know, every bean in a Spring container has its own id. Based on this, Spring specifies if the id of the object dependency (in this example is a Table object) is the same as the name of a variable in the dependent object (Room object) and when declaring the dependent object, we declare the attribute autowire=”byName”, so Spring will automatically put the object dependency into the dependent object via the setter method of the dependent object.

In our example, the id of the Table object is exactly the same as the table variable name in the Room object, so we just need to declare more for the bean room a property autowire=”byName”. Then, Spring will inject the Table object into the Room object for us.

The configuration of the Room object will be modified as follows:

At running, the program will produce the following results:

Bean autowiring in Spring framework

This has the disadvantage that if we later change the variable name of the dependent object, we must modify the id of the object dependency in Spring’s container and vice versa. Can you imagine it?


byType

This approach is applicable only to small projects where each object has only one bean in the Spring container. By changing the autowire=”byType” attribute when declaring a bean, Spring automatically puts the object dependency into the dependent object via the setter of the dependent object.

Result:

Bean autowiring in Spring framework

If you deliberately declare another bean of the object dependency as follows:

Then it will run with an error:

Bean autowiring in Spring framework


constructor

Like the attribute autowire=”byType”, this method only applies when our object has only one bean in Spring’s container. Then, Spring will automatically put the object dependency into the dependent object through the constructor of the dependent object.

For example, we change the configuration of the bean room as follows:

Result:

Bean autowiring in Spring framework

Add Comment