5
votes

Is it possible to automatically register a Spring @Component (or one of its specifications; e.g. @Repository) as an OSGi service? So I can use autowiring and all other benefits of Spring inside one bundle and OSGi Service Registry to communicate between bundles?

UPDATE: From this discussion it doesn't seem to be possible using standard Blueprint, only in Eclipse Gemini.

3

3 Answers

5
votes

I assume you're familiar with Eclipse Gemini Blueprint (formerly Spring Dynamic Modules, formerly Spring OSGi)?

You export your beans using the <service>-tag. I'm not sure if you can export all @Component etc. annotated beans automatically though.

1
votes

A possible implementation to support this without the hack @AlexeyRomanov suggested could be to add a BeanFactoryPostProcessor implementation to the extender bundle of Gemini Blueprint via a fragment.

That processor than could create the needed OSGi export definitions inside the BeanFactory and it should work properly.

An example of how that could look like is the ServiceReferenceInjectionBeanPostProcessor form SpringDM's/Gemini's extension that allows you to get OSGi-Services injected via the @ServiceReference annotation. (Although I linked Spring DM this class should still be present in Gemini, but I couldn't find a direct link to their javadoc)

0
votes

Nasty workaround, for singleton beans only: store the instance in a static field in the constructor, and use this field's getter as your Blueprint/iPOJO factory method.