Spring 加载properties 等等 其他文件(开启Contex命名空间的方式)

加载步骤: 

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的时候,这些问题根本遇不到,这些基础的问题,可以留心观察,不用深入研究

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

很丧

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值