Purpose of PropertyOverrideConfigurer:
Using this we can load the spring bean properties with values given in the properties file.
Jars Required For this in addition to Spring jars
------------------------------------------------
commons-dbcp.jar
commons-pool.jar
ojdbc jar if it is oracle
Steps In Netbeans:
--------------------
1)File->NewProject->Java->JavaApplication
2)You can add spring libraries by browsing
RightClickProject->Libraries->AddLibrary->SpringFramework3.2.3 release
3)Add the other required jars mentioned above
RightClickProject->Libraries->AddJar/Folder->Choose
commons-dbcp.jar,commons-pool.jar,ojdbc.jar
ScreenShot of directory structure:
-----------------------------------
Files:
-------
jdbcproperties.properties
--------------------------
# To change this license header, choose License Headers in Project Properties.
# To change this template file, choose Tools | Templates
# and open the template in the editor.
ds.driverClassName=oracle.jdbc.driver.OracleDriver
ds.url=jdbc:oracle:thin:@localhost:1521:XE
ds.username=hr
ds.password=hr
2)springbeans.xml
-------------------
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
">
<bean id="dsbean" class="org.jl.MyDSBean" p:ds-ref="ds"></bean>
<bean id="ds" class="org.apache.commons.dbcp.BasicDataSource"></bean>
<bean class="org.springframework.beans.factory.config.PropertyOverrideConfigurer"
p:location="classpath:jdbcproperties.properties"/>
</beans>
3)MyDSBean.java
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package org.jl;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
*
* @author Rishitha
*/
public class MyDSBean {
private DataSource ds;
public MyDSBean(){
}
public DataSource getDs() {
return ds;
}
public void setDs(DataSource ds) {
this.ds = ds;
}
public static void main(String args[]) throws SQLException{
ApplicationContext ctx = new ClassPathXmlApplicationContext("springbeans.xml");
MyDSBean dsbean = (MyDSBean)ctx.getBean("dsbean");
System.out.println(dsbean.getDs().getConnection());
}
}
Hope you understood how to set bean properties from the values in properties file.
Why can't we directly provide... values in springbeans.xml
When you deploy in production...A DB admin can easily modify properties file easily if
changes required than.... xml file(springbeans.xml)
Hope you enjoyed........
No comments:
Post a Comment