简体   繁体   English

如何读取放置在战争之外的属性文件?

[英]How to read properties file placed outside war?

I'm working on a web application these days.这些天我正在研究 web 应用程序。 That uses only jsps and servlets.那只使用 jsps 和 servlets。 It's a small application.这是一个小应用程序。 Right now I have placed all the DataSource details in DAO utility class.现在,我已将所有 DataSource 详细信息放在 DAO 实用程序 class 中。 I want to place these details in a properties file that can be placed outside the war, so that depending on the environment we can change these values, without effecting the war.我想将这些细节放在可以放在战争之外的属性文件中,以便根据环境我们可以更改这些值,而不会影响战争。 How can I achieve this?我怎样才能做到这一点?

Provide the file name using context param or java system parameter.使用上下文参数或 java 系统参数提供文件名。

1.Context Param 1.上下文参数

<context-param>
<param-name>daofilename</param-name>
<param-value>D:\daofilename.props</param-value>
</context-param>

2.System Parameter 2.系统参数

java -Ddao.filename=D:\daofilename.props server

This is how I do this:这就是我这样做的方式:

Add the following to context.xml file (conf folder, in tomcat installation dir).将以下内容添加到 context.xml 文件(conf 文件夹,在 tomcat 安装目录中)。 you may change the name attribute;您可以更改名称属性; and in value set the path of the folder where you have your properties files.并在值中设置您拥有属性文件的文件夹的路径。

<Environment name="config" value="C:\Program files\my app\"  
         type="java.lang.String" override="false"/>

Then in your util class, you can get the file like this: (in "java:comp/env/config" replace 'config' with the value of the "name" attribute you used in context.xml)然后在您的 util class 中,您可以像这样获取文件:(在“java:comp/env/config”中,将“config”替换为您在 context.xml 中使用的“name”属性的值)

String folderName = null;
Properties properties = new Properties();
try {
   InitialContext context = new InitialContext();
   folderName = (String) context.lookup("java:comp/env/config");
} catch (NamingException ex) {
   System.out.println("exception in jndi lookup");
}
if(folderName != null) {
   File configFile = new File(folderName + "yourfile.properties");
   try {
       InputStream is = new FileInputStream(configFile);
       properties.load(is);
   } catch(IOException ex) {
      System.out.println("exception loading properties file");
   }
}

Hope this helps you or anyone else.希望这可以帮助您或其他任何人。

Depending on your web server, you can place the properties file in some location that is included in the classpath.根据您的 web 服务器,您可以将属性文件放置在类路径中包含的某个位置。 For example, for some tomcat versions, that would be ${TOMCAT_BASE}/shared/classes .例如,对于某些 tomcat 版本,这将是${TOMCAT_BASE}/shared/classes The webapp can then use something like the following to read the file and have it be automatically found in this location.然后,webapp 可以使用类似以下内容来读取文件并在此位置自动找到它。

ServletContext context = getServletContext();
InputStream is = context.getResourceAsStream("yourfilename.cnf");

You can also name the file after your webapp's installed name and use that name in your code when loading the file from the classpath.您还可以在 webapp 的安装名称之后命名文件,并在从类路径加载文件时在代码中使用该名称。 That way, you can have the properties files for multiple webapps in the shared directory without conflicting with each other.这样,您可以在共享目录中拥有多个 webapps 的属性文件,而不会相互冲突。

You've indicated that you don't have access to the ServletContext because you want the code to be in a utility class.您已指出您无权访问 ServletContext,因为您希望代码位于实用程序 class 中。 One way you can get around this limitation is to register a ServletContextListener that creates an instance of your property file reader (since it has access to the context) and registers it so other code can use it.解决此限制的一种方法是注册一个 ServletContextListener,它创建一个属性文件读取器的实例(因为它可以访问上下文)并注册它以便其他代码可以使用它。 Something like the following:类似于以下内容:

public class MyServletContextListener extends ServletContextListener{
    public void contextInitialized(ServletContextEvent event){
        ServletContext context = event.getServletContext();
        context.setAttribute("settings", new MyPropertyReader(context));
    }

    public void contextDestroyed(ServletContextEvent event){}
    }

}

You should give property file location as java runtime property to your web container..您应该将属性文件位置作为 java 运行时属性提供给您的 web 容器。

Such as, java -DmypropertyFile.location=c:/propertyfile.properties -jar yourContainer.jar如, java -DmypropertyFile.location=c:/propertyfile.properties -jar yourContainer.jar

If you are using Tomcat you can add any properties you like to the context, look at the documentation for context.xml .如果您使用 Tomcat 您可以将任何您喜欢的属性添加到上下文中,请查看context.xml 的文档 All the application servers has their own way of doing this, so you will have to look around for examples of doing this in JBoss, Glassfish, etc..所有应用程序服务器都有自己的执行方式,因此您必须在 JBoss、Glassfish 等中查看执行此操作的示例。

Another, harder but probably better solution is to use JNDI resources .另一个更难但可能更好的解决方案是使用JNDI 资源

The approach I've usually taken is for the program to assume that the configuration file is located under %user.home%/.program/config.properties .我通常采用的方法是让程序假定配置文件位于%user.home%/.program/config.properties下。

If the file is not found (usually on first running) a copy is made from the classpath of a 'default' configuration file and placed there.如果找不到该文件(通常在第一次运行时),则从“默认”配置文件的类路径复制并放置在那里。

For the sake of flexibility this is usually overridable using -Dconfig=somepath.为了灵活起见,这通常可以使用 -Dconfig=somepath 覆盖。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM