Java 如何在Tomcat启动时初始化web应用程序常量(从文本文件)?

Java 如何在Tomcat启动时初始化web应用程序常量(从文本文件)?,java,tomcat,web-applications,configuration,Java,Tomcat,Web Applications,Configuration,我已经在本地Tomcat7上部署了一个web应用程序。此web应用程序需要知道三个文件夹的位置 下面是该代码目前的样子: public class Constants { public enum Folder { InboundFolder("C:\\inbound\\"), StagingFolder("C:\\staging\\"), OutboundFolder("C:\\outbound\\"); privat

我已经在本地Tomcat7上部署了一个web应用程序。此web应用程序需要知道三个文件夹的位置

下面是该代码目前的样子:

public class Constants {

    public enum Folder {

        InboundFolder("C:\\inbound\\"),
        StagingFolder("C:\\staging\\"),
        OutboundFolder("C:\\outbound\\");

        private String location;

        private Folder(String location) {
            this.location = location;
        }

        public String locate() {
            return location;
        }
    }

    // ...and so on...
我不想硬编码这些值;相反,我希望在启动Tomcat7时从文本源加载这些常量的值。我读过关于
ServletConfig
ServletContext
的内容,但我不确定它们是否是我想要的解决方案

我应该怎么做呢?

使用以下方法:

public static Object myDataStaticObject=null;

...

if (myDataStaticObject==null) {
    File file= new File("C:\\inbound\\", "myCfg.properties");
    InputStream stream = new FileInputStream(file);
    Properties p = new Properties();
    p.load(stream);
    //set your data to myDataStaticObject
}
在静态对象中加载数据&检查第一次加载时是否为null

考虑一下生产服务器中的目录权限

使用以下命令:

public static Object myDataStaticObject=null;

...

if (myDataStaticObject==null) {
    File file= new File("C:\\inbound\\", "myCfg.properties");
    InputStream stream = new FileInputStream(file);
    Properties p = new Properties();
    p.load(stream);
    //set your data to myDataStaticObject
}
在静态对象中加载数据&检查第一次加载时是否为null


考虑一下生产服务器中的目录权限

有几种方法可以解决您的问题

1
。使用ServelContext
web.xml条目


文件1
文件1位置
文件2
文件2位置
并在需要时在运行时获取这些值

2
。使用属性文件并加载该属性文件,然后使用访问器方法访问这些属性

3
。使用XML基本配置和解析,并将其保存在内存中。(您还可以使用WatchServices,以便在文件发生更改时重新加载该文件)

4
。启动应用程序服务器时,将文件位置作为运行时参数提供(最少优先)

-Dfile1=FILE1-LOCATION-Dfile2=FILE2-LOCATION


有几种方法可以解决您的问题

1
。使用ServelContext
web.xml条目


文件1
文件1位置
文件2
文件2位置
并在需要时在运行时获取这些值

2
。使用属性文件并加载该属性文件,然后使用访问器方法访问这些属性

3
。使用XML基本配置和解析,并将其保存在内存中。(您还可以使用WatchServices,以便在文件发生更改时重新加载该文件)

4
。启动应用程序服务器时,将文件位置作为运行时参数提供(最少优先)

-Dfile1=FILE1-LOCATION-Dfile2=FILE2-LOCATION


在这种情况下,您可以选择如何初始化常量。有两种常见的方法

1)初始化参数。

web.xml

<servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>my.package.MyServlet</servlet-class>

    <init-param>
        <param-name>myParam</param-name>
        <param-value>paramValue</param-value>
    </init-param>
</servlet>
2)配置文件

您应该在应用程序中创建属性文件,例如config.properties

myParam=paramValue
Config.java

public class Config {

    private Properties config;

    public Config() {
        config = new Properties();
        reloadConfig();
    }

    public Properties getConfig(){
        reloadConfig();
        return config;
    }

    public String getProperty(String key){
        return getConfig().getProperty(key);
    }

    public void reloadConfig() {

            try {
                config.load(getClass().getResourceAsStream("/config.properties"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
java

public class MyServlet extends HttpServlet {

    protected String s;

    @Override 
    public void init(ServletConfig servletConfig) throws ServletException{
        super(servletConfig);
        this.s = servletConfig.getInitParameter("myParam");
    }

    ....

}   
public class MyServlet extends HttpServlet {

    protected Config config = new Config();

    @Override 
    public void doGet(request, response) throws ServletException{

        Strign s = config.getProperty("myParam");

    }

}

在这种情况下,您可以选择如何初始化常量。有两种常见的方法

1)初始化参数。

web.xml

<servlet>
    <servlet-name>myservlet</servlet-name>
    <servlet-class>my.package.MyServlet</servlet-class>

    <init-param>
        <param-name>myParam</param-name>
        <param-value>paramValue</param-value>
    </init-param>
</servlet>
2)配置文件

您应该在应用程序中创建属性文件,例如config.properties

myParam=paramValue
Config.java

public class Config {

    private Properties config;

    public Config() {
        config = new Properties();
        reloadConfig();
    }

    public Properties getConfig(){
        reloadConfig();
        return config;
    }

    public String getProperty(String key){
        return getConfig().getProperty(key);
    }

    public void reloadConfig() {

            try {
                config.load(getClass().getResourceAsStream("/config.properties"));
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
java

public class MyServlet extends HttpServlet {

    protected String s;

    @Override 
    public void init(ServletConfig servletConfig) throws ServletException{
        super(servletConfig);
        this.s = servletConfig.getInitParameter("myParam");
    }

    ....

}   
public class MyServlet extends HttpServlet {

    protected Config config = new Config();

    @Override 
    public void doGet(request, response) throws ServletException{

        Strign s = config.getProperty("myParam");

    }

}

为什么你不;我不想断章取义?不是我不想断章取义。我的代码就是现在的样子,因为我不知道做这件事的最佳方法是什么。我将选择最简单的一种;我不想断章取义?不是我不想断章取义。我的代码就是现在的样子,因为我不知道做这件事的最佳方法是什么。我会选择最简单的。将它们放在servlet上下文中或使用属性文件谢谢!我使用第一种方法:)谢谢!我使用的是第一种方法:)