0

I searched around on stackoverflow but i do not see a solution for my usecase.

I am facing an issue when i try to add a common property file for default values

Currently we have only environment specific property files in the app-context.xml specified as follows:

    <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>

I added a default_app.properties file to avoid duplication of properties across all environments. The change is as follows:

<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>

I am accessing the properties using the @Value annotation in the classes Eg: @Value("${is_feature_enabled}")

This works perfectly fine when defined the Environment specific property files. However it throws the following exception when the property is only specified in the default_app.properties file

The exception is

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}"

Please Note: Am able to successfully reference the property defined in the default property file as a constructor argument for a bean defined in the app-context.xml. Am facing this issue only when I try to reference it in a class using @Value

Appreciate any thoughts or insights. Thanks

Updated per Comment I access the property by Value like this @Value("${is_feature_enabled}")

4
  • Can you add an example of how you reference a property in a class using @Value annotation?
    – araknoid
    Oct 14, 2017 at 13:43
  • I did go through those links but I do not believe that is the issue. I do not have multiple PropertyPlaceHolderConfigurer and also if that were the case, then my Environment specific property should have failed as well in @Value use case
    – IamSats
    Oct 15, 2017 at 13:53
  • I have added the example for @Value
    – IamSats
    Oct 15, 2017 at 13:53

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.