【问题标题】:OSGi Declarative Services and SpringOSGi 声明式服务和 Spring
【发布时间】:2014-03-13 14:09:53
【问题描述】:

我有一个数据访问模块,它使用 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 与声明式服务集成的正确方法。

谢谢,米凯尔

【问题讨论】:

  • 如果您可以不参与游戏,我可以推荐几个具有相同功能的可配置 DS 组件:DataSource、TransactionHelper、Transaction 感知连接池
  • 您还可以查看 Blueprint,它允许您使用 Spring 样式的语法声明 OSGi 服务。使用 Apache Aries,它与 JPA 和 JDBC 集成。

标签: java spring osgi declarative-services


【解决方案1】:

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中有很多问题。所以蓝图是目前最好的方法。

【讨论】:

  • 感谢您的回答,到目前为止,我一直在使用 Eclipse Gemini(以前是 Spring DM),但我听说过很多谣言,比如它没有得到维护,也不能很好地处理服务跟踪。因此,我开始研究 DS,但遇到了与 Spring 的集成问题。我在 Eclipse Gemini 论坛上询问过,他们说它已得到维护,并且已关闭发布 2.0.M3。我一直使用到现在,我没有发现任何问题。此外,Apache Aries 似乎没有更活跃。
  • 我有点怀疑双子座蓝图是否仍然有效。请参阅git.eclipse.org/c/gemini.blueprint/… 不确定 aries 蓝图有多活跃。
  • @ChristianSchneider 我认为您的链接无效liquid-reality.de/pages/viewinfo.action?pageId=6586413
猜你喜欢
  • 1970-01-01
  • 2012-04-27
  • 1970-01-01
  • 2013-10-09
  • 2018-05-20
  • 1970-01-01
  • 2011-09-03
  • 1970-01-01
  • 2013-06-03
相关资源
最近更新 更多