2
votes

We are using project lombok for setters and getters and we prefer fluent accessors for setter & getters . The problem arises when we use ConfigurationProperties with fluent accessors. Spring is not able to wire the properties with the class fields . The same things works when we remove the Accessor annotation and have the classic setters and getters. Is there a way we can use custom setters with Configurationproperties

import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.boot.context.properties.ConfigurationProperties;

@ConfigurationProperties
@Data
@Accessors(fluent = true)
public class Properties {

     private String property1;

     private String property2;

}

We get a null pointer exception while accessing this property as

properties.property1()
2
Spring uses the Java Bean specification to identify properties. Those are hardwired into the JDK support classes (introspection and reflection). So at the moment spring (boot) doesn't support custom getters and setters. - M. Deinum

2 Answers

2
votes

The framework works with the by convention naming as get***/is*** for getters and set*** for setters. In addition, @Accessors lombok feature is still experimental. Please see this link

2
votes

As Spring (and thus Spring Boot) uses the Java Beans Specification and for this it uses the default JDK support available.

The introspection and reflection API defines properties as a getter/setter. Setters are void and getters should return the actual field (the return and method argument types must also match).

So with that in mind Spring doesn't support custom getters/setters, simply because the JDK classes don't provide this.