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

Spring的加载配置文件、容器和获取bean的方式


在这里插入图片描述

🐌个人主页: 🐌 叶落闲庭
💨我的专栏:💨
c语言
数据结构
javaweb

石可破也,而不可夺坚;丹可磨也,而不可夺赤。


Spring配置文件和容器相关

  • 一、加载properties文件
    • 1.1加载单个properties文件
    • 1.2加载多个properties文件
    • 1.3加载properties文件小结
  • 二、容器
    • 2.1创建容器
      • 2.1.1加载类路径下的配置文件
      • 2.1.2从文件系统下加载配置文件(了解)
      • 2.1.3加载多个配置文件
    • 2.2获取bean
      • 2.2.1类型强转
      • 2.2.2多个参数
      • 2.2.3按类型获取
  • 总结

一、加载properties文件

1.1加载单个properties文件

  • properties文件:jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis
jdbc.username=root
jdbc.password=123456
  • 1.开启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>
  • 2.使用context命名空间,加载指定properties文件
<context:property-placeholder location="jdbc.properties"/><context:property-placeholder location="jdbc.properties"/>
  • 3.使用属性占位符${}读取properties文件中的属性
<bean  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>

在这里插入图片描述

1.2加载多个properties文件

  • properties文件1:jdbc.properties
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://127.0.0.1:3306/mybatis
jdbc.username=root
jdbc.password=123456
  • properties文件2:jdbc2.properties
username=zhangsan
  • 2.使用context命名空间,加载指定properties文件
<!--1.开启context命名空间--><!--2.使用context空间加载properties文件--><context:property-placeholder location="jdbc.properties,jdbc2.properties" system-properties-mode="NEVER"/><!--spring加载配置文件--><!--3.使用属性占位符${}读取properties文件中的属性--><bean  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.practice.dao.impl.BookDaoImpl"><property name="name" value="${jdbc.username}"/><property name="name2" value="${username}"/></bean>

在这里插入图片描述

  • 3.最理想的方式,使用*.properties即表示加载所有properties文件
    <context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/>

1.3加载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"/>

二、容器

2.1创建容器

2.1.1加载类路径下的配置文件

public class App {public static void main(String[] args) {ApplicationContext act = new ClassPathXmlApplicationContext("applicationContext.xml");BookDao bookDao = (BookDao) act.getBean("bookDao");bookDao.save2();}
}

在这里插入图片描述

2.1.2从文件系统下加载配置文件(了解)

public class App {public static void main(String[] args) {//从文件系统下加载配置文件,参数为配置文件的绝对路径ApplicationContext act = new FileSystemXmlApplicationContext("D:\\storage\\java_practice\\spring-7-30\\src\\main\\resources\\applicationContext.xml");BookDao bookDao = (BookDao) act.getBean("bookDao");bookDao.save2();}
}

2.1.3加载多个配置文件

        ApplicationContext act = new ClassPathXmlApplicationContext("bean1.xml","bean2.xml");

2.2获取bean

2.2.1类型强转

        BookDao bookDao = (BookDao) act.getBean("bookDao");

2.2.2多个参数

        BookDao bookDao = act.getBean("bookDao",BookDao.class);

2.2.3按类型获取

  • 对应的bean中此类型只能有一个
        BookDao bookDao = act.getBean(BookDao.class);

总结

关于Spring的加载配置文件、容器和获取bean的方式大概就这么多,欢迎各位小伙伴点赞+关注!!!

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

相关文章:

  • (二)利用Streamlit创建第一个app——单页面、多页面
  • 一条sql查询语句在mysql中的执行过程是什么
  • 网络互联究竟是需要什么协议相同,什么协议不同?
  • ajax axios json
  • 外观模式——提供统一入口
  • Vue中导入并读取Excel数据
  • CUDA常用函数
  • 72. ElasticSearch常用命令
  • 2023.7.26(同余方程的通解与特解)
  • Diffusion扩散模型学习3——Stable Diffusion结构解析-以图像生成图像(图生图,img2img)为例
  • LangChain||什么是LangChain? LangChain有什么用?
  • 秋招算法备战第28天 | 93.复原IP地址、78.子集、90.子集II
  • Mongodb空间索引的使用以及与Django的对接
  • Windows安装MySQL数据库
  • 聊聊函数式编程中的“式”
  • ubuntu目录分析
  • Python 进阶(三):正则表达式(re 模块)
  • Vue2 第六节 key的作用与原理
  • React之组件的生命周期
  • linux -网络编程-多线程并发服务器
  • Golang之路---02 基础语法——字典
  • Pytorch(三)
  • Linux——进程控制
  • 剑指 Offer 59 - I. 滑动窗口的最大值 / LeetCode 239. 滑动窗口最大值(优先队列 / 单调队列)
  • 【Linux后端服务器开发】IP协议
  • React组件进阶之children属性,props校验与默认值以及静态属性static
  • ceph集群中RBD的性能测试、性能调优
  • texshop mac中文版-TeXShop for Mac(Latex编辑预览工具)
  • 简单认识redis高可用实现方法
  • 搭建git服务器