在spring-context.xml和persistence.xml中加载.properties文件

30

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

我记得在Spring上下文文件中看到过这种示例,不过我忘记了具体位置。也许有人知道吗?至于persistence.xml,我其实不确定它是否有效。

我的目标是在开发和分发配置之间更改一些属性。 我目前的想法是通过使用模板配置从ant手动替换文件中的所有属性。虽然肯定有更好的方法来完成这个任务。 :)

2个回答

39

与其使用构建来创建持久化.xml文件的生产版本或开发版本,不如将所有属性设置移动到您的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>
在我的Spring内容中,我使用 PropertyPlaceholderConfigurer 读取开发/生产属性值,并将这些值设置到 entityManagerFactory bean 中。
<?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
用户在使用此示例之前可能需要进行一些研究。根据DriverManagerDataSource JavaDoc的说明:“注意:此类不是实际的连接池;它实际上并不汇集连接。它只是作为完整连接池的简单替代品,实现相同的标准接口,但在每次调用时创建新的连接。” - spaaarky21

17

您可以使用PropertyPlaceholderConfigurer从 Spring bean 定义文件中引用外部属性文件。我认为这种方法可能无法用于 JPA persistence.xml,不过如果使用 Spring 的 JPA 支持,您可以将 persistence.xml 中的大部分或所有内容合并到 bean 文件本身中,在这种情况下,它将能够正常工作。


1
谢谢,对于我的解决方案,我现在在context.xml中使用了PropertyPlaceholderConfigurer,并使用jdbc.properties作为数据源。 我将hibernate相关的内容从两个文件中移出,并放入一个hibernate.properties文件中。这两个文件会因分发和开发而被替换成不同的版本。 hibernate.properties似乎在运行时被正确地检测到,尽管即使它在类路径中,hibernatetool ant任务也必须告诉它文件在哪里。 所以现在一切都正常工作。 :) - subes

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接