title: Dependency Injection in OSGI environments
body: <p>First some background: </p>
  
  <p>I'm working on some webapp prototype code based on <a href="http://sling.apache.org/site/index.html">Apache Sling</a> which is OSGI based and runs on Apache Felix. I'm still relatively new to OSGI even though I think I've grasped most concepts by now. However, what puzzles me is that I haven't been able to find a "full" dependency injection (DI) framework. I've successfully employed rudimentary DI using Declarative Services (DS). But my understanding is that DS are used to reference -- how do I put this? -- OSGI registered services and components together. And for that it works fine, but I personally use DI frameworks like <a href="http://code.google.com/p/google-guice/">Guice</a> to wire entire object graphs together and put objects on the correct scopes (think <code>@RequestScoped</code> or <code>@SessionScoped</code> for example). However, none of the OSGI specific frameworks I've looked at, seem to support this concept. </p>
  
  <p>I've started reading about <a href="http://www.ibm.com/developerworks/opensource/library/os-osgiblueprint/index.html">OSGI blueprints</a> and <a href="http://felix.apache.org/site/apache-felix-ipojo.html">iPOJO</a> but these frameworks seem to be more concerned with wiring OSGI services together than with providing a full DI solution. I have to admit that I haven't done any samples yet, so my impression could be incorrect.  </p>
  
  <p>Being an extension to Guice, I've experimented with <a href="http://code.google.com/p/peaberry/">Peaberry</a>, however I found documentation very hard to find, and while I got basic DI working, a lot of guice-servlet's advanced functionality (automatic injection into filters, servlets, etc) didn't work at all. </p>
  
  <p>So, my questions are the following:</p>
  
  <ol>
  <li>How do declarative services compare to "traditional" DI like Guice or Spring? Do they solve the same problem or are they geared towards different problems?</li>
  <li>All OSGI specific solutions I've seen so far lack the concept of scopes for DI. For example, Guice + guice-servlet has request scoped dependencies which makes writing web applications really clean and easy. Did I just miss that in the docs or are these concerns not covered by any of these frameworks?</li>
  <li>Are <a href="http://www.jcp.org/en/jsr/detail?id=330">JSR 330</a> and OSGI based DI two different worlds? iPOJO for example brings its <a href="http://felix.apache.org/site/how-to-use-ipojo-annotations.html">own annotations</a> and <a href="http://felix.apache.org/site/scr-annotations.html">Felix SCR Annotations</a> seem to be an entirely different world. </li>
  <li>Does anybody have experience with building OSGI based systems and DI? Maybe even some sample code on github? </li>
  <li>Does anybody use different technologies like Guice and iPOJO together or is that just a crazy idea?</li>
  </ol>
  
  <p>Sorry for the rather long question. </p>
  
  <p>Any feedback is greatly appreciated. </p>
  
  <hr>
  
  <p><strong>Updates</strong></p>
  
  <p><em>Scoped injection</em>: scoped injection is a useful mechanism to have objects from a specific lifecycle automatically injected. Think for example, some of your code relies on a Hibernate session object that is created as part of a servlet filter. By marking a dependency the container will automatically rebuild the object graph. Maybe there's just different approaches to that? </p>
  
  <p><em>JSR 330 vs DS</em>: from all your excellent answers I see that these are a two different things. That poses the question, how to deal with third party libraries and frameworks that use JSR 330 annotations when used in an OSGI context? What's a good approach? Running a JSR 330 container within the Bundle? </p>
  
  <p>I appreciate all your answers, you've been very helpful! </p>