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

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

2. xml 文件配置數(shù)據(jù)庫連接池
<?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>二、引入外部屬性文件來配置數(shù)據(jù)庫連接池
1. 創(chuàng)建外部文件
創(chuàng)建 properties 格式文件,寫入數(shù)據(jù)庫信息。
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 的配置文件中來,通過使用名稱空間 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. 引用外部文件里的屬性
通過${}
<?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的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Jenkins中自動化部署Spring?Boot項目的全過程
這篇文章主要介紹了如何使用Jenkins從Git倉庫拉取SpringBoot項目并進(jìn)行自動化部署,通過配置Jenkins任務(wù),實現(xiàn)項目的構(gòu)建、鏡像構(gòu)建和容器運行,確保項目在更新時自動部署,需要的朋友可以參考下2025-01-01
Spring Security OAuth2集成短信驗證碼登錄以及第三方登錄
這篇文章主要介紹了Spring Security OAuth2集成短信驗證碼登錄以及第三方登錄,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04
JAVA實現(xiàn)sm3加密簽名以及防止重復(fù)攻擊
這篇文章主要給大家介紹了關(guān)于JAVA實現(xiàn)sm3加密簽名以及防止重復(fù)攻擊的相關(guān)資料,SM3是簽名算法,和MD5一樣(對于應(yīng)用層來說),SM4是對稱加密算法,和AES一樣(對于應(yīng)用層來說),需要的朋友可以參考下2023-10-10
springboot中使用ConstraintValidatorContext驗證兩個字段內(nèi)容相同
開發(fā)修改密碼功能時,通過ConstraintValidator校驗新密碼和確認(rèn)新密碼的一致性,首先定義Matches注解和DTO對象,然后創(chuàng)建MatchesValidator類實現(xiàn)驗證邏輯,對springboot驗證字段內(nèi)容相同問題感興趣的朋友一起看看吧2024-10-10

