Xorty
Xorty

Reputation: 18861

Spring + Lombok: Can I have @Autowired @Setter

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?

Upvotes: 23

Views: 17357

Answers (1)

slnowak
slnowak

Reputation: 1919

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

JDK8+:

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

JDK7:

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.

Upvotes: 35

Related Questions