如何在OSGi包中使用Spring bean? [英] How do I use a Spring bean inside an OSGi bundle?

查看:264
本文介绍了如何在OSGi包中使用Spring bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,我必须使用Spring根据OSGi包中的一些业务条件加载bean。这个bean不用于导出,用于计算inisde我的包。基本上我有一个实际的服务组件,它被导出,并且它必须在内部使用这个Spring bean。但是......

I have an application where I have to use Spring to load a bean based on some business conditions inside an OSGi bundle. This bean is not meant for export and is used for calculation inisde my bundle. Basically I have an actual service component, which is exported, and it has to use this Spring bean internally. But...


  1. 当我使用Spring DM时,扩展程序会在一个单独的线程中加载应用程序上下文。如何访问我的包中的上下文文件?

  2. 如何确保扩展程序线程完成加载应用程序上下文以便我可以在我的包中使用它?

  3. 我不想像Spring DM那样将应用程序上下文导出为服务,因为它仅在我的包内用于内部目的。

有没有办法做到这一点?

Is there any way to do this?

推荐答案

您不需要Spring DM来完成您想要完成的任务。

You don't need Spring DM for what you are trying to accomplish.

听起来你想要做的事实上是提供对你的bundle内部的上下文的访问,并让一些类通过ctx.getBean()进行查找。如果是这种情况,只需手动创建捆绑包中的上下文,就像您不在OSGi中进行调用一样。根本没有涉及Spring DM。

It sounds like what you want to do is actually provide access to your context inside of your bundle and have some class do lookups via ctx.getBean(). If this is the case, just create the context in your bundle manually like you would if you were not in OSGi and make the calls. No Spring DM involved at all.

这里的一个问题是你必须扩展ClassPathXmlApplicationContext以提供bundle类加载器,否则它将使用线程上下文类加载器。

The one issue here is that you have to extend ClassPathXmlApplicationContext to provide the bundles classloader, as it will use the thread context classloader otherwise.

ApplicationContext ctx = new ClassPathXmlApplicationContext(myCtxPath)
{
    protected void initBeanDefinitionReader(XmlBeanDefinitionReader reader)
    {
        super.initBeanDefinitionReader(reader);
        reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
        reader.setBeanClassLoader(getClassLoader());
    }
}

这篇关于如何在OSGi包中使用Spring bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