【问题标题】:Spring Environment Profiles & JPASpring 环境配置文件和 JPA
【发布时间】:2014-09-27 23:52:37
【问题描述】:

我想在我的 Spring JSON API 应用程序中实现环境配置文件以处理多个环境、开发、测试和生产。我见过像Unable to use JNDI DataSource provided by Tomcat in Spring? 这样的例子。此时我真的只需要处理不同的数据库设置,但稍后我可能需要根据环境进行额外的 bean 配置。

我的 persistence.xml 目前看起来像这样。

<?xml version="1.0" encoding="UTF-8"?>
<persistence version="2.1" 
    xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">

    <persistence-unit name="BPPersistenceUnit" transaction-type="RESOURCE_LOCAL">

        <!-- JNDI datasource -->
        <provider>org.hibernate.jpa.HibernatePersistenceProvider</provider>
        <non-jta-data-source>java:comp/env/jdbc/BloodPressureDB</non-jta-data-source>

        <!-- Hibernate Settings -->
        <properties>

            <!-- java persistence settings -->
            <property name="javax.persistence.jdbc.driver" value="org.postgresql.Driver" />
            <property name="javax.persistence.jdbc.url" value="jdbc:postgresql://127.0.0.1:5432/jkratz" />
            <property name="javax.persistence.jdbc.user" value="tomcat" />
            <property name="javax.persistence.jdbc.password" value="t0mc@t2014" />

            <!-- hibernate settings -->
            <property name="hibernate.show_sql" value="true" />
            <property name="hibernate.format_sql" value="true" />
            <property name="hibernate.connection.charSet" value="UTF-8" />
            <property name="hibernate.dialect" value="org.hibernate.dialect.PostgreSQL9Dialect" />
            <property name="hibernate.hbm2ddl.auto" value="validate" />

        </properties>

    </persistence-unit>
</persistence>

我见过不同的例子,所以我有点困惑如何实现。

  1. 您还能使用 Tomcat 数据源配置吗?如果是这样,它如何知道要使用哪个配置文件?

  2. 如何为多个环境配置 persistence.xml? jdbc 连接道具是否应该在 persistence.xml 中

  3. 如何在不重新编译的情况下设置活动配置文件?

