Spring configuration with property files


The Spring configuration files can include values from your properties files. You can easily customize this so that you can take control when the key and or the value of a property is requested. To do so, you must first tell Spring the location of your properties file:


<beans>
  <bean class="com.mycompany.MyPropertyPlaceholderConfigurer">
    <property name="locations">
       <value>classpath:my.properties</value>
    </property>
  </bean>
...

Next you must create the class:
com.mycompay.MyPropertyPlaceholderConfigurer:


package com.mycompany;
import java.util.Properties;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import com.adobe.hs.spdf.common.PropReader;
public class MyPropertyPlaceholderConfigurer extends
   PropertyPlaceholderConfigurer {

   // process the key and then return the associated value...
   protected String resolvePlaceholder(String placeholder, Properties props) {
	String value = process-the-key-and-lookup-the-value;
	return value;
   }
}

Finally, you can use the Spring’s ${...} notation to reference values from your properties file. e.g.,

3 Responses to “Spring configuration with property files”

  1. John Says:

    Very helpful. Especially the class writeup.

  2. Pragya Says:

    For java queries, you can refer : http://pragyarawal.blogspot.com

  3. spring config | Docufix Says:

    [...] http://note19.com/2007/08/01/spring-configuration-with-property-files/ This entry was posted in Uncategorized. Bookmark the permalink. ← Installing memcached [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


Follow

Get every new post delivered to your Inbox.

%d bloggers like this: