shuiguo

Spring读取properties文件的步骤:

开门见山

操作步骤:

1.准备外部properties文件

我的项目是用maven部署的,该文件在资源文件里,你们若不是,可放在src文件里,或需要的目录里。

2.开启context命名空间支持

在xmlns:xsi="xxx"的下一行加入

xmlns:context="http://www.springframework.org/schema/context"

在xsi:schemaLocation="xxx"里面的字符串中加入

http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd

 

注:Spring里有不少命名空间,最好自己仔细观察,找到其中规律,像上面这三行和原文中的比较。

3.加载指定的properties文件

<context:property-placeholder location="classpath:filename.properties"/>

 

4.使用加载的数据

<property name="propertyName" value="${propertiesName}"/>

 

注意: 如果需要加载所有的properties文件,可以使用*.properties表示加载所有的properties文件

注意: propertyName,指属性名,propertiesName指properties文件中的属性名

 

分类:

技术点:

相关文章: