@Autowired magic in SpringBoot
Mirek Sedzinski
Posted on June 16, 2024
Disclosure: I'm quite an experienced developer (currently go/python/bit of rust, scala/c# in the past), but a newbie when it comes to Spring Boot.
Recently, I've had to use it in one of my projects and somewhere in the code, I've come across this interesting annotation:
@Autowired
When I've read about it, I couldn't believe my eyes:
In Spring Boot, the @Autowired annotation is used for automatic dependency injection.
This means that Spring will automatically resolve and inject any beans that your object depends on.
I'm familiar with Dependency Injection, but I've never thought that anyone could use, or even propose to use, such an approach.
It's pure magic. I strongly believe that DI should be explicit, and the developer should be aware of what dependencies are injected into his/her class.
Here is, in my opinion, a much better approach to the problem:
https://github.com/google/wire
Wire is a compile-time dependency injection tool. Dependencies are defined in a separate file, and the developer is always aware of what is injected into his/her class.
I'm curious about your opinion on this topic. Do you think that @Autowired is a good approach to DI? Or maybe you prefer a more explicit approach like Wire? Or maybe you have your own way of doing DI? I'm looking forward to your answers.
Posted on June 16, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.