Java Spring和x2B的特定环境配置;Hibernate应用程序

Java Spring和x2B的特定环境配置;Hibernate应用程序,java,xml,spring,hibernate,maven,Java,Xml,Spring,Hibernate,Maven,我正在努力为hibernate使用不同的.properties文件来配置我的Spring应用程序。我的计划是创建一个maven属性,该属性将反映我希望通过概要文件选择的环境。然后在我的hibernate.cfg.xml上加载属性变量 文件:hibernate.cfg.xml: .... <property name="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</property> ... 文件:spring-context.xml

我正在努力为hibernate使用不同的.properties文件来配置我的Spring应用程序。我的计划是创建一个maven属性,该属性将反映我希望通过概要文件选择的环境。然后在我的hibernate.cfg.xml上加载属性变量

文件:hibernate.cfg.xml:

.... <property name="hbm2ddl.auto">${hibernate.hbm2ddl.auto}</property>  ...
文件:spring-context.xml:

... <context:property-placeholder location="classpath*:*persistence-${envTarget}.properties" /> ...
。。。

我这里缺少什么?

hibernate.cfg.xml不是由Spring管理的

试图使用
propertyplaceholderconfigure
(或其替代
propertysourcesplaceplaceconfigurer
)对其进行筛选是无用的

您可以使用Maven资源筛选支持:

<profiles>
    <profile>
        <id>dev</id>
        <properties>
            <hibernate.hbm2ddl.auto>dev</hibernate.hbm2ddl.auto>
        </properties>
    </profile>

    <profile>
        <id>prod</id>
        <properties>
            <hibernate.hbm2ddl.auto>validate</hibernate.hbm2ddl.auto>
        </properties>
    </profile>
</profiles>

<build>
    <resources>
        <resource>
            <directory>src/main/resources</directory>
            <filtering>true</filtering>
        </resource>
    </resources>
</build>

发展
发展
戳
验证
src/main/resources
真的
您还可以将其与Spring概要文件支持结合使用(假设您愿意在构建期间将Spring概要文件名称传递给Maven,而不仅仅是使用运行时,如果您从Maven运行Spring概要文件相关的测试,您无论如何都会这样做):


spring.profiles.active
发展
发展
...

我是通过以下方式实现的:

将所有环境的公共属性保存在
hibernate.cfg.xml
文件中,如下所示

 <hibernate-configuration>
        <session-factory>

            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>

            <!-- SQL dialect -->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>


            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>

            <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">update</property>

          <!-- DB mapping if any -->    
            <mapping class='classname'></mapping>


        </session-factory>
    </hibernate-configuration>
下面是放置在资源文件夹中的
local.properties
文件

#hibernate properties
connection.url=jdbc:mysql://localhost:3306/rivigo
connection.username=root
connection.password=root

希望有帮助

工作得很有魅力!非常感谢。现在,是否可以仅为一个特定类更改“hibernate.hbm2ddl.auto”属性?是否可以在配置文件之间共享属性?如果要在两个配置文件之间共享属性,最好在pom文件的
properties
部分声明它们。我不明白你说的更改特定类的属性是什么意思。
 <hibernate-configuration>
        <session-factory>

            <!-- JDBC connection pool (use the built-in) -->
            <property name="connection.pool_size">1</property>

            <!-- SQL dialect -->
            <property name="dialect">org.hibernate.dialect.MySQLDialect</property>

            <!-- Enable Hibernate's automatic session context management -->
            <property name="current_session_context_class">thread</property>


            <!-- Echo all executed SQL to stdout -->
            <property name="show_sql">true</property>

            <!-- Drop and re-create the database schema on startup -->
            <property name="hbm2ddl.auto">update</property>

          <!-- DB mapping if any -->    
            <mapping class='classname'></mapping>


        </session-factory>
    </hibernate-configuration>
import org.apache.commons.configuration2.FileBasedConfiguration;
import org.apache.commons.configuration2.PropertiesConfiguration;
import org.apache.commons.configuration2.builder.FileBasedConfigurationBuilder;
import org.apache.commons.configuration2.builder.fluent.Parameters;
import org.apache.commons.configuration2.ex.ConfigurationException;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.Configuration;


public class HibernateUtil {

    static private org.apache.commons.configuration2.Configuration loadTestData;
    private static final SessionFactory sessionFactory;

    public HibernateUtil(){
         loadTestData = loadPropertiesFile("local");
    }

    static {
        try {

            Configuration cfg = new Configuration();
            cfg.setProperty("hibernate.connection.url", loadTestData.getString("connection.url"));
            cfg.setProperty("hibernate.connection.username", loadTestData.getString("connection.username"));
            cfg.setProperty("hibernate.connection.password", loadTestData.getString("connection.password"));
            cfg.setProperty("hibernate.dialect", "org.hibernate.dialect.MySQLDialect");
            cfg.configure();

            sessionFactory = cfg.buildSessionFactory();
        } catch (Throwable ex) {
            System.err.println("SessionFactory creation failed" + ex);
            throw new ExceptionInInitializerError(ex);
        }
    }

    public static SessionFactory getSessionFactory() {
        return sessionFactory;
    }

    public static org.apache.commons.configuration2.Configuration loadPropertiesFile(String filename) {
        Parameters params = new Parameters();
        FileBasedConfigurationBuilder<FileBasedConfiguration> builder =
                new FileBasedConfigurationBuilder<FileBasedConfiguration>(PropertiesConfiguration.class)
                        .configure(params.properties()
                                .setFileName(filename + ".properties"));
        try {
            org.apache.commons.configuration2.Configuration config = builder.getConfiguration();
            return config;
        } catch (ConfigurationException cex) {
            // loading of the configuration file failed
            System.out.println(filename + " is not loaded" + cex.getMessage());
            return null;
        }

    }
}
#hibernate properties
connection.url=jdbc:mysql://localhost:3306/rivigo
connection.username=root
connection.password=root