【问题标题】:Osgi Property PlaceholderOsgi 属性占位符
【发布时间】:2017-03-21 19:16:36
【问题描述】:

我这里只能使用字符串绑定,enableRequestValidation 应该始终是字符串,放入我想要使用布尔值的 bean,如何使用属性占位符绑定来实现?

<property-placeholder 
    persistent-id="JsonValidator"
    update-strategy="reload" placeholder-prefix="$[" placeholder-suffix="]">
    <default-properties>
        <property name="enableRequestValidation" value="false"></property>
    </default-properties>
</property-placeholder>

<bean id="jsonSchemaRegistration"       class="rest.service.impl.jsonschema.JsonSchemaDynamicFeatureImpl">
    <property name="enableRequestValidation" value="$[enabledRequestValidation]"></property>
</bean>

添加异常如下

2016-11-08 11:25:34,944 | ERROR | Thread-74        | BlueprintContainerImpl
      | 15 - org.apache.aries.blueprint.core - 1.4.4 | Unable to start blueprint
 container for bundle core.rest.service.impl/0.6.0.SNAP
SHOT
org.osgi.service.blueprint.container.ComponentDefinitionException: Error setting
 property: PropertyDescriptor <name: enableRequestValidation, getter: class core.rest.service.impl.jsonschema.JsonSchemaDynamicFeatureI
mpl.isEnableRequestValidation(), setter: [class JsonSchemaDynamicFeatureImpl.setEnableRequestValidati
on(boolean)]
        at org.apache.aries.blueprint.container.BeanRecipe.setProperty(BeanRecip
e.java:939)[15:org.apache.aries.blueprint.core:1.4.4]
        at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRec
ipe.java:905)[15:org.apache.aries.blueprint.core:1.4.4]
        at org.apache.aries.blueprint.container.BeanRecipe.setProperties(BeanRec
ipe.java:886)[15:org.apache.aries.blueprint.core:1.4.4]

【问题讨论】:

    标签: java properties osgi placeholder


    【解决方案1】:

    您可以使用 Aries 蓝图配置管理吗?您没有提供有关您的环境是什么的任何信息,但使用 ServiceMix,我一直这样做。例如:

    <blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:config="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0"
       xsi:schemaLocation="
         http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/smlns/blueprint/v1.0.0/blueprint.xsd">
    
      <!-- OSGi blueprint property placeholder binding to a configuration file -->
      <config:property-placeholder id="myProps.props" persistent-id="myProps" update-strategy="reload">
        <config:default-properties>
          <config:property name="my.setting" value="true" />
        </config:default-properties>
      </config:property-placeholder>
    
      <bean id="myBean" class="org.me.MyClass">
        <property name="setting" value="${my.setting}" />
      </bean>
    </blueprint>
    

    注意 blueprint-cm 命名空间的 1.1.0 版本,它支持 update-strategy 设置。属性注入会找到 setSetting(boolean setting) 方法并尝试将字符串转换为布尔值。这里指定了默认值“true”,但可以通过更改 etc/myProps.cfg 来覆盖。

    【讨论】:

      【解决方案2】:

      OSGi 蓝图规范不支持属性占位符。

      【讨论】:

      • OSGi 蓝图规范不支持属性占位符。上面提到的对属性占位符的支持是 Aries 实现的非标准扩展,您可以通过 XML 命名空间看到。我指的是 OSGi 蓝图规范。
      猜你喜欢
      • 1970-01-01
      • 2014-07-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-17
      • 2015-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多