Spring read properties file
If you see the above warning message, add the following dependency to your pom. Here is my simple controller class where I call toString method which returns the user data stored in the properties file. To get the only one or two value or want to prepare properties file data according to your need, use the getter methods.
Create application. You can also create an application. If you want to name your configuration file as something other than application.
You can also refer to an explicit location by using the spring. Once you have your properties file ready you can use it in your Spring beans. One of the way to inject a property value from the properties is to use Value annotation. I am going to share 3 ways:. When your Spring Boot project is created you should have an application. If for some reason you do not have application.
Most probably one of the easiest ways to read a property from application. All you need to do is:. Like this:. Please note that to be able to read a property from application. Then I can simply call its getProperty String key method to get the value of a requested property. Another very simple way to read application properties is to use Value annotation. Simply annotation the class field with Value annotation providing the name of the property you want to read from application.
Here you can see how fields are annotated with Value property key name annotation in order to map them with the specific property in the. Rather than using Value annotation, Environment should be used to read properties file in Spring. Actually PropertySource annotation adds a PropertySource to Spring's Environment so that can be used to make your code simpler.
In that case you just need to Autowire or Inject Environment into your Config class and use Environment object to get property value. In cases where a given property key exists in more than one. For example, given two properties files a.
0コメント