2014-03-13 105 views
2

我有一个数据访问模块,它使用Spring和JDBC提供存储库的实现。OSGi声明式服务和弹簧

因此,我定义了一个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与声明式服务集成的正确方法。

谢谢,迈克尔

+0

如果您可以离开游戏,那么我可以推荐几个具有相同功能的可配置DS组件:DataSource,TransactionHelper,事务感知连接池 –

+0

您还可以看看Blueprint,它允许你使用Spring风格的语法来声明OSGi服务。使用Apache Aries,它与JPA和JDBC集成在一起。 –

回答

2

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

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

蓝图的做法是从春天怎么做JPA和容器管理事务非常不同。在春天你通常在你的上下文中创建数据源并注入它。在蓝图中,你更像在标准jpa中工作。在persistence.xml中引用数据源并使用jndi查找数据源。 Aries jndi然后从jndi桥接到OSGi服务,所以另一个bundle可以提供数据源作为OSGi服务。

你有另一种选择是使用Spring DM创建JPA服务“春天的方式”。但是spring dm没有被维护,并且在OSGi中有很多问题。所以蓝图是目前最好的选择。

+0

感谢您的回答,我使用Eclipse Gemini(之前为Spring DM),但是我听说过很多传言,比如它没有被维护,并且不能很好地处理服务跟踪。所以,我开始关注DS,并遇到了与Spring的集成问题。我问Eclipse双子座论坛,他们说这是维护,他们被关闭释放2.0.M3。直到现在我用它,我没有看到任何问题。此外,Apache白羊座似乎并没有更积极。 –

+0

我以某种方式怀疑双子座蓝图仍然活跃。见http://git.eclipse.org/c/gemini.blueprint/org.eclipse.gemini.blueprint.git/stats/?period=q&ofs=10 不知道白羊座蓝图有多活跃。 –

+0

@ChristianSchneider我相信你的链接不工作http://liquid-reality.de/pages/viewinfo.action?pageId=6586413 – Gamby