Spring?IOC容器基于XML外部屬性文件的Bean管理
Spring IOC Bean管理XML
有時(shí)候,為了靈活方便,我們會(huì)把某些固定的數(shù)據(jù)存放到文件里,然后去讀取里面的內(nèi)容來(lái)使用。
比如數(shù)據(jù)庫(kù)的連接信息,這些內(nèi)容就可以放到 properties 文件中,然后使用 xml 配置文件去讀取里面的內(nèi)容,完成需要的注入。
這里使用德魯伊連接池舉例,德魯伊連接池是阿里巴巴開(kāi)源的數(shù)據(jù)庫(kù)連接池項(xiàng)目。
一、常規(guī)配置方法
1. 引入依賴(lài)
下載一個(gè)德魯伊的 jar 包,放到 lib 下面。

然后通過(guò) File-Project Structure 添加這個(gè) lib 下的jar包,應(yīng)用。

2. xml 文件配置數(shù)據(jù)庫(kù)連接池
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<!--直接配置連接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/userDb"></property>
<property name="username" value="root"></property>
<property name="password" value="123456"></property>
</bean>
</beans>二、引入外部屬性文件來(lái)配置數(shù)據(jù)庫(kù)連接池
1. 創(chuàng)建外部文件
創(chuàng)建 properties 格式文件,寫(xiě)入數(shù)據(jù)庫(kù)信息。
prop.driverClass=com.mysql.jdbc.Driver prop.url=jdbc:mysql://localhost:3306/userDb prop.username=root prop.password=123456
2. 引入外部文件到xml配置文件中
把剛才創(chuàng)建的 properties 文件引入到 spring 的配置文件中來(lái),通過(guò)使用名稱(chēng)空間 context。
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--引入外部屬性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
</beans>3. 引用外部文件里的屬性
通過(guò)${}
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">
<!--引入外部屬性文件-->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!--配置連接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${prop.driverClass}"></property>
<property name="url" value="${prop.url}"></property>
<property name="username" value="${prop.username}"></property>
<property name="password" value="${prop.password}"></property>
</bean>
</beans>以上就是Spring IOC容器Bean管理XML外部屬性文件的詳細(xì)內(nèi)容,更多關(guān)于Spring IOC Bean管理XML的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Jenkins中自動(dòng)化部署Spring?Boot項(xiàng)目的全過(guò)程
這篇文章主要介紹了如何使用Jenkins從Git倉(cāng)庫(kù)拉取SpringBoot項(xiàng)目并進(jìn)行自動(dòng)化部署,通過(guò)配置Jenkins任務(wù),實(shí)現(xiàn)項(xiàng)目的構(gòu)建、鏡像構(gòu)建和容器運(yùn)行,確保項(xiàng)目在更新時(shí)自動(dòng)部署,需要的朋友可以參考下2025-01-01
Spring Security OAuth2集成短信驗(yàn)證碼登錄以及第三方登錄
這篇文章主要介紹了Spring Security OAuth2集成短信驗(yàn)證碼登錄以及第三方登錄,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
基于java中的流程控制語(yǔ)句總結(jié)(必看篇)
下面小編就為大家?guī)?lái)一篇基于java中的流程控制語(yǔ)句總結(jié)(必看篇)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-06-06
手把手教你怎么創(chuàng)建spring項(xiàng)目
今天教大家怎么寫(xiě)spring項(xiàng)目,文中有非常詳細(xì)的圖文示例及介紹,對(duì)正在學(xué)習(xí)java的小伙伴們很有幫助,需要的朋友可以參考下2021-06-06
JAVA實(shí)現(xiàn)sm3加密簽名以及防止重復(fù)攻擊
這篇文章主要給大家介紹了關(guān)于JAVA實(shí)現(xiàn)sm3加密簽名以及防止重復(fù)攻擊的相關(guān)資料,SM3是簽名算法,和MD5一樣(對(duì)于應(yīng)用層來(lái)說(shuō)),SM4是對(duì)稱(chēng)加密算法,和AES一樣(對(duì)于應(yīng)用層來(lái)說(shuō)),需要的朋友可以參考下2023-10-10
springboot中使用ConstraintValidatorContext驗(yàn)證兩個(gè)字段內(nèi)容相同
開(kāi)發(fā)修改密碼功能時(shí),通過(guò)ConstraintValidator校驗(yàn)新密碼和確認(rèn)新密碼的一致性,首先定義Matches注解和DTO對(duì)象,然后創(chuàng)建MatchesValidator類(lèi)實(shí)現(xiàn)驗(yàn)證邏輯,對(duì)springboot驗(yàn)證字段內(nèi)容相同問(wèn)題感興趣的朋友一起看看吧2024-10-10

