Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring + Lombok: Can I have @Autowired @Setter

Tags:

spring

lombok

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?

like image 845
Xorty Avatar asked Feb 23 '15 22:02

Xorty


1 Answers

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.

like image 95
slnowak Avatar answered Nov 02 '22 23:11

slnowak