加载步骤:
1.开启context命名空间
2.使用context空间加载properties文件
3.使用属性占位符${}读取properties文件中的属性
4.验证
properties文件:
Spring配置文件
<?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">
<!--
1.开启context命名空间(下列三行与新建的Spring文件作比较就可知道)
xmlns:context="http://www.springframework.org/schema/context"
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
-->
<!-- 2.使用context空间加载properties文件-->
<context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
<!-- 3.使用属性占位符${}读取properties文件中的属性-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="${jdbc.driver}r"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.username}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!-- 4.验证-->
<bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl">
<property name="name" value="${jdbc.driver}"></property>
</bean>
</beans>
效果:
总结:(关于使用context空间加载properties文件的问题)
1.当系统变量与你写的配置一样时,Spring会有限加载系统变量,为了防止自己配置失效的这种事情的发生,在以下语句写入system-properties-mode="NEVER"
2.如何加载两个及其以上的配置文件,在上一个文件之前加入逗号分隔开
3.最理想的加载方式 *.properties
4.最专业的写法 classpath:*.properties,这种方式只能读取到当前工程里面的配置文件
5.全覆盖式的写法 classpath*:*.properties 读取类路径依赖jar包与本工程内的配置文件
总结:不是所有的都式要加载的,根据需求来进行改变,但是不建议深入,后期学习 与 mybatis-plus的时候,这些问题根本遇不到,这些基础的问题,可以留心观察,不用深入研究