【问题标题】:Reading properties file from $TOMCAT_HOME\lib从 $TOMCAT_HOME\lib 读取属性文件
【发布时间】:2016-09-26 05:01:30
【问题描述】:

我们有一个使用 Tomcat 服务器的小型 Web 应用程序。所有数据库详细信息都在属性文件中作为键值对供hibernate.cfg.xml 读取。

为了使服务器团队易于管理,属性文件需要转到$TOMCAT_HOME/lib 文件夹。 Hibernate 配置文件位于:APP_HOME\src\main\resources\hibernate.cfg.xml
它可以被应用程序类访问。
休眠配置文件似乎没有读取 $TOMCAT_HOME/lib/myapp.properties
我的主要要求是属性文件应该驻留在 $TOMCAT_HOME/lib 中。
谁能建议我做正确的事情。

提前谢谢..!

【问题讨论】:

    标签: java hibernate tomcat7


    【解决方案1】:

    你可以试试这个来获取属性文件的路径:

    String propertyFilePathStr= System.getProperty("catalina.base") +
                      File.separator + "lib"+ File.separator + "myapp.properties";
    

    然后通过以下方式配置 Hibernate 以使用此配置文件:

    Configuration config = new Configuration().configure(propertyFilePathStr);
    

    但是,这并不是说这仅适用于 Tomcat,如果您使用的是 Glassfish 等其他服务器,则需要进行更改。

    【讨论】:

      【解决方案2】:

      对于 tomcat 可以使用这个来完成:

      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);
      

      【讨论】:

        【解决方案3】:

        感谢 V33R 和 Chetan。你的两位cmets给了我一个方向。 我使用了系统属性

        catalina.home

        而不是 catalina.base 没有指向我需要的正确位置。 以下是我最终所做的:

            private static Properties extractPropertiesFile() throws FileNotFoundException, IOException{
        
            String propertiesFilePath = System.getProperty("catalina.home") +
                    File.separator + "lib"+ File.separator + "myapp.properties";
        
            Properties properties = new Properties();
            properties.load(new FileInputStream(propertiesFilePath));
        
            return properties;
        }
        

        我必须将在 hibernate.cfg.xml 文件中设置的数据库属性删除到以下方法。我尝试加载提取的属性文件,但没有按预期工作,因此将它们加载到 Hibernate.cfg.Configuration 实例中,如下所示。 构建会话工厂:

         private static SessionFactory buildSessionFactory() {
        
                try {
                    Configuration configuration = new Configuration();
                    Properties properties = extractPropertiesFile();
        
                    configuration.configure("hibernate.cfg.xml").setProperties(properties);
        
                    // The properties below are extracted from $TOMCAT_HOME/lib/myapp.properties
                    configuration.setProperty("hibernate.connection.driver_class", properties.getProperty("propertyfile.database.driver.classname"));
                    serviceRegistry = new ServiceRegistryBuilder().applySettings(
                            configuration.getProperties()). buildServiceRegistry();
                    sessionFactory = configuration.buildSessionFactory(serviceRegistry);
                } catch (HibernateException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (FileNotFoundException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        

        获取SessionFactory的公共方法

            public static SessionFactory getSessionFactory() {
        
            if(sessionFactory == null){
                sessionFactory = buildSessionFactory();
            }
            return sessionFactory;
        }
        

        【讨论】:

        • 酷.. 还好你发布了你最终为解决这个问题所做的事情,以便其他人可以从中受益。但是,如果您要运行两个 tomcat 实例,则应使用 catalina.base,否则 catalina.home 应该没问题..
        • 我不确定是否可以使用属性“catalina.base”访问带有 $TOMCAT_HOME/lib 的文件,因为它指向:workspace\.metadata\.plugins\org.eclipse.wst。 server.core\tmp0.从这里我看不到对 lib 文件夹的任何引用。
        • 可能是您在 Eclipse 中配置服务器的方式。对我来说,catalina.basecatalina.home 都指向同一个位置,C:\Program Files\Apache Software Foundation\Tomcat 7.0. 无论如何,既然你的代码正在运行,是的!
        • 有趣..!除了通过 Eclipse Server 控制台修改默认端口号之外,我还没有对 tomcat 安装做任何事情。不过,我在 Eclipse 上配置了多个 Tomcat。你对你的tomcat设置做过什么吗?你有多个tomcat吗?想知道为什么 'cataline.home' 和 'catalina.base' 在你的情况下是一样的。
        • 我使用MyEclipse(与eclipse非常相似)。除了像你通常做的那样添加一个服务器,将它指向 Apache 的位置,就是这样。我只是在我的机器上安装了 tomcat。
        猜你喜欢
        • 1970-01-01
        • 2023-03-20
        • 2015-05-11
        • 1970-01-01
        • 1970-01-01
        • 2020-11-11
        • 1970-01-01
        • 1970-01-01
        • 2019-12-09
        相关资源
        最近更新 更多