Both Spring and Lombok are commonly used projects.
Lombok has a feature to copy annotations from fields onto constructors. This works great for Spring @Component
objects making use of Lombok's @RequiredArgsConstructor
, but also needing a @Qualifier
on a dependency.
This is currently not possible with Spring, as @DefaultValue
is only allowed on ElementType.PARAMETER
.
Allowing @DefaultValue
on an ElementType.FIELD
would enable immutable ConfigurationProperty objects with default values.
A sample class using a manually maintained constructor:
package com.example.demo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.boot.context.properties.bind.DefaultValue;
@ConstructorBinding
@ConfigurationProperties("app")
public class DemoConfigurationProperties {
public DemoConfigurationProperties(@DefaultValue("default!") String property) {
this.property = property;
}
private final String property;
}
Allowing the default value annotations would enable a lombok.config consisting of:
lombok.copyableannotations += org.springframework.boot.context.properties.bind.DefaultValue
and the above class written more concisely as:
package com.example.demo;
import lombok.RequiredArgsConstructor;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.ConstructorBinding;
import org.springframework.boot.context.properties.bind.DefaultValue;
@ConstructorBinding
@ConfigurationProperties("app")
@RequiredArgsConstructor
public class DemoConfigurationProperties {
@DefaultValue("default!")
private final String property;
}
This would be more useful in configuration classes that contain more than one property.
Comment From: wilkinsona
This is a duplicate of https://github.com/spring-projects/spring-boot/issues/20402.