【问题标题】:How to inject/resolve properties into an XML file with Guice to configure the persistence.xml file?如何使用 Guice 将属性注入/解析到 XML 文件中以配置 persistence.xml 文件?
【发布时间】:2011-11-26 13:20:05
【问题描述】:

我正在使用 guice-persist,我想知道是否有任何可以将 Java 属性文件中的属性注入或解析到 persistence.xml 文件中,就像我对 Spring 所做的那样。例如:

<?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_2_0.xsd"
    version="2.0">

    <persistence-unit name="WoloxShivaJPAUnit" transaction-type="RESOURCE_LOCAL">
        <provider>org.hibernate.ejb.HibernatePersistence</provider>

        <class>com.guidomb.MyClass</class>

        <shared-cache-mode>ENABLE_SELECTIVE</shared-cache-mode>
        <properties>
            <property name="hibernate.connection.driver_class" value="${hibernate.connection.driver_class}">
            <property name="hibernate.connection.url" value="${hibernate.connection.url}">
            <property name="hibernate.connection.username" value="${hibernate.connection.username}">
            <property name="hibernate.connection.password" value="${hibernate.connection.password}">
            <property name="hibernate.dialect" value="${hibernate.dialect}">
            <property name="hibernate.id.new_generator_mappings" value="true">
            <property name="hibernate.ejb.naming_strategy" value="org.hibernate.cfg.ImprovedNamingStrategy" />
        </properties>

    </persistence-unit>
</persistence>

如果不是,我如何使用 guice-persist 库配置 EntityManager 以便从属性文件中注入这些属性?。

谢谢!

【问题讨论】:

    标签: hibernate configuration guice guice-persist


    【解决方案1】:

    看起来你可以在创建JpaPersistModule时提供额外的属性:

    JpaPersistModule jpa = new JpaPersistModule("myFirstJpaUnit");
    jpa.properties(...);
    Injector injector = Guice.createInjector(..., jpa);
    

    【讨论】:

      猜你喜欢
      • 2016-03-21
      • 1970-01-01
      • 2013-11-15
      • 2017-02-13
      • 1970-01-01
      • 2014-02-18
      • 1970-01-01
      • 1970-01-01
      • 2012-02-21
      相关资源
      最近更新 更多