Reading properties from tomcat

前端 未结 6 1475
逝去的感伤
逝去的感伤 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:02

    There are many different ways but it depends on your needs:

    To load a property file from $TOMCAT_HOME/conf directory you will need to access it using a java.io.File object since the class loader (as in this.getClass().getClassLoader().getResourceAsStream(...) is just able to load files (and classes) from your class path (under WEB-INF/classes, WEB-INF/lib or $TOMCAT_HOME/lib).

    The easiest example to load a file from the Tomcat's config directory would be:

    File configDir = new File(System.getProperty("catalina.base"), "conf");
    File configFile = new File(configDir, "myconfig.properties");
    InputStream stream = new FileInputStream(configFile);
    Properties props = new Properties();
    props.load(stream);
    

    Notice that this approach will make your code dependent of Tomcat (the loading mechanism depends on the fact of a Tomcat's system property being available). This is something that I wouldn't recommend at all so if you move your property file to a folder inside your classpath then you should be able to load it the way you tried and your application could be deployed in any container.

    And, you could configure your properties as JNDI resources but it would be too much hassle to access them.

    0 讨论(0)
  • 2020-12-15 23:05

    Rather than referring to location of a particular configuration file, I'd use JNDI-bound configuration, to keep the applications dependent on the configuration data and Java EE standards only. I can see two options:

    1. If you only have a moderate number of configuration entries in your property file, then consider making them Environment Entries and access them via JNDI in your applications.

    2. Alternatively, say in case you want to keep your properties in a separate property file, you might also create a bean to represent the properties, a factory (initializing the bean using a property file) and hook it up to JNDI as a Resource Definition.
      With that, one might even be able to regularly re-read the property file without a need to restart the apps, switch between various sources of configuration or similar other requirements.

    Each of web applications using this configuration will have to include a Resource Reference in it's web.xml to refer to the configuration bean (or a reference to each env. entry if option 1 is used).

    0 讨论(0)
  • 2020-12-15 23:05

    As far as I know you can configure datasource in tomcat also. DataSource in Tomcat

    After this you can use datasource in your application.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-15 23:16

    add .properties file in apache-tomcat-7.0.78\conf folder File configDir = new File(System.getProperty("catalina.base"), "conf"); File configFile = new File(configDir, "dashboardiframes.properties"); InputStream stream = new FileInputStream(configFile);Properties properties = new Properties(); properties.load(stream);

    0 讨论(0)
  • 2020-12-15 23:25

    Put your files in your webapp's WEB-INF/classes. This is the default classpath directory. conf if for Tomcat internals only.

    0 讨论(0)
提交回复
热议问题