有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java OSGi声明性服务和Spring

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

谢谢,米克尔


共 (1) 个答案

  1. # 1 楼答案

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

    就像Holly提到的,blueprint在其他一些aries模块的帮助下支持这一点。我已经创建了一个教程,展示了如何在一个完整的例子中使用它。见:http://liquid-reality.de/pages/viewinfo.action?pageId=6586413

    blueprint方法与spring处理jpa和容器管理事务的方式非常不同。在spring中,您通常在上下文中创建数据源并将其注入。在blueprint中,您的工作更像标准jpa。在那里,数据源在持久性中被引用。xml并使用jndi进行查找。Aries jndi然后将jndi连接到OSGi服务,这样另一个捆绑包就可以将数据源作为OSGi服务提供

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