1

i added following in my applicationContext File

        <bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="locations">
                <list>
                    <value>classpath:stage.properties</value>
                    <value>classpath:environment.properties</value>
                </list>
            </property>
        </bean>

where i need to access stage.properties file values .stage.properties file is in src/main/resources

i have written following line in my java class to access this file

         @Value("${spring.username}")
         private String usr;

but i am getting valu for usr is like =${spring.username} what i am missing here?

suraha
  • 387
  • 2
  • 4
  • 16
  • http://stackoverflow.com/questions/11890544/spring-value-annotation-in-controller-class-not-evaluating-to-value-inside-pro – Reimeus May 05 '16 at 14:55

1 Answers1

2

@Value("${spring.username}") notation requires the use of PropertyPalceholderConfigurer / PropertySourcesPlaceholderConfigurer depending on Spring version to resolve property placeholder. Either

Solution 1

Replace

<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
            <property name="locations">
                <list>
                    <value>classpath:stage.properties</value>
                    <value>classpath:environment.properties</value>
                </list>
            </property>
        </bean>

with

<context:property-placeholder location="classpath:stage.propertes,classpath:environment.properties"/>

Make sure to import the Spring context namespace

Solution 2.

Use SpEL to access your properties bean with @Value as follows

@Value("#{properties['spring.username']}
private String usr;

This will access the spring.username property of the properties bean in your context

ekem chitsiga
  • 5,523
  • 2
  • 17
  • 18