Monday, September 28, 2009

Loading a properties file using spring

Example properties file with filename:  fileOnHardDrive.properties

env.protocol=http
env.host=www.blogger.com

On your spring config file, you can load the properties file using the following code

<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
        <list>
            <value>classpath:fileOnClasspath.properties</value>
            <value>file:fileOnHardDrive.properties</value>
        </list>
        </property>
</bean>

after that, you can now use the values on your properties file by using their key enclosed by a dollar sign and two curly braces. Example code:

<bean id="myUrl" class="java.net.URL">
        <constructor-arg value="${env.protocol}://${env.host}"/>
</bean>
Spring will now interpret the constructor arg value to be:
http://www.blogger.com
 
:) 

7 comments:

  1. Quote from previous post:
    Personally, I don't like Spring (maybe because I didnt understand anything when the instructor taught it)...

    So what exactly happened?! O.O

    ReplyDelete
  2. LOL xD welcome back to your blog! I will refer to this post when I (or my boss) get myself to using Spring and Hibernate...

    ReplyDelete
  3. hindi na ako maka-relate lol!!!

    parang napunta na sa BAUL ang java ko, spring and hibernate pa kaya?

    mark >> i like meat balls too!

    ReplyDelete
  4. uy gusto me panoorin ang meatballs movie!!! :D parang cute eh hehe...

    ReplyDelete
  5. sorry, hindi pala meat balls, meat buns pala yun :( hehehe...

    ReplyDelete