当前位置: 首页 > news >正文

Spring:IoC/DI加载properties文件

Spring框架可以通过Spring的配置文件完成两个数据源druidC3P0的配置(Spring:IOC/DI配置管理第三方bean),但是其中包含了一些问题,我们来分析下:

  • 这两个数据源中都使用到了一些固定的常量如数据库连接四要素,把这些值写在Spring的配置文件中不利于后期维护
  • 需要将这些值提取到一个外部的properties配置文件中

Spring框架如何从配置文件中读取属性值来配置呢?

实现思路

需求:将数据库连接四要素提取到properties配置文件,spring来加载配置信息并使用这些信息来完成属性注入。

1.在resources下创建一个jdbc.properties(文件的名称可以任意)

2.将数据库连接四要素配置到配置文件中

3.在Spring的配置文件中加载properties文件

4.使用加载到的值实现属性注入

步骤1:准备properties配置文件

resources下创建一个jdbc.properties文件,并添加对应的属性键值对

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/spring_db
jdbc.username=root
jdbc.password=root
步骤2:开启context命名空间

在applicationContext.xml中开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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd">
</beans>
步骤3:加载properties配置文件

在配置文件中使用context命名空间下的标签来加载properties配置文件

<context:property-placeholder location="jdbc.properties"/>
步骤4:完成属性注入

使用${key}来读取properties配置文件中的内容并完成属性注入

<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsd" >
<!--    1.开启context命名空间-->
<!--    2.使用context空间加载properties文件--><!--方式1    <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>-->
<!--方式2    <context:property-placeholder location="jdbc.properties,jdbc2.properties" system-properties-mode="NEVER"/>-->
<!--    classpath:*.properties  :   设置加载当前工程类路径中的所有properties文件-->
<!--    system-properties-mode属性:是否加载系统属性--><!--方式3    <context:property-placeholder location="*.properties" system-properties-mode="NEVER"/>--><!--方式4 classpath*:*.properties  :  设置加载当前工程类路径和当前工程所依赖的所有jar包中的所有properties文件--><context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/><!--    3.使用属性占位符${}读取properties文件中的属性-->
<!--    说明:idea自动识别${}加载的属性值,需要手工点击才可以查阅原始书写格式--><bean  id="dataSource"  class="com.alibaba.druid.pool.DruidDataSource"><property name="driverClassName" value="${jdbc.driver}"/><property name="url" value="${jdbc.url}"/><property name="username" value="${jdbc.username}"/><property name="password" value="${jdbc.password}"/></bean><bean id="bookDao" class="com.itheima.dao.impl.BookDaoImpl"><property name="name" value="${jdbc.url}"/></bean></beans>

说明:

  • 方式一:可以实现,如果配置文件多的话,每个都需要配置
  • 方式二:*.properties代表所有以properties结尾的文件都会被加载,可以解决方式一的问题,但是不标准
  • 方式三:标准的写法,classpath:代表的是从根路径下开始查找,但是只能查询当前项目的根路径
  • 方式四:不仅可以加载当前项目还可以加载当前项目所依赖的所有项目的根路径下的properties配置文件
  • <context:property-placeholder/>标签会加载系统的环境变量,而且环境变量的值会被优先加载,因此,增加system-properties-mode:设置为NEVER,表示不加载系统属性

至此,读取外部properties配置文件中的内容就已经完成,进行测试:

public class App {public static void main(String[] args) {ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext4.xml");DataSource dataSource = (DataSource) ctx.getBean("dataSource");System.out.println(dataSource);}
}

运行结果:
在这里插入图片描述

加载properties文件小结

本节主要讲解的是properties配置文件的加载,需要掌握的内容有:

  • 如何开启context命名空间

    在这里插入图片描述

  • 如何加载properties配置文件

    <context:property-placeholder location="" system-properties-mode="NEVER"/>
    
  • 如何在applicationContext.xml引入properties配置文件中的值

    ${key}
    

[说明]:内容主要来源黑马程序员网上资源学习

http://www.lryc.cn/news/486672.html

相关文章:

  • Docker 篇-Docker 详细安装、了解和使用 Docker 核心功能(数据卷、自定义镜像 Dockerfile、网络)
  • 深挖C++赋值
  • 【免越狱】iOS砸壳 可下载AppStore任意版本 旧版本IPA下载
  • 【python笔记02】面向对象思想
  • Java基础-Java多线程机制
  • MySQL技巧之跨服务器数据查询:基础篇-A数据库与B数据库查询合并--封装到存储过程中
  • MATLAB向量元素的引用
  • leetcode-44-通配符匹配
  • 基于YOLOv8深度学习的智慧课堂学生专注度检测系统(PyQt5界面+数据集+训练代码)
  • vue项目使用eslint+prettier管理项目格式化
  • Java基础-组件及事件处理(中)
  • UNIX网络编程-TCP套接字编程(实战)
  • python编写一个自动清理三个月以前的邮件脚本
  • C++组合复用中,委托的含义与作用
  • 自制C++游戏头文件:C++自己的游戏头文件!!!(后续会更新)
  • java 读取 有时需要sc.nextLine();读取换行符 有时不需要sc.nextLine();读取换行符 详解
  • Redis知识分享(三)
  • python安装包报错
  • Linux性能优化之火焰图简介
  • Unity类银河战士恶魔城学习总结(P129 Craft UI 合成面板UI)
  • linux基础笔试练习题笔记(2)
  • Android OpenGL ES详解——glTexImage2D方法
  • Redisson 中开启看门狗(watchdog)机制
  • 【JSOO】设计模式
  • 本草纲目数字化:Spring Boot在中药实验管理中的应用
  • java八股-jvm入门-程序计数器,堆,元空间,虚拟机栈,本地方法栈,类加载器,双亲委派,类加载执行过程
  • 重构Action-cli前端脚手架
  • 华为USG5500防火墙配置NAT
  • 【大数据学习 | HBASE高级】hive操作hbase
  • 集群聊天服务器(9)一对一聊天功能