有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

apache karaf如何通过OSGi blueprint propertyplaceholder和Java DSL加载外部属性文件

我在ApacheServiceMix中安装了一个包,它使用ApacheBlueprint进行配置。我正在使用一个外部属性文件abc。cfg位于/config文件夹中,正在按如下方式加载:

通过蓝图

<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) 个答案

  1. # 1 楼答案

    blueprint 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
    

    如果需要,再加上一些。 相关文档可在here