IOC操作Bean管理XML方式(外部属性文件)
目錄
IOC操作Bean管理XML方式(外部屬性文件)
前情引入:
實驗演示:
? ??1.直接配置數據庫信息
(1)配置德魯伊連接池
(2)引入德魯伊連接池jar包
(3)創建一個bean6.xml配置文件
? ??2.引入外部屬性文件配置數據庫連接池
(1)創建外部屬性文件 properties 格式的配置文件,寫數據庫信息
步驟一:創建文件 jdbc.properties? 用來寫數據庫配置
(2)把外部 properties 屬性文件引入到 Spring 配置文件中
步驟一:引入 context 名稱空間
步驟二:在 spring 配置文件使用標簽引入外部屬性文件
?
IOC操作Bean管理XML方式(外部屬性文件)
?
前情引入:
問題:之前的做法是,創建一個類,在bean中進行屬性注入
但是,如果一個類中屬性很多,就需要寫很多<property><property/>
這么寫其實不是很方便,如果屬性值發生一些變化的話還需要去bean.xml配置文件中進行修改
?
解決:將一些?固定的值?或者 相關的值 放到一個其它的文件中
場景應用:比如操作數據庫中,有些數據庫的固定值,例如數據庫的地址、用戶名 、密碼......,就可以把數據庫這些值放到一個<property>的配置文件中,然后再在 xml配置文件中去讀取<property>配置文件中的內容,就是這么一個注入過程
?
實驗演示:
1.直接配置數據庫信息
(1)配置德魯伊連接池
(2)引入德魯伊連接池jar包
什么是德魯伊連接池jar包:Druid(德魯伊)是阿里巴巴開發的號稱為監控而生的數據庫連接池,Druid是目前最好的數據庫連接池。在功能、性能、擴展性方面,都超過其他數據庫連接池,同時加入了日志監控,可以很好的監控DB池連接和SQL的執行情況。Druid已經在阿里巴巴部署了超過600個應用,經過一年多生產環境大規模部署的嚴苛考驗
?首先:復制druid-1.0.9.jar包到項目中
1
2
3
?4
5
?
(3)創建一個bean6.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:p="http://www.springframework.org/schema/p"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"><!--最基本的:直接配置連接池--> <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><!--代表驅動名稱--><property name="driverClassName" value="com.mysql.jdbc.Driver"></property><!--數據庫的地址 jdbc:mysql://localhost:3306 名字 userDb--><property name="url" value="jdbc:mysql://localhost:3306/userDb"></property><!--登錄用戶名和密碼--><property name="username" value="root"></property><property name="password" value="root"></property> </bean></beans>?
查看源碼:
(數據庫名稱,以及驅動的名字......這些名字大部分是固定的)
問題:配置好的值現在是固定的,并且是直接在xml文件里面手寫出來,不是引入
解決:現在把值寫入外部的配置文件中,然后把配置文件引入進來,因此我們下面的操作就是
? ? ? ? ? ?引入外部屬性文件配置數據庫連接池
?
2.引入外部屬性文件配置數據庫連接池
?
【說白了,就是這么引入好維護,容易擴展】
(1)創建外部屬性文件 properties 格式的配置文件,寫數據庫信息
步驟一:創建文件 jdbc.properties? 用來寫數據庫配置
?
jdbc.properties?代碼如下:
.properties內容都是鍵值形式
prop.driverClass=com.mysql.jdbc.Driver prop.url=jdbc:mysql://localhost:3306/userDb prop.userName=root prop.password=root?
(2)把外部 properties 屬性文件引入到 Spring 配置文件中
?
步驟一:引入 context 名稱空間
?
?
步驟二:在 spring 配置文件使用標簽引入外部屬性文件
?
就可以通過一個名稱去引用一個文件,這樣做比較 好維護
<?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:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:util="http://www.springframework.org/schema/util"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"><!--從本地location 引入外部屬性文件--><context:property-placeholder location="classpath:jdbc.properties"/><!--配置連接池--><bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"><!--不是固定值:代表驅動名稱 通過表示式從文件中--><property name="driverClassName" value="¥{prop.driverClass}"></property><!--不是固定值:數據庫的地址 jdbc:mysql://localhost:3306 名字 userDb--><property name="url" value="¥{prop.url}"></property><!--不是固定值:登錄用戶名和密碼--><property name="username" value="¥{prop.userName}"></property><property name="password" value="¥{prop.password}"></property></bean></beans>?
?
?
總結
以上是生活随笔為你收集整理的IOC操作Bean管理XML方式(外部属性文件)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux nginx 安装stream
- 下一篇: asp.net ajax控件工具集 Au