Spring Beans - Autowiring

What is it?

Wiring allows the Spring container to automatically resolve dependencies between collaborating beans by inspecting the beans that have been defined (contents of the BeanFactory).

In the spring framework, setting bean dependencies in configuration files is a good practice, but the spring container can also autowire relationships between collaborating beans.

Autowiring is specified per bean and can thus be enabled for some beans, while others will not be autowired.

The following excerpt from the XML configuration file shows a bean being autowired by name.

<bean id="employeeDAO" class="com.howtodoinjava.EmployeeDAOImpl" autowire="byName" />

Apart from the autowiring modes provided in the bean configuration file, autowiring can be specified in bean classes using @Autowired annotation. To use @Autowired annotation in bean classes, you must first enable the annotation in the spring application using the below configuration.

<context:annotation-config />

The same can be achieved using AutowiredAnnotationBeanPostProcessor bean definition in the configuration file.

<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>

Now, when annotation configuration has been enabled, you can autowire bean dependencies using @Autowired, the way you like.

@Autowired
public EmployeeDAOImpl ( EmployeeManager manager ) {
    this.manager = manager;
}

Explain Different Autowiring Modes in Spring?

There are five auto wiring modes in the spring framework. Let us discuss them one by one.

  1. no: This option is default for spring framework and it means that autowiring is OFF. You have to explicitly set the dependencies using tags in bean definitions.
  2. byName: This option enables the dependency injection based on bean names. When autowiring a property in bean, property name is used for searching a matching bean definition in configuration file. If such bean is found, it is injected in property. If no such bean is found, a error is raised.
  3. byType: This option enables the dependency injection based on bean types. When autowiring a property in bean, property’s class type is used for searching a matching bean definition in configuration file. If such bean is found, it is injected in property. If no such bean is found, a error is raised.
  4. constructor: Autowiring by constructor is similar to byType, but applies to constructor arguments. In autowire enabled bean, it will look for class type of constructor arguments, and then do a autowire by type on all constructor arguments. Please note that if there isn’t exactly one bean of the constructor argument type in the container, a fatal error is raised.
  5. autodetect (deprecated in Spring 3): Autowiring by autodetect uses either of two modes i.e. constructor or byType modes. First it will try to look for valid constructor with arguments, If found the constructor mode is chosen. If there is no constructor defined in bean, or explicit default no-args constructor is present, the autowire byType mode is chosen.

Why does Spring (and the various IDEs) not recommend @Autowired annotation?

Field injection is not recommended.

But @Resource annotation does not produce this warning.

@Autowired vs @Resource

In fact, both utilize annotations for dependency injection but there are a few different details:

  1. Identification of dependency: @Autowired by default injects beans by type whereas @Resource by default uses by name, if not, then by type.
  2. Targets: @Autowired can be used on constructors, methods, parameters, and fields whereas @Resource can only be applied to methods and fields.
  3. Provider: @Autowired is provided by Spring while @Resource is provided by JSR-250.

@Autowired is provided by Spring, which binds it to the specific Spring IoC. If another IoC framework is used instead, the field injection is unsupported.

In contrast, @Resource is provided by JSR-250, which is Java standard. The IoC container we used should be compatible with Java standards so that the dependency can still work normally.

TODO

Make a note of Lombok’s @RequiredArgsConstructor annotation here.

  1. https://www.baeldung.com/spring-annotations-resource-inject-autowire

Links to this note