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外部屬性文件的詳細內(nèi)容,更多關(guān)于Spring IOC Bean管理XML的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
在Spring環(huán)境中正確關(guān)閉線程池的姿勢
這篇文章主要介紹了在Spring環(huán)境中正確關(guān)閉線程池的姿勢,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
Springboot RocketMq實現(xiàn)過程詳解
這篇文章主要介紹了Springboot RocketMq實現(xiàn)過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-05-05
Mybatis攔截器實現(xiàn)數(shù)據(jù)權(quán)限詳解
這篇文章主要介紹了Mybatis攔截器實現(xiàn)數(shù)據(jù)權(quán)限詳解, 通過Mybatis攔截器我們可以攔截某些方法的調(diào)用,我們可以選擇在這些被攔截的方法執(zhí)行前后加上某些邏輯,需要的朋友可以參考下2023-11-11

