Java 应用服务器和本地Eclipse的Hibernate外部配置设计

Java 应用服务器和本地Eclipse的Hibernate外部配置设计,java,hibernate,spring-mvc,configuration,Java,Hibernate,Spring Mvc,Configuration,我在这里找到了许多关于使用Spring外部化Hibernate设置的帖子,因此它不在Hibernate.cfg.xmlconfig文件中。然而,我的问题是,拥有一个使用Eclipse的开发环境,然后将生成的WAR文件部署到单独的服务器上 我目前正在使用EclipseLuna,并将我的SpringMVCWeb应用程序部署到Tomcat8的本地安装中。hibernate.cfg.xml文件当前位于resources文件夹中。当我在本地部署它时,效果非常好。但是,配置使用我的个人登录凭据,这不利于在服

我在这里找到了许多关于使用Spring外部化Hibernate设置的帖子,因此它不在
Hibernate.cfg.xml
config文件中。然而,我的问题是,拥有一个使用Eclipse的开发环境,然后将生成的WAR文件部署到单独的服务器上

我目前正在使用EclipseLuna,并将我的SpringMVCWeb应用程序部署到Tomcat8的本地安装中。
hibernate.cfg.xml
文件当前位于resources文件夹中。当我在本地部署它时,效果非常好。但是,配置使用我的个人登录凭据,这不利于在服务器上让每个人都看到

我需要的是找到一种设计,让应用程序在本地工作,并在服务器上部署

我正在考虑创建一个名为conf的文件夹,并将
hibernate.cfg.xml
文件放在这里,然后通过Eclipse类路径(Debug As configuration…)将其定义为文件夹。这应该允许本地版本仍然读取正确的文件并工作。但是我被困在如何设置服务器端。因为我们只需输入WAR文件,它将自动部署。我可以使用
属性PlaceHolderConfigure
,但这会在本地副本上引发错误

我唯一能想到的另一件事是创建一个进程,在这个进程中,我为应用程序创建文件夹,每次将WAR文件解压缩到这个文件夹中,减去conf文件夹。并且只更新服务器上的conf文件夹


这是一个好的设计还是有更好的方法?我感谢所有在这方面的帮助和指导

这里有一个方法。首先,您需要使用JNDI访问数据源。这里有一些方法,你可能会找到更多

需要做的一件事是将数据源作为tomcat上下文中的资源。诀窍是在eclipse和生产环境中为本地部署提供支持。 在本地部署包
context.xml
中,作为
META-INF
文件夹中war的一部分,但在生产部署中,在
$CATALINA_BASE/conf/[engineame]/[hostname]/
中创建
context.xml
,这将覆盖war中提供的
context.xml


另一个选项是使用
propertyplaceholderconfigure
和覆盖环境变量设置的选项。这将要求您在tomcat服务器启动配置的环境变量中指定数据库凭据。

我成功地实现了这一点,并想与大家分享一下我是如何做到的

我继续在根文件夹中创建了“conf”文件夹。在这里,我放置了一个
hibernate.cfg.xml
文件、
log4j.properties
messages.properties
文件。然后我创建了一个名为“db”的子文件夹,并将
db.properties
文件放在其中

因此,结构是:

ApplicationName
--> conf
    --> db
        db.properties
    hibernate.cfg.xml
    log4j.properties
    messages.properties
db.properties
在这个文件中,我放置了hibernate连接信息(和其他信息)

*-servlet.xml文件中:

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="locations">  
        <list>  
            <value>classpath:db/db.properties</value>  
        </list>  
    </property>  
    <property name="ignoreResourceNotFound" value="false"/>
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="cacheSeconds" value="60"/> 
</bean>

类路径:db/db.properties
在Eclipse中,当我要运行应用程序时,我必须首先转到“Debug As…=>Debug Configuration…”。在Classpath下,我将conf文件夹添加到user entries部分下

Eclipse就是这样。当它运行时,它将从conf文件夹中拉入配置并正常工作

TOMCAT
为了在服务器上运行,我首先创建了一个文件夹来存储conf的内容。我创建了文件夹
/app/properties
。我从SVN签出了该项目,并将conf文件夹的内容复制到新创建的文件夹中

最后,我进入了
$TOMCAT_HOME/conf/catalina.properties
,通过将新文件夹添加到
shared.loader
属性,将其添加到类路径中

就这样!部署WAR时,它会从
/app/properties
位置提取信息,应用程序就会工作

如果需要更多信息,请告诉我

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE hibernate-configuration PUBLIC
  "-//Hibernate/Hibernate Configuration DTD//EN"
  "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">

<hibernate-configuration>
    <session-factory>
        <!-- Entity mapping -->
        <mapping class="com.mine.Entity1"></mapping>
        <mapping class="com.mine.Entity2"></mapping>
    </session-factory>
</hibernate-configuration>
Configuration configuration = new Configuration();
configuration.configure();

PropertyResourceBundle prop = (PropertyResourceBundle) ResourceBundle.getBundle("db/db");

// Basic connection information
configuration.setProperty("hibernate.connection.username", prop.getString("db.username"));
configuration.setProperty("hibernate.connection.password", prop.getString("db.password"));
configuration.setProperty("hibernate.connection.url", prop.getString("db.url"));
configuration.setProperty("hibernate.connection.driver_class", prop.getString("db.driver"));
configuration.setProperty("hibernate.dialect", prop.getString("db.dialect"));
configuration.setProperty("hibernate.default_schema", prop.getString("db.default_schema"));

configuration.setProperty("hibernate.current_session_context_class", prop.getString("hibernate.session_context"));

// Handling SQL statements in the logs
configuration.setProperty("show_sql", prop.getString("hibernate.show_sql"));
configuration.setProperty("format_sql", prop.getString("hibernate.format_sql"));
configuration.setProperty("use_sql_comments", prop.getString("hibernate.use_sql_comments"));

// C3P0 Settings
configuration.setProperty("hibernate.c3p0.min_size", prop.getString("hibernate.c3p0.min_size"));
configuration.setProperty("hibernate.c3p0.max_size", prop.getString("hibernate.c3p0.max_size"));
configuration.setProperty("hibernate.c3p0.timeout", prop.getString("hibernate.c3p0.timeout"));
configuration.setProperty("hibernate.c3p0.max_statements", prop.getString("hibernate.c3p0.max_statements"));
configuration.setProperty("hibernate.c3p0.idle_test_period", prop.getString("hibernate.c3p0.idle_test_period"));
configuration.setProperty("hibernate.c3p0.preferredTestQuery", prop.getString("hibernate.c3p0.preferredTestQuery"));
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
    <property name="locations">  
        <list>  
            <value>classpath:db/db.properties</value>  
        </list>  
    </property>  
    <property name="ignoreResourceNotFound" value="false"/>
</bean>

<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:messages" />
    <property name="cacheSeconds" value="60"/> 
</bean>