How to read properties file in java using spring




















In fact below codes do that. But I think there is a good method for this alternative. You can use PropertySource to externalize your configuration to a properties file. There is number of way to do get properties:. Here your new properties file name is "otherprops. This is the simplest implementation to access properties file in spring boot version 1. In this we will map prefix of key using ConfigurationProperties and key name is same as field of class. Reference : how to read value from application.

Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. How to read data from java properties file using Spring Boot Ask Question.

Asked 5 years, 6 months ago. There are scenarios when you have to provide few configuration properties in order to configure the resource like in case of Database you need to provide driver class, DB location, user name and password or in case of sending mail through your application you need to provide properties like SMTP host, user name, password.

Many a times developers put all these details in the Spring XML configuration file itself which is not a good practice. Another advantage is that any change in any setting will require you to change the specific properties file only. Suppose you have your Database properties in a properties file called db. In order to resolve these placeholders you must register a PropertySourcesPlaceholderConfigurer. Spring also has PropertySource annotation added in Spring 3.

Mark Elliot Mark Elliot 71k 19 19 gold badges silver badges bronze badges. I did what you advised me but it still throws the same exception — nikos. Steven Gomez Steven Gomez 5 5 silver badges 2 2 bronze badges. Don't forget to close the Reader in prop. There are many methods in that link. Why did you choose the one with class loader? Are there any advantages or disadvantages of that method? You can use ResourceBundle class to read the properties file.

Paul Vargas This approach is recommended for i18n. My use case is for Jenkins so it's nice to have a solution that doesn't rely upon a bunch of ClassPathLoader shenanigans. You can't use this keyword like - props. Mukus Mukus 4, 2 2 gold badges 37 37 silver badges 54 54 bronze badges. ApplicationContext is available only in case of Spring application — joro. Then load it as: props. Since your properties file is not at the root of your project, you don't need the leading slash.

I was getting the exception thrown in the first place without the leading slash. It's still not working. You can use java. I am going to share 3 ways: Read application. Read application. All you need to do is: 1 Use the Autowired annotation to inject the Environment object into your Rest Controller or Service class, like so: Autowired private Environment env; 2 User getProperty String key method to get a value for a specific property.

Here is my Rest Controller class package com. Autowired; import org. Environment; import org. GetMapping; import org. RequestMapping; import org. RequestParam; import org. Reading Properties with Value Annotation Another very simple way to read application properties is to use Value annotation.



0コメント

  • 1000 / 1000