DEV Community

Tommy
Tommy

Posted on

Spring Autowiring

Spring Autowiring is a feature of the Spring Framework that allows for the automatic injection of dependencies between beans in a Spring application. It allows developers to define the dependencies of a bean in the configuration file, rather than hard-coding them in the bean's class.

Autowiring can greatly simplify the process of creating and configuring beans in a Spring application, making it easier to manage dependencies and improve code maintainability.

How to autowire beans?

  • XML Configuration: Autowiring can be specified in XML configuration files by setting the "autowire" attribute on the definition to one of the autowiring modes, such as "byType" or "byName".
<bean id="exampleBean1" class="com.example.ExampleBean1" autowire="byType"/>

<bean id="exampleBean2" class="com.example.ExampleBean2" autowire="byName"/>

<bean id="exampleBean3" class="com.example.ExampleBean3"/>
Enter fullscreen mode Exit fullscreen mode
  • Java Configuration: Autowiring can be specified in Java configuration files by using the @Autowired annotation on the setter methods or fields that should be autowired. Alternatively, you can use the @Autowired constructor to autowire by constructor. In the example below, we annotate the fields.
@Component
public class ExampleConfig {

  @Autowired
  private ExampleBean1 exampleBean1;

  // You can use @Qualifier annotation along with @Autowired to autowire beans by name.
  @Autowired
  @Qualifier("exampleBean2")
  private ExampleBean2 exampleBean2;

...
Enter fullscreen mode Exit fullscreen mode

The default autowiring mode in Java configuration is "byType", while in XML configuration, it is set to "no".

Autowiring Modes:

Autowiring modes are different ways in which the Spring framework can wire beans together automatically.

  1. autodetect: When autowiring is set to "autodetect", Spring framework will first look for a constructor to resolve the bean dependencies, and if a constructor is not found, it will fall back to using the "byType" mode.

  2. no: When autowiring is set to "no", it means that the beans are not wired together automatically and the developer must manually specify the dependencies between the beans in the configuration. This mode provides the greatest degree of control over how beans are wired together, but it also requires the most effort on the part of the developer.

  3. byName: This mode looks for a bean with the same name as the property that is being set. For example, if a class has a property called "processor", the Spring container will look for a bean named "processor" and wire it to the property.

  4. byType: This mode looks for a bean of the same type as the property that is being set. For example, if a class has a property of type "Processor", the Spring container will look for a bean of type "Processor" and wire it to the property.

  5. constructor: Autowiring can also be done on constructors, in which case the Spring container will look for beans of the same type as the constructor arguments and wire them to the constructor.

Oldest comments (0)