2009-12-02 22 views
28

有没有办法在spring-context.xml和JPA persistence.xml中引用.properties文件?在spring-context.xml和persistence.xml中加载.properties

我想我已经在某个地方看到了一个这样的例子,在春天的上下文中,虽然我不记得那是哪里。也许有人知道这一点? 关于persistence.xml我实际上不确定这是否可行。

我的目标是改变开发和分配配置之间的一些属性。 我目前的想法是在模板配置中通过ant手动替换文件中的所有属性。虽然应该有更好的方法来做到这一点。 :)

回答

16

您可以使用PropertyPlaceholderConfigurer从Spring bean定义文件引用外部属性文件。尽管Spring的JPA支持允许你将大部分(如果不是全部的话)persistence.xml的内容合并到bean文件本身,但在这种情况下,它可以正常工作,但我认为这不会用于JPA persistence.xml。

+1

感谢,对我的解决方案我现在使用context.xml中的PropertyPlaceholderConfigurer,并使用jdbc.properties作为数据源。 我将两个文件中的hibernate东西都移到了hibernate.properties中。这两个文件被替换为用于分发和开发的不同版本。 hibernate.properties似乎在运行时被正确检测到,虽然hibernatetool ant任务必须被告知文件在哪里,即使它在类路径中也是如此。 现在所有人都在工作。 :) – subes

38

而不是使用您的构建来创建您的persistence.xml的prod或dev版本,只需将所有属性设置 移动到您的spring内容。

我的persistence.xml是

<?xml version="1.0" encoding="UTF-8"?> 
<persistence 
    xmlns="http://java.sun.com/xml/ns/persistence" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" 
    version="1.0"> 
    <persistence-unit name="JPAService" transaction-type="RESOURCE_LOCAL"> 
    </persistence-unit> 
</persistence> 

在我的春天内容,然后我用PropertyPlaceholderConfigurer阅读开发/生产属性值,并设置这些进入 的entityManagerFactory豆

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xsi:schemaLocation=" 
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"> 

    <bean class="org.springframework.context.annotation.CommonAnnotationBeanPostProcessor"/> 
    <bean class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor"/>  
    <bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/> 
    <bean class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"/> 

    <bean id="propertyPlaceholder" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
     <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/> 
     <property name="ignoreResourceNotFound" value="true"/> 
     <property name="locations"> 
      <list> 
       <value>classpath:dev.properties</value> 
      </list> 
     </property> 
    </bean> 

    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
     <property name="driverClassName" value="${datasource.driverClassName}"/> 
     <property name="url" value="${datasource.url}"/> 
     <property name="username" value="${datasource.username}"/> 
     <property name="password" value="${datasource.password}"/> 
    </bean> 

    <bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> 
     <property name="persistenceXmlLocation" value="classpath:./META-INF/persistence.xml"/> 
     <property name="persistenceUnitName" value="JPAService"/> 
     <property name="dataSource" ref="dataSource"/> 

     <property name="jpaVendorAdapter"> 
      <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"> 
       <property name="databasePlatform" value="org.hibernate.dialect.OracleDialect"/> 
       <property name="showSql" value="true" /> 
       <property name="generateDdl" value="true"/> 
      </bean> 
     </property> 
     <property name="jpaProperties"> 
       <!-- set extra properties here, e.g. for Hibernate: --> 
      <props> 
      <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop> 
      </props> 
     </property> 
    </bean> 

    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"> 
     <property name="entityManagerFactory" ref="entityManagerFactory"/> 
     <property name="dataSource" ref="dataSource"/> 
    </bean> 

    <tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="false"/> 
</beans> 
+2

+1正是我正在寻找。谢谢! – andyb

+2

用户可能需要在使用此示例之前进行一点研究。从DriverManagerDataSource的JavaDoc:“注意:这个类不是实际的连接池,它实际上并不连接连接,它只是简单地替换成熟的连接池,实现相同的标准接口,但在每个连接池上创建新的连接呼叫。” – spaaarky21

相关问题