【问题标题】:How to initialize web-app constants (from a text file) upon Tomcat startup?如何在 Tomcat 启动时初始化 Web 应用程序常量(来自文本文件)?
【发布时间】:2013-07-06 16:36:48
【问题描述】:

我在本地 Tomcat 7 上部署了一个 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...

我不想硬编码这些值;相反,我想在启动 Tomcat 7 时从文本源加载这些常量的值。我已经阅读了 ServletConfigServletContext 但我不确定它们是否是我的解决方案想要。

我该怎么做呢?

【问题讨论】:

  • 为什么你不想在上下文中保留它?
  • 并不是我不想在上下文中保留它。我的代码就是现在的样子,因为我不知道这样做的最佳方法是什么。我会选择最直接的。
  • 将它们放在 servlet 上下文中或使用属性文件

标签: java tomcat web-applications configuration


【解决方案1】:

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

1。使用serveltContext
web.xml 条目

  <context-param>
         <param-name>file1</param-name>
         <param-value>file1-location</param-value>
      </context-param>
      <context-param>
         <param-name>file2</param-name>
         <param-value>file2-location</param-value>
      </context-param>

并在需要时在运行时获取这些值。

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

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

4。在启动应用程序服务器时提供您的文件位置作为运行时参数(最不受欢迎)

-Dfile1=文件1-位置-Dfile2=文件2-位置

【讨论】:

    【解决方案2】:

    在这种情况下,您有很多选择如何初始化常量。常用的有两种方式

    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>
    

    MyServlet.java

    public class MyServlet extends HttpServlet {
    
        protected String s;
    
        @Override 
        public void init(ServletConfig servletConfig) throws ServletException{
            super(servletConfig);
            this.s = servletConfig.getInitParameter("myParam");
        }
    
        ....
    
    }   
    

    2) 配置文件

    您应该在您的应用程序中创建属性文件,在 src 文件夹中,例如 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();
                }
            }
        }
    }
    

    MyServlet.java

    public class MyServlet extends HttpServlet {
    
        protected Config config = new Config();
    
        @Override 
        public void doGet(request, response) throws ServletException{
    
            Strign s = config.getProperty("myParam");
    
        }
    
    }
    

    【讨论】:

    • 谢谢!我正在使用第一种方法:)
    【解决方案3】:

    使用这个:

    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
    }
    

    将您的数据加载到静态对象中并检查首次加载是否为空。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-20
      • 1970-01-01
      • 2012-12-10
      • 2011-01-23
      相关资源
      最近更新 更多