How to get the value from system property in spring boot

Tags: , , ,



I am using following command to run my spring boot application

java -Dlibrary.system.property=value -jar myapp.jar

Currently, I am able to access it via following command like below

System.getProperty("library.system.property")

However I need to access it via any annotation in Spring something like

@value(${library.system.property})

I tried to use

    @Value("${library.system.property")
    private String property;

    @Bean
    public SampleProvider getSampleProvider () {
        return SampleProvider.from(property);
    }

But the value of the property is null. Do I need to use conditional bean or something?

Answer

Thanks all. Issue got resolved by changing the way of passing the argument through command line as below

java -jar myapp.jar --library.system.property=value

Accessing the value by below code snippet

@Value("${library.system.property}")
private String property;

@Bean
public SampleProvider getSampleProvider () {
    return SampleProvider.from(property);
}


Source: stackoverflow