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>

我还使用声明式服务将存储库实现公开为服务,如下所示:

<?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 与声明式服务集成的正确方法。

谢谢,米凯尔

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 服务提供。

您拥有的另一个选择是使用 spring dm 以“spring 方式”创建 jpa 服务。但是spring dm没有维护,在OSGi中有很多问题。所以蓝图是目前最好的方法。

于 2014-03-14T07:00:22.547 回答