【问题标题】:Read PropertyPlaceholderConfigurer in persistence.xml file在 persistence.xml 文件中读取 PropertyPlaceholderConfigurer
【发布时间】:2012-02-09 10:44:41
【问题描述】:

我知道我们可以在 spring xml 文件中使用 spring 的 PropertyPlaceholderConfigurer bean,它读取指定的属性文件并使用 xml 文件中的值。同样明智的是,我们可以在我的 persistence.xml 文件中使用这种机制。

我可以在 spring xml 文件中像这样在数据源 bean 中使用 org.eclipse.persistence.jpa.PersistenceProvider 吗?

    <bean id="dataSource"
    class="org.eclipse.persistence.jpa.PersistenceProvider">
    <property name="javax.persistence.jdbc.driver" value="${datasource.driverClassName}" />
    <property name="javax.persistence.jdbc.url" value="${datasource.url}" />
    <property name="javax.persistence.jdbc.user" value="${datasource.username}" />
    <property name="javax.persistence.jdbc.password" value="${datasource.password}" />
</bean>

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

</bean>

提前致谢。

【问题讨论】:

标签: java spring spring-mvc persistence


【解决方案1】:

就像我在评论中所说的,第一部分是不可能的,请查看SO question

关于第二部分:是的,那行得通。我们使用单独的 datasource.xml 文件并将其导入应用程序上下文以实现更好的模块化。

spring-context.xml:

<import resource="classpath:datasouce.xml" />

datasource.xml:

<?xml version="1.0" encoding="UTF-8" ?>

<beans xmlns="http://www.springframework.org/schema/beans" 
    xmlns:aop="http://www.springframework.org/schema/aop" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xmlns:jee="http://www.springframework.org/schema/jee" 
    xmlns:tx="http://www.springframework.org/schema/tx" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd   
                        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
                        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
                        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd  
                        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd   
                        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"
    default-autowire="byName">

    <bean id="myDatasource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="username" value="..." />
        <property name="password" value="..." />
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/myTestDB" />
    </bean>

    <bean id="entityManagerFactory"
        class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
        <property name="dataSource" ref="myDatasource"/>
        <property name="jpaVendorAdapter">
            <bean class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
               <property name="showSql" value="true" />
               <property name="generateDdl" value="true" />
               <property name="databasePlatform" value="org.hibernate.dialect.MySQL5InnoDBDialect" />
            </bean>
        </property>
    </bean>

    <tx:annotation-driven transaction-manager="transactionManager"/>
    <bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager" />

</beans>

【讨论】:

  • 通过在我的代码中使用第二种方式,我得到低于异常 Caused by: java.lang.IllegalStateException: Cannot apply class transformer without LoadTimeWeaver specified 。我的 xml 文件声明为` `
  • 尝试添加这个:&lt;property name="loadTimeWeaver"&gt; &lt;bean class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver"/ &lt;/property&gt;
  • 我也这样做了,现在我得到了以下信息:Caused by: java.lang.IllegalStateException: Must start with Java agent to use InstrumentationLoadTimeWeaver. See Spring documentation.
  • 嗨皮特,通过添加下面的类它可以工作class="org.springframework.instrument.classloading.SimpleLoadTimeWeave 但究竟是什么 loadTimeWeaver
  • 嗯...不能说太多,你必须谷歌它;)很高兴知道它有效。作为参考,我编辑了我的帖子并包含了我们完整的datasource.xml,我们将其导入我们的spring-context.xml,就是这样。
【解决方案2】:

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

阅读emeraldjava loading .properties in spring-context.xml and persistence.xml的原帖

【讨论】:

  • @Hemanth :没有用 iam 低于异常:Caused by: java.lang.IllegalStateException: Cannot convert value of type [java.lang.String] to required type [org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager] for property 'persistenceUnitManager': no matching editors or conversion strategy found
猜你喜欢
  • 2012-02-08
  • 2011-03-10
  • 1970-01-01
  • 2011-04-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
  • 2020-01-26
相关资源
最近更新 更多