【问题讨论】:

    标签: java spring tomcat jpa spring-profiles


    【解决方案1】:

    1.还可以使用 Tomcat 数据源配置吗?如果是这样,它如何知道要使用哪个配置文件?

    您可以使用 Tomcat 数据源配置,但是数据源定义与 Spring 或 Spring 配置文件无关。如果您使用的是 JNDI 方法,那么您可能定义了多个数据源,并且可以通过配置文件属性指定在您的应用程序中使用的数据源,但实际定义与 Spring 无关。

    或者,您可以使用非 JNDI 数据源,即在 Spring 中配置并且可能使用配置文件。

    2.如何为多个环境配置 persistence.xml? jdbc 连接道具是否应该在 persistence.xml 中

    不,在使用 Spring 时,您只需要一个最小的 persistence.xml,或者根本不需要。

    3.如何在不重新编译的情况下设置活动配置文件?

    你不能

    以下是使用非 JNDI 数据源启动和运行所需的全部内容。

    示例 Spring 配置(非 JNDI):

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:jpa="http://www.springframework.org/schema/data/jpa" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd http://www.springframework.org/schema/context 
              http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    
        <jpa:repositories base-package="uk.co.certait.spring.data.repository" />
    
        <bean id="entityManagerFactory"
            class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
            <property name="jpaVendorAdapter" ref="jpaAdapter"></property>
            <property name="persistenceUnitName" value="persistenceUnit" />
            <property name="dataSource" ref="dataSource" />
            <property name="jpaProperties">
                <props>
                    <prop key="hibernate.hbm2ddl.auto">${hibernate.ddl.auto}</prop>
                    <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                    <prop key="hibernate.show_sql">${hibernate.show.sql}</prop>
                    <prop key="hibernate.format_sql">true</prop>
                    <prop key="hibernate.cache.use_second_level_cache">${hibernate.enable.cache}</prop>
                    <prop key="hibernate.cache.provider_class">org.hibernate.cache.EhCacheProvider</prop>
                </props>
            </property>
        </bean>
    
        <bean id="jpaAdapter"
            class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter" />
    
        <bean class="org.springframework.orm.jpa.JpaTransactionManager"
            id="transactionManager">
            <property name="entityManagerFactory" ref="entityManagerFactory" />
        </bean>
    </beans>
    

    示例 Spring 数据源定义

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="
                http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    
        <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName">
                <value>${database.driver}</value>
            </property>
            <property name="url">
                <value>${database.url}</value>
            </property>
            <property name="username">
                <value>${database.username}</value>
            </property>
            <property name="password">
                <value>${database.password}</value>
            </property>
        </bean>
    
    </beans>
    

    示例 Spring 配置文件定义:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd">
    
        <beans profile="default">
            <bean id="applicationPropertiesPlaceholder"
                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="locations">
                    <list>
                        <value>classpath:profiles/hsqldb.profile.properties</value>
                    </list>
                </property>
            </bean>
        </beans>
    
        <beans profile="hsqldb">
            <bean id="applicationPropertiesPlaceholder"
                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="locations">
                    <list>
                        <value>classpath:profiles/hsqldb.profile.properties</value>
                    </list>
                </property>
            </bean>
        </beans>
    
        <beans profile="mysql">
            <bean id="applicationPropertiesPlaceholder"
                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="locations">
                    <list>
                        <value>classpath:profiles/mysql.profile.properties</value>
                    </list>
                </property>
            </bean>
        </beans>
    
        <beans profile="mssql">
            <bean id="applicationPropertiesPlaceholder"
                class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
                <property name="locations">
                    <list>
                        <value>classpath:profiles/mssql.profile.properties</value>
                    </list>
                </property>
            </bean>
        </beans>
    
    </beans>
    

    最小的 Persisteance.XML

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <persistence xmlns="http://java.sun.com/xml/ns/persistence"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.0"
        xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_2_0.xsd">
        <persistence-unit name="persistenceUnit"
            transaction-type="RESOURCE_LOCAL">
            <provider>org.hibernate.ejb.HibernatePersistence</provider>
            <properties>
                <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
                <property name="hibernate.connection.charSet" value="UTF-8" />
            </properties>
        </persistence-unit>
    </persistence>
    

    示例配置文件属性文件:

    #MySQL
    #database.url=jdbc:mysql://localhost:3306/test
    database.url=jdbc:log4jdbc:mysql://localhost:3306/test
    #database.driver=com.mysql.jdbc.Driver
    database.driver=net.sf.log4jdbc.DriverSpy
    database.username=root
    database.password=password
    hibernate.dialect=org.hibernate.dialect.MySQL5InnoDBDialect
    
    hibernate.show.sql=false
    hibernate.ddl.auto=create
    hibernate.enable.cache=false
    

    【讨论】:

    • 感谢您的回复和代码示例!关于 JNDI 与 Spring Datasource 的问题,在使用 JNDI 而不是非 JNDI 方面是否有任何权衡或缺点?
    【解决方案2】:
    1. 由于我与 Tomcat 没有太多共同之处,我将只提供一个类似问题的链接:
      Using dynamic Datasource with Tomcat
    2. JPA 允许在单个 persistence.xml 文件中定义多个持久性单元,因此 您至少需要将它们的名称更改为在其打包范围内是唯一的(就 Web 存档、EJB JAR 等而言):

      <?xml version="1.0" encoding="UTF-8"?>
      <persistence version="2.1" 
          xmlns="http://xmlns.jcp.org/xml/ns/persistence" 
          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/persistence 
          http://xmlns.jcp.org/xml/ns/persistence/persistence_2_1.xsd">
      
          <persistence-unit name="BPPersistenceUnit-postgres" 
                            transaction-type="RESOURCE_LOCAL">
              <!-- JNDI datasource -->
              ...
              <!-- properties -->
              ...
          </persistence-unit>
          <persistence-unit name="BPPersistenceUnit-mysql" 
                            transaction-type="RESOURCE_LOCAL">
              <!-- JNDI datasource -->
              ...
              <!-- properties -->
              ...
          </persistence-unit>
      </persistence>
      
    3. JPA 允许在运行时使用重载的createEntityManagerFactory 方法指定持久性提供程序属性:

      // dynamic configuration for PostgreSQL
      Map<String, String> props = new HashMap<>();
      props.put("javax.persistence.jdbc.driver", "org.postgresql.Driver");
      ...
      EntityManagerFactory pgEmf = 
      Persistence.createEntityManagerFactory("BPPersistenceUnit-postgres", props);
      
      // dynamic configuration for MySQL
      ...
      

      传递给方法的属性与persistence.xml中已经指定的属性相结合,因此您可以获得真正可配置、多用途和动态的配置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-02-18
      • 1970-01-01
      • 2020-07-18
      • 2019-12-03
      • 1970-01-01
      • 2016-01-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多