Home > Net >  How define properties names using @ConfigurationProperties in Spring?
How define properties names using @ConfigurationProperties in Spring?

Time:01-21

I have class:

@ConfigurationProperties(prefix = "user")
public class BarcodeConfig {
    private String customName;
    private Details customDetails;

And property file with:

user.name=sampleName
user.details.address=sampleAddress
user.details.something=sampleSomething

How map "user.name" to field "customName" and "user.details.*" to "customeDetails" without changing field names?

CodePudding user response:

you can use @AliasFor("name")

CodePudding user response:

You have to provide the property path from your configuration file using the @Value annotation.

For example, if you want to map the user.name to customName:

...
@Value(${user.name})
private String customName;
...

Spring will automatically fetch the user.name property value and assigns it to the customName variable attribute.

  • Related