首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何通过OSGi blueprint属性占位符和Java DSL加载外部属性文件

如何通过OSGi blueprint属性占位符和Java DSL加载外部属性文件
EN

Stack Overflow用户
提问于 2015-05-27 14:36:29
回答 1查看 5.7K关注 0票数 1

我在Apache servicemix中安装了一个包,它使用apache blueprint进行配置。我使用的是位于/config文件夹中的外部属性文件abc.cfg,加载方式如下:

通过蓝图

代码语言:javascript
复制
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:camel-cxf="http://camel.apache.org/schema/blueprint/cxf"
xmlns:jaxrs="http://cxf.apache.org/jaxrs" xmlns:cxf="http://cxf.apache.org/blueprint/core"
xsi:schemaLocation="
    http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
    http://camel.apache.org/schema/cxf http://camel.apache.org/schema/cxf/camel-cxf.xsd
    http://cxf.apache.org/blueprint/core http://cxf.apache.org/schemas/blueprint/core.xsd
    http://camel.apache.org/schema/blueprint/cxf http://camel.apache.org/schema/blueprint/cxf/camel-cxf.xsd"
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.0.0">

<cm:property-placeholder id="myProperties" persistent-id="abc" />

通过java DSL

代码语言:javascript
复制
public class MyActivator implements BundleActivator {

    @Override
    public void start(final BundleContext context) throws Exception {
        final ServiceReference serviceReference = context.getServiceReference(ConfigurationAdmin.class.getName());
        if (serviceReference != null) {

            final ConfigurationAdmin admin = (ConfigurationAdmin) context.getService(serviceReference);
            final Configuration configuration = admin.getConfiguration("abc");
            final Dictionary<String, Object> configurations = configuration.getProperties();

            if (configurations == null) {
                throw new CustomException("Exception in loading properties file");
            }
            populateProperties(configurations);
        }
    }
}

一切正常,但现在我需要将属性文件移动到自定义位置,以便将属性文件从不同的包中分离出来。因此,我将abc.cfg移到了/config/myFolder/中,但我无法以任何一种方式将新位置指定到我的包中。我试过使用ext:property-placeholder,但它不起作用,可能是因为我用错了(找不到任何全面的东西来理解它)。因此,请指导我如何在cm:property-placeholder中指定我属性文件的位置,并通过java DSL中的配置管理服务。此外,我不确定是否可以在我的包中以两种不同的方式加载相同的属性文件。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-05-27 19:34:43

无论是blueprint cm:property-placeholde还是configuration-admin服务都不使用您添加到etc文件夹中的文件。cm只是使用配置管理服务的另一种方式。

felix FileInstaller确实从ServiceMix实例的etc文件夹中读取cfg文件,并将这些属性传播到Configuration Admin服务。

因此,在本例中,您需要向FileInstaller添加另一个配置,以便从另一个路径读取数据。

这可以通过添加新的配置文件来完成:

代码语言:javascript
复制
org.apache.felix.fileinstall-mySpecialDir.cfg

添加要监视的新文件夹的位置:

代码语言:javascript
复制
felix.fileinstall.dir = myNewSpecialDirectory-to-be-watched

如果需要的话,再加一些。相关文档可在here中找到

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30474886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档