Can Spring Boot be used with OSGi? If not, any plans to have an OSGi Spring Boot?

convert osgi to spring boot
osgi and karaf
spring dynamic modules
maven osgi
osgi bundle-activator
osgi implementations
osgi bundle hot deploy
osgi java 11

Can Spring Boot be used with OSGi? If not, any plans to have an OSGi Spring Boot (Apache Felix or Eclipse Equinox)? In my opinion, cloud applications must be highly modular and updatable like OSGi offers.

Yes, it's possible to run Spring Boot apps in OSGI container.

First of all, you'll have to switch from Spring Boot jar packaging to OSGI bundle.

If you're using Maven you can use org.apache.felix:maven-bundle-plugin for doing that. As Spring Boot dependency jars are not valid OSGI bundles, we should either make them valid bundles with bnd tool or we can embed them into the bundle itself. That can be done with maven-bundle-plugin configuration, particularly with <Embed-Dependency>.

However, we need to start the bundle with Spring Boot app somehow. The idea is to start Spring Boot in BundleActivator:

@Import(AppConfig.class)
@SpringBootConfiguration
@EnableAutoConfiguration
public class SpringBootBundleActivator implements BundleActivator {

    ConfigurableApplicationContext appContext;

    @Override
    public void start(BundleContext bundleContext) {
        Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
        appContext = SpringApplication.run(SpringBootBundleActivator.class);
    }

    @Override
    public void stop(BundleContext bundleContext) {
        SpringApplication.exit(appContext, () -> 0);
    }
}

You should also set context classloader to an OSGI classloader loading the bundle by Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());. That is required because Spring uses context classloader.

You can see this in action in my demo repo: https://github.com/StasKolodyuk/osgi-spring-boot-demo

Spring OSGi, In fact, the Spring Dynamic Modules bundle does not even export them to It might be possible to use Spring Dynamic Modules on a lower execution There are no current plans to make such a version of Spring. If you discover any additional materials useful for OSGi newbies, please let us know to update the list​. Can Spring Boot be used with OSGi? If not, any plans to have an OSGi Spring Boot? (4) I think it worth posting as separate answer (not everyone reads all comments to answers). Excellent solution from @StasKolodyuk gives a way to run Spring Boot application in an OSGI environment.

I think it worth posting as separate answer (not everyone reads all comments to answers).

Excellent solution from @StasKolodyuk gives a way to run Spring Boot application in an OSGI environment.

But with limitation: Spring Boot's auto-mapping by annotation does not work because of lack of package scan support when run in OSGI.

Here is another trick, that finally allows Spring Boot app with components to be auto-taken from your code to be run in OSGI (I tested in Karaf).

Functional example is available at https://github.com/dimmik/osgi-spring-boot-demo

The trick is to provide appropriate ResourcePatternResolver to SpringApplication instance:

package by.kolodyuk.osgi.springboot;

import org.osgi.framework.BundleActivator;
import org.osgi.framework.BundleContext;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.osgi.io.OsgiBundleResourcePatternResolver;

@SpringBootApplication
public class SpringBootBundleActivator implements BundleActivator {

    ConfigurableApplicationContext appContext;

    @Override
    public void start(BundleContext bundleContext) {
        // Set context classloader (main trick, to enable SpringBoot start at the first place)
        Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());
        // trick to enable scan: get osgi resource pattern resolver
        OsgiBundleResourcePatternResolver resourceResolver = new OsgiBundleResourcePatternResolver(bundleContext.getBundle());
        // and provide it to spring application
        appContext = new SpringApplication(resourceResolver, SpringBootBundleActivator.class).run();
    }

    @Override
    public void stop(BundleContext bundleContext) {
        SpringApplication.exit(appContext, () -> 0);
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBootBundleActivator.class);
    }
}

Exposing the boot classpath in OSGi, A fairly common question that I get from time to time is how to go about To express package dependencies, bundles use the OSGi directive on the <tt>​com.sun</tt> package, it will fail to resolve since there are no providers for it. If the packages available in the OSGi framework boot classpath are not  The modulename-osgi-context.xml file contains the bean definitions for importing and exporting OSGi services. It may (but is not required to) use the Spring Dynamic Modules OSGi schema as the top-level namespace instead of the Spring 'beans' namespace. The Spring-Context manifest header may be used to specify an alternate set of configuration

One possibility is to embed OSGi into your Spring Boot Application, to make some parts of your application accessible through the framework. See https://stackoverflow.com/a/4673904/173101 to see how you can start OSGi programmatically.

