我在stackoverflow上四处搜索,但我没有看到我的用例的解决方案。
当我尝试为默认值添加通用属性文件时遇到问题
目前我们在 app-context.xml 中只有环境特定的属性文件,指定如下:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:env_properties/${DEPLOY_ENV}/app.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
</bean>
我添加了一个 default_app.properties 文件,以避免在所有环境中重复属性。变化如下:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:env_properties/default_app.properties</value>
<value>classpath:env_properties/${DEPLOY_ENV}/app.properties</value>
</list>
</property>
<property name="ignoreUnresolvablePlaceholders" value="true"/>
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE"/>
</bean>
我正在使用类中的@Value 注释访问属性,例如:@Value("${is_feature_enabled}")
当定义环境特定的属性文件时,这工作得很好。但是,当仅在 default_app.properties 文件中指定该属性时,它会引发以下异常
例外是
Caused by: org.springframework.beans.factory.BeanCreationException:
Could not autowire field: private boolean
com.XXXXXX.YYY.ZZZZ.SomePackage.SomeClass.is_feature_enabled;
nested exception is java.lang.IllegalArgumentException:
Could not resolve placeholder 'is_feature_enabled' in string value
"${is_feature_enabled}"
请注意:我能够成功引用默认属性文件中定义的属性作为 app-context.xml 中定义的 bean 的构造函数参数。仅当我尝试使用 @Value 在类中引用它时才面临这个问题
欣赏任何想法或见解。谢谢
更新每条评论我像这样@Value("${is_feature_enabled}") 按值访问属性