question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Quick fix action to refactor field injection to constructor injection

See original GitHub issue

In 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:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:13 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
BoykoAlexcommented, Sep 21, 2022

@MahatmaFatalError A parameter is to be added to a constructor if the constructor meets the following criteria:

  1. No constructors. Constructor is created and is not annotated.
  2. Only one constructor. Parameter is added.
  3. Multiple constructors, only one annotated with @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.

1reaction
martinlippertcommented, Sep 19, 2022

@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 check Experimental reconciling for Java sources based on Rewrite project. Once you have that setting enabled, the quick fix will appear on code like this:

	@Autowired
	private MyService service;

When you put the cursor on @Autowired or service and hit the shortcut for Quick Fix, you will see a quick fix appearing called Convert @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!!!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found