spring_加载properties文件


加载properties文件

加载 properties 配置信息

  • 开启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
                ">
    </beans> 
  • 使用context命名空间,加载指定properties文件

    <context:property-placeholder location="jdbc.properties"/>
  • 使用${}读取加载的属性值

    <property name="username" value="${jdbc.username}"/>

加载方式

  1. 不加载系统属性

    <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
  2. 加载多个properties文件

    <context:property-placeholder location="jdbc.properties,msg.properties"/>
  3. 加载所有properties文件

    <context:property-placeholder location="*.properties"/>
  4. 加载properties文件标准格式

    <context:property-placeholder location="classpath:*.properties"/>
  5. 从类路径或jar包中搜索并加载properties文件

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

文章作者: han yue
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 han yue !
评论
  目录