我在这里只能使用字符串绑定(bind),enableRequestValidation 应该始终是字符串,放入我的 bean 中我想使用 boolean 值,如何使用属性占位符绑定(bind)来实现此目的?
<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]
最佳答案
您可以使用 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 设置。属性注入(inject)将找到 setSetting(boolean setting) 方法并尝试将字符串转换为 boolean 值。这里指定了默认值“true”,但可以通过更改 etc/myProps.cfg 来覆盖该值。
关于java - Osgi 属性占位符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40482233/