views:

56

answers:

2

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;
}
+2  A: 

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)
Bozho
@Value annotation replaces IoC with Resource Locator. And it requires to define the property value in the java code. And I could not have 2 different instances with different properties. That's why I want only exposure as property.
Dair T'arg
About lombok -- using this project in my case seems to be like a gun on the wheel.
Dair T'arg
+2  A: 

Spring supports annotation-based field injection out of the box for the JSR-250 @Resource annotation. Spring's own @Autowired and JSR 330's @Inject also work.

You just need to add this line to your context.xml:

<context:annotation-config/>

Reference:

seanizer