【问题标题】:loading .properties in spring-context.xml and persistence.xml在 spring-context.xml 和 persistence.xml 中加载 .properties
【发布时间】:2010-12-22 12:57:53
【问题描述】:

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

我想我在 spring 上下文文件中的某个地方看到过这样的例子,虽然我不记得那是在哪里。也许有人知道这个? 关于persistence.xml,我实际上不确定这是否有效。

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

【问题讨论】:

    标签: java xml spring jpa properties


    【解决方案1】:

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

    我的 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 读取 dev/prod 属性值并将它们设置到 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>
    

    【讨论】:

    • 用户在按原样使用此示例之前可能需要做一些研究。来自 DriverManagerDataSource JavaDoc:“注意:这个类不是一个实际的连接池;它实际上并不池连接。它只是作为一个完整连接池的简单替代品,实现相同的标准接口,但在每个连接池上创建新的连接打电话。”
    【解决方案2】:

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

    【讨论】:

    • 谢谢,对于我的解决方案,我现在使用 context.xml 中的 PropertyPlaceholderConfigurer 和 jdbc.properties 作为数据源。我将hibernate的东西从两个文件中移到了hibernate.properties中。这两个文件被替换为用于分发和开发的不同版本。 hibernate.properties 似乎在运行时被正确检测到,尽管 hibernatetool ant 任务必须被告知文件在哪里,即使它在类路径中。所以现在一切正常。 :)
    猜你喜欢
    • 2017-05-04
    • 1970-01-01
    • 2010-09-30
    • 2020-12-02
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    • 1970-01-01
    • 2013-08-09
    相关资源
    最近更新 更多