Spring + Lombok: Can I have @Autowired @Setter

11,369

I don't know if there is a way to do this in Spring, but you could try to achieve exactly the same behaviour with http://projectlombok.org/features/experimental/onX.html

So it will be something like

class Foo {
  @Setter(onMethod=@__({@Autowired}))
  private Bar bar;
}

Unfortunately, it's quite ugly...

Also, keep in mind feature status - they said it could be removed from lombok in future releases.

Share:
11,369

Related videos on Youtube

Xorty
Author by

Xorty

Freelance Java developer chronically obsessed with clean design. Focusing mainly on Spring Framework and Java EE. Dependency Injection worshipper.

Updated on October 09, 2022

Comments

  • Xorty
    Xorty over 1 year
    class Foo {
      @Setter @Autowired private Bar bar;
    }
    

    Spring will use field injection here. Is there currently any way of telling it to use a setter injection?