Newsletter

Resource Bundle In Spring: Dynamically Loading The Values For Property Placeholders In XML

Spring » on Feb 11, 2012 { 5 Comments } By Sivateja

Let us see how to load the values into spring configuration file dynamically using ResourceBundle concept

  • Instead of directly placing the values into xml we can load the values at run time for the dataSource properties using ResourceBundle
  • If we want to get the dataSource properties at runtime from ResourceBundle, while configuring the bean into xml we should put bundle keys with expression language into the xml file like…
<bean id="id1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName">
          <value>${jdbc.driver}</value>
    </property>
</bean>

In ResourceBundle

jdbc.driver = sun.jdbc.odbc.JdbcOdbcDriver

  •  In spring we have pre-defined class given, called PropertyPlaceholderConfigure and this class will read the data from the bundle and it will write the values into the configuration file
  • PropertyPlaceholderConfigure given in
    org.springframework.beans.factory.config.* package

ClientLogic

Resource res = new ClassPathResource("spconfig.xml");
XmlBeanFactory factory = new XmlBeanFactory(res);

PropertyPlaceholderConfigur ppc = new PropertyPlaceholderConfigurer();
ppc.setLocation(new ClassPathResource(MyPropertiesFile.properties));
ppc.postProcessBeanFactory(factory);

Notes:
At line number 5, there spring container will read the properties file from bundle.

At line number 6, spring container will write the properties into XML file

Now will see one example..

​​

You Might Also Like

  ::. About the Author .::

Java4s_Author
Sivateja Kandula - Java/J2EE Full Stack Developer
Founder of Java4s - Get It Yourself, A popular Java/J2EE Programming Blog, Love Java and UI frameworks.
You can sign-up for the Email Newsletter for your daily dose of Java tutorials.

Comments

5 Responses to “Resource Bundle In Spring: Dynamically Loading The Values For Property Placeholders In XML”
  1. suresh says:

    hi,
    ur site excellent
    pls provide spring with hibernate

  2. java4s says:

    @Suresh

    We are working on spring remaining modules…!
    Will get back to you ASAP.

    Thank you.

  3. Your Explanation on Spring JDBC,as well as explanation on IOC superb,,,,any one can learn spring easily with java4s…

    Thanks Java4s

  4. santhosh says:

    hi , I want dynamically reload the property file without restarting the server. how to implement this in spring.. need this urgent.. can you please me.

  5. Mansur says:

    Hi Team,
    There is some typo mistake as "PropertyPlaceholderConfigur ppc = new PropertyPlaceholderConfigurer();".

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Posts from Top Categories
Spring Boot Hibernate Spring
Contact | About Us | Privacy Policy | Advertise With Us

© 2010 - 2024 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.