Spring IOC - properties文件
Spring提供了读取外部properties文件的机制,使用读取到的数据为bean的属性赋值。
操作步骤:
-
准备外部properties文件
-
开启context命名空间支持
<!-- 1.加载context命名空间的支持 -->
xmlns:context="http://www.springframework.org/schema/context"
- 加载指定的properties文件
<context:property-placeholder location="classpath:filename.properties">
- 使用加载的数据
<property name="propertyName" value="${propertiesName}"/>
注意:如果需要加载所有的properties文件,可以使用*.properties文件
注意:读取数据使用${propertiesName}格式进行,其中propertiesName指properties文件中的属性名