1

i am learning spring and trying to understand retrieve value from properties file.

I am trying the following way to try out some samples

<util:properties id="spelProp" location="classpath:/META-INF/spelProperties.properties"></util:properties>

where the content of spelProperties.properties are

spelTeacher.firstName="First name from Properties"
spelTeacher.lastName="Last name from properties"

I try to access the lastName inorder to set one of the bean property like

<bean id="spelTeacher3Xml" class="com.learningweb.Service.SpelSampleTeacher">
<property name="lastName" value="#{spelProp[spelTeacher.lastName]}" />
</bean>

When i try to run using STS i am getting an error that "/META-INF/spelProperties.properties" does not exists.

Can someone help me to understand what is wrong here. Did i miss any item in configuring classpath or any Spring framework related settings?

Appreciate your help (Also any refernce to understand "classpath:" would be helpful. I tried to search but i am getting the right material i am looking for. I am trying to understand what does classpath: does in spring) Thanks!!!

linux developer
  • 761
  • 1
  • 11
  • 30

1 Answers1

3

You may want to try something like this instead:

    <bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">  
        <property name="locations">  
            <list>  
                <value>classpath:/META-INF/spelProperties.properties</value>  
            </list>  
        </property>  
    </bean>

or

    <context:annotation-config/>
    <context:property-placeholder location="classpath*:*.properties" />

And then access it in spelTeacher3Xml like: value="${spelTeacher.lastName}"

Depending on your version of Spring, checkout the API for PropertyPlaceholderConfigurer or some other more specialized version of that.

This guys site has a lot of basic Spring configuration tutorials. They may be a little dated, though.

varcharmander
  • 236
  • 1
  • 10
  • Thank you for the suggestion. I have a question about using ${} i read the SPel is actually #{}. are both one and the same? – linux developer Jan 13 '14 at 23:15
  • For the most part, they are both acceptable for the given purposes, SpringEL is just capable of more. [See this question](http://stackoverflow.com/questions/5322632/spring-expression-language-spel-with-value-dollar-vs-hash-vs) – varcharmander Jan 14 '14 at 00:02