How to read data from java properties file using Spring Boot

前端 未结 4 1509
青春惊慌失措
青春惊慌失措 2020-12-01 07:30

I have a spring boot application and I want to read some variable from my application.properties file. In fact below codes do that. But I think there is a good

4条回答
  •  鱼传尺愫
    2020-12-01 08:34

    i would suggest the following way:

    @PropertySource(ignoreResourceNotFound = true, value = "classpath:otherprops.properties")
    @Controller
    public class ClassA {
        @Value("${myName}")
        private String name;
    
        @RequestMapping(value = "/xyz")
        @ResponseBody
        public void getName(){
            System.out.println(name);
        }
    }
    

    Here your new properties file name is "otherprops.properties" and the property name is "myName". This is the simplest implementation to access properties file in spring boot version 1.5.8.

提交回复
热议问题