1、开启命名空间
1.复制xmlns,在后面加上:context,将bean改为context
2.复制两个http,将bean改为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
">
2、使用命名空间加载properties配置文件
jbdc.driver=com.mysql.jbdc.Driver
<!--2、使用命名空间加载properties配置文件-->
<context:property-placeholder location="jdbc.properties"/>
3、使用属性占位符读取properyies配置文件中的内容
<!--3、使用属性占位符读取文件properties中的属性-->
<bean class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jbdc.driver}"/>
</bean>
注意:配置文件中我们可能会出现与系统环境配置命名相同的情况,此时系统环境优先级高于我们自定义的配置,可能会出现错误
解决方法:关闭系统环境变量
<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
4、加载多个配置文件
<context:property-placeholder location="classpath:*.properties" system-properties-mode="NEVER"/>
在classpath后面加上* 表示不仅可以在当前项目中加载配置文件,还可以在依赖的jar包中加载配置文件。
<context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/>

本文介绍了如何在Spring配置文件中开启命名空间,特别是`context`命名空间,用于加载和使用properties配置文件。通过`context:property-placeholder`标签,可以加载指定的properties文件,并用属性占位符读取内容,如设置`DruidDataSource`的数据源配置。同时,文章提到了当配置文件名与系统环境变量冲突时,可以通过设置`system-properties-mode`为`NEVER`来避免系统环境变量的影响,并展示了如何加载多个配置文件的方法。
1475

被折叠的 条评论
为什么被折叠?



