Quick fix action to refactor field injection to constructor injection
See original GitHub issueIn general, field injection comes with some downsides http://olivergierke.de/2013/11/why-field-injection-is-evil/
Unfortunately, refactoring of old, grown code bases towards constructor injection is tedious. So I was looking for a refactoring automation (and apparently I was not the first one with this issue https://stackoverflow.com/questions/44008985/convert-spring-field-injection-to-constructor-injection-intellij-idea)
So in essence, a quick fix action on @Inject
or @Autowired
fields to create a new or extend an existing constructor (depending on (required = false) flag) would be really nice.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:13 (5 by maintainers)
Top Results From Across the Web
Convert Spring field injection to constructor injection (IntelliJ ...
This is the quick fix related to the inspection "Field injection warning": Spring Team recommends: "Always use constructor based dependency ...
Read more >To field inject, or not to field inject in Spring?
Let's say you decide to refactor a class to use constructor injection instead. To make it more challenging, suppose that you chose a...
Read more >Quick fix to generate constructor from field injections ignores ...
Thus ignoring the "required=false" attribute. This changes the program as now Component2 is also required when current component gets created.
Read more >Dependency Injection: Field Injection vs Constructor Injection?
Using field injections means narrowing down compatibility of a class to dependency injection environments that instantiate objects using reflection and support ...
Read more >Why I Changed My Mind About Field Injection?
I would use constructor injection for mandatory dependencies and setter injection for optional dependencies. This way you are not hiding the ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@MahatmaFatalError A parameter is to be added to a constructor if the constructor meets the following criteria:
@Autowired
. This constructor is added a parameter.Therefore, this refactoring doesn’t add
@Autowired
. @martinlippert was referring to the use case in your screenshots. First screenshot - no constructor case. The new constructor doesn’t get the annotation and it is a good practice.@MahatmaFatalError This belongs to a broader effort that we are working on at the moment to integrate refactorings and code changes using https://github.com/openrewrite, so the code change part of this quick fix is actually implemented in https://github.com/openrewrite/rewrite-spring.
Since this is an early stage and not yet meant for mainstream consumption, you need to enable this in the preferences via
Preferences -> Language Servers -> Spring Language Servers -> Spring Boot Language Server
and then checkExperimental reconciling for Java sources based on Rewrite project
. Once you have that setting enabled, the quick fix will appear on code like this:When you put the cursor on
@Autowired
orservice
and hit the shortcut for Quick Fix, you will see a quick fix appearing calledConvert @Autowired field into Constructor Parameter
.But keep in mind, this is a very early, experimental feature and not yet optimized or meant for mainstream consumption. It is early work towards enabling a broader set of new Spring-related refactorings, validations, and automated code changes.
Hope this helps. Feedback always welcome!!!