Search code examples
springspring-bootspring-properties

How to get the effective Properties in Spring Boot application regardless of where it is defined?


suppose I define a property when starting my Spring Boot application from command line by passing -Dmy.property=314 to JVM, and also I define this property in the application.properties :

my.property=318

to my knowledge the command line one has higher priority and when I inject the value of my.property in a bean I get 314. is there any API that I can get properties regardless of where it is defined and respect this priority? I mean I get the property that will be injected in beans by Spring.


Solution

  • If you don't want to inject the property via other mechanisms like @Value or bindings like @ConfigurationProperties, you can get it via Environment

        @Autowired
        private Environment env;
        ...
        env.getProperty("xxx.yyy");