10

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;
}
Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Vladimir
  • 9,913
  • 4
  • 26
  • 37
  • I didn't understand what you are trying to do? You want to inject string into the field? – Bozho Oct 04 '10 at 10:46
  • Not exactly. I want to expose field as a property without writing a setter method. – Vladimir Oct 04 '10 at 10:51
  • It's a good question, I think. Spring MVC can do direct field injection for MVC command objects, so it's sensible to consider direct field injection for bean properties. However, I don't believe there's any way to do this in Spring. – skaffman Oct 04 '10 at 10:55
  • setter is necessary i think, why do you need to do this? – jmj Oct 04 '10 at 10:55
  • 3
    I just want to replace a bunch of public setters (which are really junk methods) with a plain annotations wherever it is useful. – Vladimir Oct 04 '10 at 11:06

3 Answers3

6

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
  • 588,226
  • 146
  • 1,060
  • 1,140
  • 2
    @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. – Vladimir Oct 04 '10 at 11:13
  • 1
    About lombok -- using this project in my case seems to be like a gun on the wheel. – Vladimir Oct 04 '10 at 11:15
  • Lombok is the way to go. Just annotate your private fields with @Setter and that's it. No runtime dependency, you just need lombok at compile time and the generated .class files will have the setter methods. The only problem is when you want to annotate the method with something like @Required. – Chochos Apr 20 '11 at 15:38
5

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:

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
1

What you are asking for is not possible. Spring subscribes to convention over configuration. So it expects there to be setters and getters. While direct field injection is possible with Spring; and Spring uses Reflection to achieve this, Spring does not provide for reversing this process to use Reflection to access fields without setters or getters. Even Spring AOP implementation expects to find methods to structure it's proxies.

cyotee doge
  • 1,128
  • 4
  • 15
  • 33