Spring is a way to detect the connections between various beans automatically. Further, we can do the same by declaring the dependencies of beans in a single spring file configuration. Moreover, the auto-wiring feature of Spring enables us to infuse object dependency completely. Further, it uses a setter or constructor injection. And the auto wiring only works with reference, unlike others with primitive and string values. There are different types of auto wiring in Spring, and we shall learn in detail.

Autowiring in Spring Explained

Different Types of Autowiring in Spring

Now that we know what auto wiring in the Spring is. The following substantial question most people have in mind is, “What are the different types of auto wiring in spring?”. And we will go through the different types of autowiring in detail.

Autowiring byName

One type of auto wiring in Spring is byName. The byName mode of auto wiring infuses an object’s dependency as per the bean’s name. Moreover, to consider auto wiring in byName mode, the name of the property and the bean must be the same. Further, it works based on the setter method, wherein we can use a setter to function in the byName mode.

Autowiring byType

Another type of auto wiring is the byType. In this mode, the auto wiring helps us to inject the object dependency as per the type. Moreover, the property and bean names can be different, and there are no restrictions for the same. Further, it also functions with the help of the setter method.

Autowiring “Constructor”

The next best auto-wiring type is the constructor. Here the object dependency is infused by connecting with the class’s constructor. Further, only the constructor with a large number of parameters is considered. Even though the functioning of the mode is similar to byType, it focuses more on constructor arguments.

Autowiring “NO”

The Autowiring “NO” means that there is no auto wiring done. Moreover, it is a default auto-wiring mode.

Autowiring Auto Detects

The auto-detect mode of the auto-wiring uses both modes, “constructor” and “byType.” Firstly, it looks for valid constructor arguments; if the argument is available, they will opt for the constructor mode. However, they will consider the byType mode if there are no constructor arguments.

Thus, these are different types of auto wiring in Spring.

Autowired Annotation in Spring

Using the setter method, constructor, or field auto-wiring, we can also use @autowired annotation in the Spring to auto-wire a bean. Moreover, if the configuration is a java based, we can use auto-wiring annotation driven infusing. Further, if the annotation injection is enabled, we can use annotations in properties, setters, and construction modes. Also, we can use autowiring annotations in XML-based configurations in Spring.

Autowiring in Java

As mentioned earlier, we can do autowiring in Java when we enable the annotation injection. Moreover, in Java-based configuration, the bean method is well-defined. In Java, we can wire the beans via a constructor, properties, or setter method. Moreover, there are two types of configurations customer and person. And the customer configurations have dependencies over the person configurations.

Spring Autowiring Concept

The Spring wiring concept is nothing but the autowiring techniques used in the Spring to enable automatic dependency. Enabling the autowiring in Spring helps us to enable automatic dependency infusing. We need to declare all the bean dependencies in a configuration file and auto-wire the connection between collaborating beans. Further, in a Java-based configuration, we can enable autowire in the Spring by using annotations like @componentScan. Moreover, in XML configuration, one can use the tag <context:annotation-config> in the application context file.

Spring Autowire Example

Each autowire example in Spring will let us know how we can use them for different configurations.

Creating Maven Project

One of the examples is creating a Maven project. And these are the steps to follow:

  • Firstly, go to file.
  • Choose a project option.
  • Next, click on the Maven option.
  • Under “select the project name option,” then click on “create a simple project and mark it checked. Also, mark the “continue with the default values” checked.
  • Then on the artifact ID page, fill in the name and package of the project.
  • This way, the Group ID and the artifact ID are set.
  • Finally, hit the finish button to complete and exit from the wizard.

Next is the second phase of the project is as follows:

  • Include the spring dependency 3.2.3.
  • To do the same, we must first locate the “properties section.” Then, on the POM editor’s overview page, create a new property name and set the value as 3.2.3 release.
  • Next, in the “dependencies” section, include the group ID, artifact ID, and version.

In the third phase:

  • Create a class that can be used as a bean to be autowired.
  • Here we can name the beans color.java and autowire it in all possible ways.

Autowiring by “no.”

Another spring autowire example is the autowiring done by using the “no” mode. In the “no” autowiring mode, the bean is set in “no” or default no mode. And auto wiring is not needed.

Autowiring by “byName.”

The following way of autowiring is “byName.” These are the steps:

  • Set the class name as, say, Dog.Java.
  • Further, the bean’s name is color.
  • Combine the public and private voids to make an autowiring.

Autowiring byType

In the byType autowiring mode, we can set the class name as Elephant.java and the bean name as color. Then keep the autowiring in the byType mode. This way, we can autowire Elephant.java with color by combing the public and private voids.

Autowiring using “Constructor” Mode

Using the constructor mode, we can autowire the class Frog.Java with the bean “color.” Further, we can combine the public and private voids and autowire the class with the bean.

Conclusion

Overall, autowiring is one of the most popular features of Spring that enables us to inject object dependencies with ease. Further, these are some of the types of autowiring and their examples that will help us understand the concept of autowiring in spring.