2014-09-27 61 views
0

我想在我的Spring JSON API應用程序中實現環境配置文件以處理多個環境,開發,測試和生產。我見過例如Unable to use JNDI DataSource provided by Tomcat in Spring?。我現在只需要處理不同的數據庫設置,但後來我可能需要額外的bean配置,具體取決於環境。Spring環境配置文件和JPA

我的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="[email protected]" /> 

      <!-- 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. 如何設置活動配置文件而不重新編譯?

回答

2

1.您仍然可以使用Tomcat數據源配置嗎?如果是這樣,它如何知道使用哪個配置文件?

您可以使用Tomcat datasourse配置,但是數據源定義與Spring或Spring配置文件無關。如果您使用的是JNDI方法,那麼您可能會定義多個數據源,並可以通過配置文件屬性指定在您的應用程序中使用的數據源,但實際定義與Spring無關。或者,您可以使用非JNDI數據源,即在Spring中配置並可能使用配置文件。

2.如何爲多個環境配置persistence.xml?如果jdbc連接道具甚至在persistence.xml中

不,你只需要一個最小的persistence.xml使用Spring或者根本不需要。

3。如何設置活動配置文件而不重新編譯?

不能

以下是所有你需要起牀和與非JNDI數據源運行。

實施例彈簧配置(非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> 

實施例彈簧的數據源定義

<?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> 

示例性彈簧配置文件定義:

<?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 
+0

感謝您的答覆和代碼示例!關於JNDI與Spring Datasource相關的問題,是否有任何折衷或退步,以使用非JNDI使用JNDI? – greyfox 2014-09-29 15:36:01

1
  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中已作規定的聯合所以這樣你可以得到真正的配置,多用途和動態配置。