Hibernate - Prod vs Dev

时间:2014-09-01 08:30:53

标签: java database hibernate development-environment production-environment

在我的应用程序中,我使用命令行jvm参数来区分我是在生产中还是在开发中运行。基于此,我继续使用相关的配置/属性文件来访问不同的数据库等。

使用hibernate,我们有hibernate.cfg.xml文件,其属性为

<property name="hibernate.connection.url">url</property>

我们如何区分两个不同的网址,以避免在开发中连接到生产,反之亦然。

谢谢

3 个答案:

答案 0 :(得分:0)

我还建议使用数据源(JNDI)或Make两个不同的MySQL端口号来区分Prod / Dev环境。

答案 1 :(得分:0)

正如已有人提到的,正确的方法是在应用程序服务器中定义数据源并通过JNDI名称公开它们。

如果以正确的方式选择并且您正在使用Maven,则可以通过maven资源过滤获得解决方法。

查看规范:here

答案 2 :(得分:0)

有一种方法可以为你提供这个例子;

在WEB-INF中添加名称为类的文件名。

WEB-INF-类 - configDevelopment

 - -config.properties                        
 - -log4j.xml
 - -theme-html5.properties

WEB-INF-类 - configProduction

 - config.properties
 - log4j.xml
 - theme-html5.properties

test.properties for test

connection.jndiName=jdbc/test
user.loginURI=user/login

log4j.xml for test

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration PUBLIC "-//APACHE//DTD LOG4J 1.2//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <!-- Appenders -->

    <!-- Console Appender -->
    <appender name="console" class="org.apache.log4j.ConsoleAppender">
        <param name="Target" value="System.out" />
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%t %-5p: %c - %m%n" />
        </layout>
    </appender> 

    <!-- Application Loggers -->

    <!-- 3rdparty Loggers -->

    <logger name="SPGW">
        <level value="debug" />
    </logger>

    <logger name="org.apache">
        <level value="error" />
    </logger>

    <logger name="org.springframework">
        <level value="error" />
    </logger>

    <logger name="org.hibernate">
        <level value="error" />
    </logger>

    <!-- Root Logger -->
    <root>
        <priority value="debug" />
        <appender-ref ref="console" />
    </root>

</log4j:configuration>

根context.xml中

 <!-- APP CONFIGURER -->
        <bean id="appConfigurer" 
            class="YOURPROJECT.PropertyPlaceholderExposer">
            <property name="ignoreResourceNotFound" value="true" />
            <property name="ignoreUnresolvablePlaceholders" value="true" />   
            <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />     
            <property name="locations">
                <list>
                    <value>classpath:configDevelopment/config.properties</value>
                </list>
            </property>
        </bean>

        <!-- LOG4J -->
        <bean id="log4jInitializer" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
            <property name="staticMethod">
                <value>org.springframework.util.Log4jConfigurer.initLogging</value>
            </property>
            <property name="arguments">
                <list>
                    <value>classpath:configDevelopment/log4j.xml</value>
                </list>
            </property>
        </bean>

<!-- DATA SOURCE -->
    <bean id="_dataSourceProxy"
        class="org.springframework.jndi.JndiObjectFactoryBean">
       <property name="resourceRef" value="true" />
       <property name="jndiName" value="${connection.jndiName}"></property>
       <property name="lookupOnStartup" value="false"></property>
       <property name="cache" value="false"></property>
       <property name="proxyInterface" value="javax.sql.DataSource"></property>
    </bean>

PropertyPlaceholderExposer.java

package YOURPROJECT;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.util.StringValueResolver;

public class PropertyPlaceholderExposer extends PropertyPlaceholderConfigurer implements Map<String, String> {

    Map<String, String> props = new HashMap<String, String>();

    @Override
    protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props)
            throws BeansException {

        this.props.clear();
        for (Entry<Object, Object> e: props.entrySet())
            this.props.put(e.getKey().toString(), e.getValue().toString());

        super.processProperties(beanFactory, props);
    }

    @Override
    protected void doProcessProperties(ConfigurableListableBeanFactory beanFactoryToProcess,
            StringValueResolver valueResolver) {

        super.doProcessProperties(beanFactoryToProcess, valueResolver);

        for(Entry<String, String> e: props.entrySet())
            e.setValue(valueResolver.resolveStringValue(e.getValue()));
    }

    // Implement map interface to access stored properties
    @Override public Set<String> keySet() { return props.keySet(); }
    @Override public Set<java.util.Map.Entry<String, String>> entrySet() { return props.entrySet(); }
    @Override public Collection<String> values() { return props.values(); }
    @Override public int size() { return props.size(); }
    @Override public boolean isEmpty() { return props.isEmpty(); }
    @Override public boolean containsValue(Object value) { return props.containsValue(value); }
    @Override public boolean containsKey(Object key) { return props.containsKey(key); }
    @Override public String get(Object key) { return props.get(key); }
    @Override public void clear() { throw new UnsupportedOperationException(); }
    @Override public String put(String key, String value) { throw new UnsupportedOperationException(); }
    @Override public String remove(Object key) { throw new UnsupportedOperationException(); }
    @Override public void putAll(Map<? extends String, ? extends String> t) { throw new UnsupportedOperationException(); }

}