Spring bean fields injection

Vladimir picture Vladimir · Oct 4, 2010 · Viewed 11.8k times · Source

Using Spring IoC allows to set bean properties exposed via setters:

public class Bean {
    private String value;
    public void setValue(String value) {
        this.value = value;
    }
}

And the bean definition is:

<bean class="Bean">
    <property name="value" value="Hello!">
</bean>

Is there any existing plugins/classes for Spring Framework that allows to directly expose bean fields as properties without defining setters? Something like this with the same bean definition:

public class Bean {
    @Property
    private String value;
}

Answer

Bozho picture Bozho · Oct 4, 2010

You can:

  • use the @Value annotation and inject a property (using expression language)
  • take a look at Project Lombok, which will let you skip all setters and getters (and more)