Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exporting Spring components as OSGi services

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.

like image 796
Alexey Romanov Avatar asked Apr 13 '12 09:04

Alexey Romanov


People also ask

Does spring use OSGi?

Spring DM is composed of a set of OSGi bundles that become part of the infrastructure when installed in an OSGi container. They can then manage a Spring application context for bundles or even start a web container inside the OSGi container.

What are OSGi services?

The OSGi Service Platform provides a mechanism for developing applications by using a component model and deploying those applications into an OSGi framework. The OSGi architecture is separated into a number of layers that provide benefits to creating and managing Java™ applications.

What is OSGi in spring?

The Open Service Gateway Initiative is a specification defining a Java-based component system. It's currently managed by the OSGi Alliance, and its first version dates back to 1999. Since then, it has proved to be a great standard for component systems, and it's widely used nowadays.

Is OSGi a Microservice?

OSGI is an application architecture, while microservices is a distributed systems concept. In my experience, microservices offer a number of benefits: Individual microservices are easy to deploy, test, and maintain. Microservices are language agnostic.


2 Answers

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.

like image 69
artbristol Avatar answered Oct 24 '22 03:10

artbristol


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)

like image 1
Johannes Wachter Avatar answered Oct 24 '22 02:10

Johannes Wachter