2012-01-13 16 views
2

私は春のPropertyPlaceholderConfigurer beanをspringのxmlファイルに使用することができます。これは、指定されたプロパティファイルを読み込んでxmlファイルの値を使用します。賢明には、persistence.xmlファイルでこのメカニズムを使用する方法があります。persistence.xmlファイル内のPropertyPlaceholderConfigurerを読む

このようなデータソースBeanにorg.eclipse.persistence.jpa.PersistenceProviderを春xmlファイルで使用できますか?

<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> 

ありがとうございました。

+0

この[SO質問] [1] [1]を参照してください。 stackoverflow.com/questions/1834954/loading-properties-in-spring-context-xml-and-persistence-xml – Pete

+0

@Pete:コードがaoveに投稿されていることを確認してください。 – Ran

答えて

0

ことにより、元の記事を読んで、第二部については、このSO question

をご確認ください。別のdatasource.xmlファイルを使用してアプリケーションのコンテキストにインポートして、モジュール性を高めます。

春-のcontext.xml:

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

datasource.xmlます。http://

<?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> 
+0

By私のコードで2番目の方法を使用して、私は例外の下になっています。原因:java.lang.IllegalStateException:LoadTimeWeaverが指定されていないクラストランスを適用できません。 ./メタ:私のxmlファイルは\t <豆ID = "のEntityManager" \t \tクラス= "org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"> \t \t <プロパティ名= "persistenceXmlLocation" 値= "クラスパス 'として宣言しますINF/persistence.xmlの "/> <プロパティ名=" てpersistenceUnitName」値= "テスト" /> <プロパティ名= "データソース" REF = "データソース" /> \t ' – Ran

+0

これを追加してください: ' ' – Pete

+0

Iこれも同様に、以下のようになりました: '原因:java.lang.IllegalStateException:InstrumentationLoadTimeWeaverを使用するには、Javaエージェントで開始する必要があります。 '' Spring documentation ''を参照してください。 – Ran

0

ビルドを使用してpersistd.xmlのprodまたはdevバージョンを作成するのではなく、すべてのプロパティ設定をバネコンテンツに移動するだけです。はい、それは動作します:

が、私は私のコメントで言ったように、最初の部分ができないemeraldjava loading .properties in spring-context.xml and persistence.xml

+0

ポストされたコードを確認してください。 – Ran

+0

sprngのドキュメントを参照してくださいhttp://static.springsource.org/spring/docs/2.0.x/reference/orm.html#orm-jpa-multiple-pu –

+0

@Hemanth:以下の例外が発生する使用はありません:原因: java.lang.IllegalStateException:[java.lang.String]型の値をプロパティ 'persistenceUnitManager'の必須型[org.springframework.orm.jpa.persistenceunit.PersistenceUnitManager]に変換できません:一致するエディタまたは変換方式が見つかりませんでした ' – Ran

関連する問題