title: OSGi Declarative Services and Spring
body: <p>I have a data access module that provides implementations of repositories using Spring and JDBC.</p>
  
  <p>Therefore, I define a Spring context as follows:</p>
  
  <pre><code>&lt;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"&gt;
  
      &lt;bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close"&gt;
          &lt;property name="driverClass" value="${jdbc.driverClassName}" /&gt;
          &lt;property name="jdbcUrl" value="${jdbc.url}" /&gt;
          &lt;property name="user" value="${jdbc.username}" /&gt;
          &lt;property name="password" value="${jdbc.password}" /&gt;
      &lt;/bean&gt;
  
      &lt;bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"&gt;
          &lt;property name="dataSource" ref="dataSource" /&gt;
      &lt;/bean&gt;
  
      &lt;tx:annotation-driven mode="aspectj" transaction-manager="transactionManager" /&gt;
  
      &lt;bean id="annotationTransactionAspect" factory-method="aspectOf" class="org.springframework.transaction.aspectj.AnnotationTransactionAspect"&gt;
          &lt;property name="transactionManager" ref="transactionManager" /&gt;
      &lt;/bean&gt;
  &lt;/beans&gt;
  </code></pre>
  
  <p>I also expose the repositories implementations as services using Declarative Services as follows:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;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"&gt;
  
      &lt;!-- Property and Properties --&gt;
      &lt;properties entry="OSGI-INF/myrepository-component.properties" /&gt;
  
      &lt;!-- Service (optional) --&gt;
      &lt;service&gt;
          &lt;provide interface="com.example.osgi.dataaccess.api.MyRepository" /&gt;
      &lt;/service&gt;
  
      &lt;!-- Zero or more references to services --&gt;
  
      &lt;!-- Exactly one implementation --&gt;
      &lt;implementation class="com.example.osgi.dataaccess.jdbc.impl.MyRepositoryImpl" /&gt;
  &lt;/component&gt;
  </code></pre>
  
  <p>So, my services are created outside the Spring environment and therefore they are not fully configured (e.g. datasource is not injected).</p>
  
  <p>I'm looking for the right way to integrate Spring with Declarative Services.</p>
  
  <p>Thanks, Mickael</p>