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 .:: | ||
Comments
4 Responses to “Spring Resourcebundle Example”
Your Explanation is Too Good yaar !!!
Excellent Explanation. Complex technology in simple words .. keep up the good job …
superb explanation….. i didn’t find any alternate for this site till now…
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>