Reputation: 831
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?
Upvotes: 13
Views: 32084
Reputation: 1167
@Configuration
Public class YourAppConfig {
@Value("${your.system.property}") String prop
@Autowired
Public void LoadProperties() {
System.setProperty(PROPERTY_NAME, prop) ;
}
@Bean
...
Upvotes: 1
Reputation: 765
You can access the system properties using the following expression:
@Value("#{systemProperties['library.system.property']}")
private String property
You can also access the system environment using the following expression:
@Value("#{systemEnvironment['SOME_ENV_VARIABLE']}")
private String property
Lastly, if you are using Spring Boot you can refer to the property name directly as long as you are passing it in the command line. For example, if you launch the JAR like java -jar boot.jar --some.property=value
you can read it as:
@Value("${some.property}")
private String property
Upvotes: 19
Reputation: 18986
Simply use
import org.springframework.boot.system.SystemProperties;
...
String propertyKey = "library.system.property";
String propertyValue = SystemProperties.get(propertyKey);
Upvotes: -1
Reputation: 30
You can access your data from properties file by using @ Value on your controller. For example: @Value("${library.system.property}") private String property;
Now your value is in property,now you can use it anywhere.
Upvotes: -2
Reputation: 831
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);
}
Upvotes: 3