Akshay
Akshay

Reputation: 1817

Spring ConfigurationProperties to work with fluent setters or custom setters

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()

Upvotes: 4

Views: 2077

Answers (2)

M. Deinum
M. Deinum

Reputation: 125242

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.

Upvotes: 4

Montassar El Béhi
Montassar El Béhi

Reputation: 637

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

Upvotes: 2

Related Questions