When not to @Autowire in Spring / Spring Boot

Dependency Injection is cool, but many misunderstand the purpose of it and most importantly don’t understand how to use it correctly.

Mohammed Atif
Engineering@Zemoso

--

Senior Dev : Why are you using field injection instead of constructor injection?Junior Dev : What is field injection? I am using @Autowired

A simple conversation that happens pretty often. Sounds trivial but have deep meaning within. Lack of knowledge on why and how things work can lead to catastrophic codes.

Let us dive deep into different type of dependency injections even before we discuss about when and why to use which one

Types on Dependency Injection

  1. Field Injection
  2. Constructor Injection
  3. Setter Injection

Field Injection

In simple words declaring a variable, hopefully private, and then annotating it with @Autowired

@Autowired
private UserService userService;

Constructor Injection

In simple words, letting the Spring Container inject the dependencies directly through constructor

@RestController
public class UserController {
private final UserService userService;
public UserController(UserService userService){
this.userService = userService;
}
}

Setter Injection — Rarely used

It is similar to Field Injection but here setter is Annotated with @Autowired tag.

private UserService userService;@Autowired
public void setUserService(UserService userService){
this.userService = userService;
}

How do they differ?

Keeping it short,

  • Field Injection uses reflection to set the values of private variables
  • Constructor Injection happens at the time of creating the object itself
  • Setter Injection uses setters to set the value

Now that you have got the gist of dependency injections you can explore more about it outside this blog.

No more suspense. Jumping right to the point…

Verdict

Constructor Injection is always the first choice when it comes about Dependency Injection because of its reliable and strict nature.

Field Injection can also be used in the scenarios where Constructor Injection is absolutely not possible (and Circular Dependencies have to be avoided even as a work around).

Other Considerations

One key point that is widely discussed when talking about Field Injection vs Constructor Injection is Required and Optional Dependencies

  • Many debate that we can use Field Injection for optional and Constructor Injection for required dependencies, but having optional dependency itself seems to be a bad design in most of the cases.
  • Since the scope of optional dependency is an Unrestricted Territory, any developer can randomly add multiple dependencies by calling it optional and eventually reduce the quality @ of the overall code.
  • Personally I feel if you need a dependency in your code then it implies that it is required and if it is optional then why to even include it. This statement is debatable and can vary from person to person.

Let me know how you use dependency injection in your code and how it improves the overall quality of your project

In ZeMoSo technologies, we follow the best design and coding principles ensuring highest quality applications every time. To know more about our engineering process follow us on https://medium.com/engineering-zemoso

--

--