Spring實(shí)戰(zhàn)之屬性覆蓋占位符配置器用法示例
本文實(shí)例講述了Spring實(shí)戰(zhàn)之屬性覆蓋占位符配置器用法。分享給大家供大家參考,具體如下:
一 配置文件
<?xml version="1.0" encoding="GBK"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd">
<!-- PropertyOverrideConfigurer是一個(gè)容器后處理器,它會(huì)讀取
屬性文件信息,并用這些信息設(shè)置覆蓋Spring配置文件的數(shù)據(jù) -->
<bean class=
"org.springframework.beans.factory.config.PropertyOverrideConfigurer">
<property name="locations">
<list>
<value>dbconn.properties</value>
<!-- 如果有多個(gè)屬性文件,依次在下面列出來 -->
</list>
</property>
</bean>
<!-- 定義數(shù)據(jù)源Bean,使用C3P0數(shù)據(jù)源實(shí)現(xiàn),
配置該Bean時(shí)沒有指定任何信息,但Properties文件里的
信息將會(huì)直接覆蓋該Bean的屬性值 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method="close"/>
</beans>
二 屬性文件
dataSource.driverClass=com.mysql.jdbc.Driver dataSource.jdbcUrl=jdbc:mysql://localhost:3306/spring dataSource.user=root dataSource.password=32147
三 測試類
package lee;
import javax.sql.DataSource;
import java.sql.*;
import org.springframework.context.*;
import org.springframework.context.support.*;
public class BeanTest
{
public static void main(String[] args)throws Exception
{
ApplicationContext ctx = new
ClassPathXmlApplicationContext("beans.xml");
DataSource ds = (DataSource)ctx.getBean("dataSource");
Connection conn = ds.getConnection();
PreparedStatement pstmt = conn.prepareStatement(
"insert into news_inf value(null , ? , ?)");
pstmt.setString(1 , "瘋狂Java講義3");
pstmt.setString(2 , "瘋狂iOS講義3");
pstmt.executeUpdate();
pstmt.close();
conn.close();
}
}
四 測試結(jié)果

更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Spring框架入門與進(jìn)階教程》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》、《Java文件與目錄操作技巧匯總》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
相關(guān)文章
詳解java連接mysql數(shù)據(jù)庫的五種方式
這篇文章主要介紹了詳解java連接mysql數(shù)據(jù)庫的五種方式,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11
Spring MVC--攔截器實(shí)現(xiàn)和用戶登陸例子
本文主要介紹了Spring MVC--攔截器實(shí)現(xiàn)和用戶登陸例子,具有很好的參考價(jià)值,下面跟著小編一起來看下吧2017-03-03
J2SE基礎(chǔ)之在Eclipse中運(yùn)行hello world
本文的內(nèi)容非常的簡單,跟隨世界潮流,第一個(gè)Java程序輸出“Hell World!”。希望大家能夠喜歡2016-05-05
詳解Java8中接口的默認(rèn)方法和靜態(tài)方法
Java 8是Java語言的一個(gè)重要版本,其中引入了許多新特性和改進(jìn),其中一個(gè)值得關(guān)注的特性是接口的默認(rèn)方法和靜態(tài)方法,本文就來和大家簡單講講吧2023-05-05
Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例
這篇文章主要介紹了Spring Boot 整合 Mybatis Annotation 注解的完整 Web 案例,需要的朋友可以參考下2017-05-05
java實(shí)現(xiàn)隨機(jī)抽取獎(jiǎng)品工具類
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)隨機(jī)抽取獎(jiǎng)品工具類,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05
使用Spring AntPathMatcher的doMatch方法
這篇文章主要介紹了使用Spring AntPathMatcher的doMatch方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-09-09

