Reading properties from tomcat

前端 未结 6 1476
逝去的感伤
逝去的感伤 2020-12-15 23:02

What is the best practice for reading config file(s) for your application. Which folders in tomcat server are \"on the classpath\".

I tried placing my config file in

6条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-15 23:12

    Though it might be a PITA, it is probably also a feature that you don't know where it will be deployed. You can't "couple" yourself to that fact then!

    Depending upon the rest of your Java stack, the best way is usually independent of Tomcat. If you are using Spring, you can say for example:

    new ClassPathResource("**/myFile.properties")
    

    or if using Java Config, another example:

    @PropertySource("classpath:META-INF/MyWeb.properties")
    

    In plain Java you can say:

    InputStream stream = loader.getResourceAsStream(resourceName);
    

    where loader is an instance of ClassLoader

提交回复
热议问题