How to read data from java properties file using Spring Boot

前端 未结 4 1468
青春惊慌失措
青春惊慌失措 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:16

    I have created following class

    ConfigUtility.java

    @Configuration
    public class ConfigUtility {
    
        @Autowired
        private Environment env;
    
        public String getProperty(String pPropertyKey) {
            return env.getProperty(pPropertyKey);
        }
    } 
    

    and called as follow to get application.properties value

    myclass.java

    @Autowired
    private ConfigUtility configUtil;
    
    public AppResponse getDetails() {
    
      AppResponse response = new AppResponse();
        String email = configUtil.getProperty("emailid");
        return response;        
    }
    

    application.properties

    emailid=sunny@domain.com

    unit tested, working as expected...

提交回复
热议问题