Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 942
Akshay Avatar asked Sep 04 '25 03:09

Akshay


2 Answers

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.

like image 160
M. Deinum Avatar answered Sep 06 '25 04:09

M. Deinum


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

like image 34
Montassar El Béhi Avatar answered Sep 06 '25 05:09

Montassar El Béhi