spring中在xml配置中加載properties文件的步驟
在Spring中,你可以使用PropertyPlaceholderConfigurer
來加載和解析properties文件。以下是在XML配置中加載properties文件的步驟:
假設(shè)我們有一個jdbc.properties的配置文件:
jdbc.driver=com.mysql.jdbc.Driver jdbc.url=jdbc:mysql://localhost:3306/ssm753as jdbc.username=root jdbc.password=123456
首先,在XML配置文件中引入context
命名空間,如下所示:
<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">
然后,在XML配置文件中添加PropertyPlaceholderConfigurer
bean的定義,指定要加載的properties文件路徑,例如:
<context:property-placeholder location="classpath:jdbc.properties" />
最后,你可以通過${}
語法在其他bean定義中使用properties文件中的屬性值,例如:
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource"> <property name="driverClassName" value="${jdbc.driver}" /> <property name="url" value="${jdbc.url}" /> <property name="username" value="${jdbc.username}" /> <property name="password" value="${jdbc.password}" /> </bean>
到此這篇關(guān)于在spring中如何在xml配置中加載properties文件的文章就介紹到這了,更多相關(guān)spring加載properties文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ Idea SpringBoot 數(shù)據(jù)庫增刪改查實例詳解
SpringBoot 是 SpringMVC 的升級,對于編碼、配置、部署和監(jiān)控,更加簡單。這篇文章主要介紹了IntelliJ Idea SpringBoot 數(shù)據(jù)庫增刪改查實例,需要的朋友可以參考下2018-02-02詳解在IDEA中使用MyBatis Generator逆向工程生成代碼
這篇文章主要介紹了詳解在IDEA中使用MyBatis Generator逆向工程生成代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-06-06SpringBoot整合MyBatis-Plus樂觀鎖不生效的問題及解決方法
這篇文章主要介紹了SpringBoot整合MyBatis-Plus樂觀鎖不生效的問題解決方案,通過實例代碼介紹了SpringBoot各個層次的操作,需要的朋友可以參考下2022-04-04