Newsletter

Spring Resourcebundle Example

Spring » on Feb 12, 2012 { 4 Comments } By Sivateja

Let us see how to use Resourcebundle in spring jdbc [ Dynamically load the values for property placeholders in spring configuration XML ]

Files Required

  • SpringJdbcSelect.java
  • OurLogic.java
  • spconfig.xml
  • jdbcBund.properties

Directory Structure

SpringJdbcSelect.java

package java4s;

import java.util.Iterator;
import java.util.List;

import org.springframework.jdbc.core.JdbcTemplate;

public class SpringJdbcSelect
{
	JdbcTemplate jt;
	public void setJt(JdbcTemplate jt)
	{
		this.jt = jt;
	}
	public void loadAll()
	{
		List l = jt.queryForList("select * from countries");
		Iterator it = l.iterator();
		while(it.hasNext())
		{
			Object o = it.next();
			System.out.println(o.toString());
		}
	}
}

OurLogic.java

package java4s;

import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;

public class OurLogic
{
	public static void main(String args[])
	{
		Resource res = new ClassPathResource("spconfig.xml");
		XmlBeanFactory factory = new XmlBeanFactory(res);

		PropertyPlaceholderConfigurer ppc = new PropertyPlaceholderConfigurer();
		ppc.setLocation(new ClassPathResource("/jdbcBund.properties"));
		ppc.postProcessBeanFactory(factory);

		SpringJdbcSelect jt =(SpringJdbcSelect)factory.getBean("id3");
		jt.loadAll();
	}
}

spconfig.xml

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN 2.0//EN"
"http://www.springframework.org/dtd/spring-beans-2.0.dtd">

<beans>

     <bean id="id1" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${jdbc.className}"/>
        <property name="url" value="${jdbc.url}"/>
        <property name="username" value="${jdbc.user}"/>
        <property name="password" value="${jdbc.pass}"/>
     </bean>
     
     <bean id="id2" class="org.springframework.jdbc.core.JdbcTemplate">
         <constructor-arg>
               <ref bean="id1"/>
         </constructor-arg>    
     </bean>
     
    <bean id="id3" class="java4s.SpringJdbcSelect">
           <property name="jt">
               <ref bean="id2"/>
           </property>
     </bean>

</beans>

jdbcBund.properties

jdbc.className = oracle.jdbc.driver.OracleDriver
jdbc.url = jdbc:oracle:thin:@localhost:1521:XE
jdbc.user = system
jdbc.pass = admin

Output:

 

​​

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

4 Responses to “Spring Resourcebundle Example”
  1. Boopathy says:

    Your Explanation is Too Good yaar !!!

  2. Satish says:

    Excellent Explanation. Complex technology in simple words .. keep up the good job …

  3. shaheed jahagirdar says:

    superb explanation….. i didn’t find any alternate for this site till now…

  4. Sangram says:

    By configuring below code in spconfig.xml, the properties file can be readable.

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" value="classpath:com/foo/jdbc.properties"/>
    </bean>

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.