美文网首页
Spring之加载properties文件

Spring之加载properties文件

作者: 萧修 | 来源:发表于2023-07-04 08:57 被阅读0次

1、开启命名空间 context,

1.1、
xmlns增加冒号,添加content,将所有beans改为context

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

1.2 在xsi:schemaLoaction后面增加以下内容,和1.1一样,将beans改为context

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

2、使用context加载命名空间,其中system-properties-mode为系统属性是否加载

<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>

3、使用${}加载properties

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

${jdbc.xxx}

创建properties,步骤为:
新建 -> Resource bundle 命名bundle,在里面配置属性

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///spring_db
jdbc.username=root
jdbc.password=xxxxx

记载properties几种注意事项

<!--    不加载系统属性 -->
    <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
<!--    加载多个properties文件-->
    <context:property-placeholder location="jdbc.properties,jdbc2.properties"/>
<!--    加载所有properties-->
    <context:property-placeholder location="*.properties"/>
<!--    加载properties文件的标准格式-->
    <context:property-placeholder location="classpath:*.properties"/>
<!--    从类路径或jar包中搜索并加载properties文件-->
    <context:property-placeholder location="classpath*:*.properties"/>

相关文章

网友评论

      本文标题:Spring之加载properties文件

      本文链接:https://www.haomeiwen.com/subject/pygcudtx.html