2014-03-13 109 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