0

applicationContext.xml次のファイルが何をしているのか誰か説明してくれませんか? より詳細なコンテキストについては、http://persistentdesigns.com/wp/jersey-spring-and-jpa/から取得します。

私の質問のいくつか(私はあまり理解していないので、網羅的ではありません):

  • Forid="dataSource"dataSourceキーワードですか、それとも使用するデータソースの名前ですか? たとえば、データソースの名前が実際には learningRestDS である場合、次のように置き換えdataSourceますlearningRestDSか?

  • org.springframework.jdbc.datasource.DriverManagerDataSourceの代わりにデータソース クラス名が使用されるのはなぜcom.mysql.jdbc.jdbc2.optional.MysqlDataSourceですか?

applicationConext.xml:

<?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:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
   <!--  Scan for both Jersey Rest Annotations a -->
   <context:component-scan base-package="com.persistent.rest,com.persistent.service,com.persistent.service.jpa"/>
   <context:annotation-config />
   <tx:annotation-driven />
   <bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource"
    p:driverClassName="com.mysql.jdbc.Driver" p:url="jdbc:mysql://localhost:3306/jpa"
    p:username="user" p:password="password" />

<bean id="entityManagerFactory"
    class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean"
    p:dataSource-ref="dataSource" p:jpaVendorAdapter-ref="jpaAdapter">
    <property name="loadTimeWeaver">
        <bean
            class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" />
    </property>
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager"
    p:entityManagerFactory-ref="entityManagerFactory" />
<bean id="jpaAdapter"
    class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
    p:database="MYSQL" p:showSql="true" />

更新: エラー:

SEVERE: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personService':
Injection of persistence methods failed;
nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'entityManagerFactory' defined in class path resource [applicationContext.xml]:
Invocation of init method failed; nested exception is java.lang.AbstractMethodError: 
org.springframework.orm.jpa.persistenceunit.SpringPersistenceUnitInfo.getValidationMode()Ljavax/persistence/ValidationMode;
4

2 に答える 2

1

"dataSource"後でその Bean を参照するために使用する任意の Bean 名 (ID) です。

p:dataSource-ref="dataSource"

データ ソースに名前が付けられ"learningRestDS"ている場合は、次を使用します。

p:dataSource-ref="learningRestDS"

はのp:dataSource-refプロパティの名前です(そのクラスには、注入に使用されるメソッドがありDriverManagerDataSourceます。setDataSource()learningRestDS


データ ソースを定義するときは、DataSourceインターフェイス (抽象化の力) を実装する任意のクラスを自由に使用できます。この XML の作成者は、 DriverManagerDataSource代わりにMysqlDataSource. どちらも契約に従っている限り、どちらも同じように機能します1 。DataSource

他にも接続プーリング ライブラリなどDataSource、さまざまな実装が考えられます。それらの利点は、すべての JDBC ドライバー/データベースで動作することです。

1はパフォーマンスが非常に悪いため、実際にDriverManagerDataSourceはテスト用にのみ使用する必要がありますが、機能的な観点からは、他のDataSource.


あなたのコメントに答えるために更新してください。あなたが見ている:

Invalid property 'driverClassName' of bean class [com.mysql.jdbc.jdbc2.optional.MysqlDataSource]: No property 'driverClassName' found 

つまり、Spring は、そのようなプロパティ (セッター) を持たない -のプロパティdriverClassNameに何かを注入しようとします。MysqlDataSourceこれは、引用しているアプリケーション コンテキスト XML ファイルが不完全/一貫性がない/正しくないことも意味しますDriverManagerDataSource。これが Spring が使用するファイルであることを確認し、driverClassNameMySQL データ ソースが使用されている場合はプロパティを削除します。

于 2012-10-04T20:12:16.567 に答える
1

「dataSource」の ID は、Java Bean インスタンスが関連付けられている単なる名前です。それを指す ref="" 属性も更新する限り、必要に応じて変更できます。

クラス org.springframework.jdbc.datasource.DriverManagerDataSource は、JDBC 接続の手動インスタンス化に通常関連付けられるすべてのコードをカプセル化するヘルパー クラスです。そういう意味ではラッパーと考えることができます。指定したプロパティ、つまりドライバー クラスと URL を取得し、それを使用して新しい接続をインスタンス化します。

そうは言っても、com.mysql.jdbc.jdbc2.optional.MysqlDataSource が java.sql.DataSource インターフェイスを実装している限り、それを代わりに使用できます。

本番環境では、これをプーリング データ ソース、または JNDI 構成の接続プールに置き換える可能性があります。多くの選択肢があり、Spring の DriverManagerDataSource のみを使用することにこだわる必要はありません。

于 2012-10-04T20:15:50.010 に答える