OSGi声明服务和Spring

时间:2014-03-13 14:09:53

标签: java spring osgi declarative-services

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

谢谢,Mickael

1 个答案:

答案 0 :(得分:3)

Spring和Declarative服务并不是一起使用的。因此,您使用的方法将无法工作。声明式服务是一个非常简单的框架,只能将服务连接到组件并将组件作为服务发布。它没有关于jpa的能力。所以如果你想使用像jpa这样的容器管理持久性,我认为DS不会是一个很好的匹配。

像Holly提到的蓝图在其他一些白羊座模块的帮助下支持它。我已经创建了一个教程,演示了如何在完整的示例中使用它。请参阅:http://liquid-reality.de/pages/viewinfo.action?pageId=6586413

蓝图方法与spring对jpa和容器管理事务的处理方式有很大不同。在Spring中,您通常会在上下文中创建数据源并将其注入。在蓝图中,你的工作更像是标准的jpa。在persistence.xml中引用数据源并使用jndi查找。 Aries jndi然后从jndi桥接到OSGi服务,因此另一个bundle可以提供数据源作为OSGi服务。

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