当前位置: 首页 > 知识库问答 >
问题:

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

曾歌者
2023-03-14

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

通过蓝图

<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

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中的配置管理服务。另外,我不确定在我的包中以两种不同的方式加载同一个属性文件是否合适。

共有1个答案

邴景山
2023-03-14

蓝图 cm:property-placeholde 和 configuration-admin 服务都不会使用您添加到 etc 文件夹中的文件。cm 只是使用配置管理服务的另一种方式。
felix FileInstaller 会从 ServiceMix 实例的 etc 文件夹中读取 cfg 文件,并将这些属性传播到配置管理服务。
因此,在您的情况下,您需要向FileInstaller添加另一个配置以从另一个路径读取。
这可以通过添加新的配置文件来完成:

org.apache.felix.fileinstall-mySpecialDir.cfg

在其中添加要监视的新文件夹:

felix.fileinstall.dir = myNewSpecialDirectory-to-be-watched

如果需要的话再加一些。可以在这里找到它的文档

 类似资料:
  • 我想指定属性文件的位置,OSGi蓝图属性占位符应从中读取属性值: 更新:Configuration felix.configadmin felix.fileinstall适用于我。 我安装了: org.apache.felix.configadmin-1.8.0.jar org . Apache . Felix . file install-3 . 1 . 4 . jar org.eclipse.

  • 我不明白为什么不能在Spring-Boot中向application.properties文件中注入值。外部属性添加到logging.file变量中。我有一个application.properties文件,看起来如下所示 具有相应的Spring-boot应用程序类 请注意,如果我自己注入服务器端口号,那么在注入和启动应用程序时没有任何问题。 我在这个问题上兜圈子,弄不清自己做错了什么。

  • 我正在尝试使用骆驼JavaDSL将文件路由到SFTP服务器,如下所示: 但是,当消息到达此终结点时,Camel 会引发以下异常: 我可以看到在堆栈跟踪中打印的交易所上设置了目标目录属性。如果我替换 exchangeProperty(destinationDir) 在具有实际目标目录(tmp/目标/dir 1/)的路由中,它工作正常。问题是,我需要目标目录是动态的。我尝试过在路由中使用( 在调试Ca

  • 问题内容: 有谁知道我是否应该可以在预选赛中使用属性占位符作为表达式?我似乎无法正常工作。 我正在使用Spring 3.0.4。 XML: config.properties: 问题答案: 这可行。如果仅使用默认的spring bean名称,则可以省略服务名称。serviceA与ServiceA等 XML: Props:

  • 我有一个SpringBoot应用程序。对于Thymeleaf,对于此属性文件: 在控制器上: 在模板上: 但我在浏览器上看到:

  • 我在尝试添加