But in general there is nothing like "OSGi-Support". OSGi can be integrated into every Java-Application and, vice versa, you can package every Java-Code (also your Spring-Boot application) into an OSGi-bundle to start it within an OSGi container (although it probably wouldn't make much sense at all).

Chapter 1. Modular development with Spring and OSGi, Because Spring DM is not only about using modular Java systems to get things to No man is an island, however, and code is no different. In this book, you'll learn how Spring DM and its OSGi substrate can be used to OSGi bundles are standard JAR files, with additional metadata in their manifest file that aims at  Yes, it's possible to run Spring Boot apps in OSGI container.. First of all, you'll have to switch from Spring Boot jar packaging to OSGI bundle.. If you're using Maven you can use org.apache.felix:maven-bundle-plugin for doing that.

There's actually plenty of good reasons for deploying Spring Boot into OSGi, the main one being performance, particularly startup performance if your Spring Boot service is a functional service (i.e. it starts, returns results, ends). An application I'm currently beta testing in Spring Boot starts up in ~ 0.5 seconds deployed to Equinox versus 3.5 seconds on its own. Other reasons might be integration to an OSGi based application or Java EE server.

That said, you can also run OSGi from Spring Boot, for performance reasons I would probably favour Concierge as an OSGi implementation over Felix or Equinox, simply due to its small size (unless your app needs all the features of the bigger implementations.

Another alternative would be to wrap the Spring libraries used by your Spring Boot application into MSF4J (from WSO2). This doesn't take much work and can give you a 10x faster startup with 1/10th the memory usage.

OSGi vs Spring Boot, We take an opinionated view of the Spring platform and third-party libraries so you can OSGi and Spring Boot can be categorized as "Frameworks (Full Stack)​" tools. to be used in Mobile Development (Android), the framework Spring Boot is not as Whichever way you choose to go, there will be no regrets - trust me. You can use spring-dm to use spring in OSGi but honestly I do not recommend it. Spring dm tries to be as compatible to pure spring as possible and so introduces a lot of issues when in OSGi. Additionally springsource seems to hava abandoned work on spring dm so it seems like a dead track to me.

No, it doesn't support OSGi. Spring Boot is aimed to create microservices as packaged applications with every dependency and even the servlet containers packaged in an executable JAR, so it's highly modular and updateable, without the need to provide and configure an OSGi container.

Building Microservices in OSGi With the Apache Karaf Framework , In the Java ecosystem, these could be the Netflix OSS, Spring Cloud, Eclipse the idea of using OSGi in combination with microservices is in fact not new and has In this article, we will demonstrate how we can get the best of both worlds An OSGi framework/runtime makes use of the Java class loading  Instead of the injection with constructor we could have used an @Resource annotation, but this doesn’t work in Spring 2.5.6 and Spring Dm (but works well with Spring 3.0). And now the activator

Spring OSGi, Eclipse Gemini Blueprint is based on Spring Dynamic Modules 2.0 which has been both names are used interchangeably (especially by those that have used Spring There are no current plans to make such a version of Spring. OSGi platforms do not change the way libraries work, it just enforces tighter classloading. Personally, I still struggle with these concepts, but is the point of both technologies. We found that OSGi was difficult to manage, so we've been deploying smaller services to Spring Boot, but both are very viable options once you learn the tooling and design. And like the other posters mention, you may choose to use both, or neither.

Deploying into Apache Karaf Red Hat Fuse 7.0, Apache Karaf is structured to use OSGi functionality. the project's POM file and the project's Blueprint file(s) (located in META-INF/spring ). You do not have to use this service for configuration, but it is typically the most convenient way of of features that are installed when Apache Karaf boots up for the very first time. In this article, we explored the essential features of OSGi with a straightforward example that it's enough to understand the potential of OSGi. In conclusion, whenever we have to guarantee that a single application has to be updated without any disservice, OSGi can be a viable solution. The code for this post can be found over on GitHub.

pf4j-spring and Spring Boot · Issue #8 · pf4j/pf4j-spring · GitHub, This topic is about PF4J and not about OSGI. I am sure that you will find enough information about OSGI on the internet (Eclipse and majority of Java application  The OSGi Service Platform Core Specification defines the term service interface to represent the specification of a service's public methods. Typically this will be a Java interface, but the specification also supports registering service objects under a class name, so the phrase service interface can be interpreted as referring to either an interface or a class.

Comments
  • See my answer stackoverflow.com/a/53518521/3503685 with working demo
  • Works great, thanks for a solution. But only with kind of "static" routes. Springboot cannot find resources in bundles (java.io.FileNotFoundException: URL [bundle://221.0:1/by/kolodyuk/osgi/springboot/] cannot be resolved to absolute file path because it does not reside in the file system: bundle://221.0:1/by/kolodyuk/osgi/springboot/) Did you manage to find workaround?
  • @DmitryAndrievsky for which kind of resources did you faced that? Was that resource residing in another bundle?
  • @StasKolodyuk for anything related to bundle scan. Resource was in the same bundle. Spring tries to scan resources, and fails with exception mentioned above. here's an example (based on your code), that is assumed to be auto-handled, but actually it is not @ RestController public class SimpleController { @ GetMapping("/simple") public String index() { return "some text"; } }
  • And instead of using @ Configuration attribute (with additional config classes) I tried just mark BundleActivator as @ SpringBootApplication. If it makes sense, I can provide example that works when run as SB app (java -jar ...), but fails in OSGI
  • @DmitryAndrievsky, yeah, component scanning doesn't work properly. You need to use configuration classes and import beans manually. I faced that issue, dig down to PathMatchingResourcePatternResolver which doesn't work properly in OSGI bundles.There's an OSGI implementation here docs.spring.io/spring-osgi/snapshot-site/1.1.x/xref/org/… However, you need to hack in Spring Boot somehow to make it load an OSGI version but I didn't go that far.
  • Hi, how can one get bundleContext or inject List of services inside Spring Boot's controller class?
  • I have tried this solution with Spring Boot 2.1.4 but it seems that it doesn't work. It doesn't load my application.properties. Have you tried upgrading Spring Boot?
  • I have question for your answer. You wrote that every Java Code (also Spring-Boot app) can be packaged into OSGi-bundle to start it within an OSGi container. The question is, how to package a Spring-Boot app into OSGi-bundle?
  • @xmlParser, see my answer stackoverflow.com/a/53518521/3503685 with working demo
  • What if one want to distribute a microservice app as a standalone app. In karat for instance. It would be nice to have each spring boot microservice deployed as OSGi bundle...