本文介绍了Spring Boot可以与OSGi一起使用吗?如果没有,有没有计划使用OSGi Spring Boot?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring Boot可以与OSGi一起使用吗?如果没有,是否有计划使用OSGi Spring Boot(Apache Felix或Eclipse Equinox)?我认为,云应用程序必须像OSGi提供的那样具有高度模块化和可更新性.

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.

推荐答案

是的,可以在OSGI容器中运行Spring Boot应用.

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

首先,您必须从Spring Boot jar包装切换到OSGI bundle.

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

如果您使用的是Maven,则可以使用org.apache.felix:maven-bundle-plugin进行此操作.由于Spring Boot依赖罐不是有效的OSGI捆绑包,因此我们应该使用bnd工具使它们成为有效的捆绑包,或者我们可以将其嵌入捆绑包本身.这可以通过maven-bundle-plugin配置来完成,尤其是使用<Embed-Dependency>.

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>.

但是,我们需要以某种方式使用Spring Boot应用程序启动捆绑软件.这个想法是在BundleActivator:

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);
    }
}

您还应该将上下文类加载器设置为OSGI类加载器,以通过Thread.currentThread().setContextClassLoader(this.getClass().getClassLoader());加载捆绑软件.这是必需的,因为Spring使用上下文类加载器.

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.

您可以在我的演示存储库中看到此操作: https://github.com /StasKolodyuk/osgi-spring-boot-demo

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

这篇关于Spring Boot可以与OSGi一起使用吗?如果没有,有没有计划使用OSGi Spring Boot?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 05:50