Monday, September 25, 2017

The @autowired conundrum

The @autowired annotation will work fine if one has only one implementation of the interface. If one has only one implementation then does it really make sense to have interfaces? It will make sense if one has the injected class in a different module and one wishes to maintain a decoupling between the modules.
In a spring application one creates an interface for the service and injects into the controller, one creates and interface for the DAO and injects into the controller.
In a project using agile does it help having the three components in different modules, being developed by different teams? If the answer seems to be "no" then does creating an interface and using @autowire add any value.

In cases where one has more than one implementation of the service or DAO one needs to use @Qualifier to clear the ambiguity. If one uses this, then isn't one breaking dependency injection? Is it really Inversion of Control?

It appears that the only purpose that these interfaces can help is for Acceptance Testing.

And the main question, Does @autowired make sense at all?
 

No comments: