3

Spring と JDBC を使用してリポジトリの実装を提供するデータ アクセス モジュールがあります。

したがって、Spring コンテキストを次のように定義します。

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.2.xsd">

    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">
        <property name="driverClass" value="${jdbc.driverClassName}" />
        <property name="jdbcUrl" value="${jdbc.url}" />
        <property name="user" value="${jdbc.username}" />
        <property name="password" value="${jdbc.password}" />
    </bean>

    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" />

    <bean id="annotationTransactionAspect" factory-method="aspectOf" class="org.springframework.transaction.aspectj.AnnotationTransactionAspect">
        <property name="transactionManager" ref="transactionManager" />
    </bean>
</beans>

また、次のように、Declarative Services を使用してリポジトリの実装をサービスとして公開します。

<?xml version="1.0" encoding="UTF-8"?>
<component name="cdr-repository" enabled="true" xmlns="http://www.osgi.org/xmlns/scr/v1.1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.osgi.org/xmlns/scr/v1.1.0 http://www.osgi.org/xmlns/scr/v1.1.0">

    <!-- Property and Properties -->
    <properties entry="OSGI-INF/myrepository-component.properties" />

    <!-- Service (optional) -->
    <service>
        <provide interface="com.example.osgi.dataaccess.api.MyRepository" />
    </service>

    <!-- Zero or more references to services -->

    <!-- Exactly one implementation -->
    <implementation class="com.example.osgi.dataaccess.jdbc.impl.MyRepositoryImpl" />
</component>

そのため、私のサービスは Spring 環境の外で作成されているため、完全には構成されていません (たとえば、データソースが挿入されていません)。

Spring を Declarative Services と統合する正しい方法を探しています。

ありがとう、ミカエル

4

1 に答える 1

3

Spring サービスと宣言型サービスは、一緒に使用することを意図していません。したがって、使用するアプローチは機能しません。宣言型サービスは、サービスをコンポーネントに接続し、コンポーネントをサービスとして公開するだけの非常に単純なフレームワークです。jpaに関する機能はありません。したがって、コンテナ管理の永続性を備えた jpa のようなものを使用する場合、DS は適していないと思います。

Holly が言及したように、青写真は他の aries モジュールの助けを借りてそれをサポートします。完全な例でこれを使用する方法を示すチュートリアルを作成しました。参照: http://liquid-reality.de/pages/viewinfo.action?pageId=6586413

ブループリントのアプローチは、Spring が jpa およびコンテナー管理トランザクションを行う方法とは大きく異なります。春には、通常、コンテキストでデータソースを作成して注入します。ブループリントでは、標準の jpa のように動作します。データソースは persistence.xml で参照され、jndi を使用して検索されます。次に、Aries jndi は jndi から OSGi サービスにブリッジして、別のバンドルがデータソースを OSGi サービスとして提供できるようにします。

もう 1 つのオプションは、spring dm を使用して "spring way" で jpa サービスを作成することです。しかし、Spring dm は維持されておらず、OSGi には多くの問題があります。そのため、ブループリントが現在の最善の方法です。

于 2014-03-14T07:00:22.547 に答える