openrewrite / openrewrite/rewrite-spring

Replacing field injection with constructor injection

Open
#557 16 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

recipe
Dominant language
Java
Stars
403
Forks
149
Avg merge
2h 33m
Merged PRs (30d)
10

Description

What problem are you trying to solve?

We want to convert a bunch of classes that used Spring field injection to constructor injection. We are willing to live with some caveats (e.g. only works if no constructors, select annotations, etc).

Describe the situation before applying the recipe

class A {
   @Autowired
   @Qualifier("B")
    private B b;
}

Describe the situation after applying the recipe

class A {
    private B;

    public A(@Qualifier("B") b) {
        this.b = b;
    }
}

Any additional context

We could likely get value from something that didn't hit all the corner cases. However, if there are a bunch of corner cases that ultimately means an unending feature request on this, it's not like we really need this.

Are you interested in contributing this recipe to OpenRewrite?

Yes.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

No file or test is named, so begin by reviewing the repository's existing Spring recipe entry points and unit-test conventions. Use the issue's before-and-after examples to define the initial transformation, then verify the stated constructor, annotation, qualifier, and caveat cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.