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


🐌个人主页: 🐌 叶落闲庭

💨我的专栏:💨
c语言
数据结构
javaweb

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


Spring配置文件和容器相关

一、加载properties文件

1.1加载单个properties文件

  • properties文件:jdbc.properties
java 复制代码
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 复制代码
<?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
        ">
</beans>
  • 2.使用context命名空间,加载指定properties文件
xml 复制代码
<context:property-placeholder location="jdbc.properties"/><context:property-placeholder location="jdbc.properties"/>
  • 3.使用属性占位符${}读取properties文件中的属性
xml 复制代码
<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
java 复制代码
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
java 复制代码
username=zhangsan
  • 2.使用context命名空间,加载指定properties文件
xml 复制代码
<!--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文件
xml 复制代码
    <context:property-placeholder location="classpath*:*.properties" system-properties-mode="NEVER"/>

1.3加载properties文件小结

  • 不加载系统属性
xml 复制代码
    <context:property-placeholder location="jdbc.properties" system-properties-mode="NEVER"/>
  • 加载多个properties文件
xml 复制代码
    <context:property-placeholder location="jdbc.properties,jdbc2.properties"/>
  • 加载所有properties文件
xml 复制代码
    <context:property-placeholder location="*.properties"/>
  • 加载properties文件标准格式
xml 复制代码
    <context:property-placeholder location="classpath:*.properties"/>
  • 从类路径或jar包搜索并加载properties文件
xml 复制代码
    <context:property-placeholder location="classpath*:*.properties"/>

二、容器

2.1创建容器

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

java 复制代码
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从文件系统下加载配置文件(了解)

java 复制代码
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加载多个配置文件

java 复制代码
        ApplicationContext act = new ClassPathXmlApplicationContext("bean1.xml","bean2.xml");

2.2获取bean

2.2.1类型强转

java 复制代码
        BookDao bookDao = (BookDao) act.getBean("bookDao");

2.2.2多个参数

java 复制代码
        BookDao bookDao = act.getBean("bookDao",BookDao.class);

2.2.3按类型获取

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

总结

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

相关推荐
舌尖上的五香几秒前
ThreadLocal、InheritableThreadLocal、TransmittableThreadLocal
java
okok__TXF2 分钟前
Sentinel入门篇【流量治理】
java·sentinel
谁他个天昏地暗4 分钟前
Java 实现 Excel 文件对比与数据填充
java·开发语言·excel
大P哥阿豪21 分钟前
Go defer(二):从汇编的角度理解延迟调用的实现
开发语言·汇编·后端·golang
今天背单词了吗98027 分钟前
算法学习笔记:11.冒泡排序——从原理到实战,涵盖 LeetCode 与考研 408 例题
java·学习·算法·排序算法·冒泡排序
Brookty35 分钟前
【操作系统】进程(二)内存管理、通信
java·linux·服务器·网络·学习·java-ee·操作系统
风象南35 分钟前
SpringBoot 与 HTMX:现代 Web 开发的高效组合
java·spring boot·后端
wstcl2 小时前
让你的asp.net网站在调试模式下也能在局域网通过ip访问
后端·tcp/ip·asp.net
倔强的小石头_4 小时前
【C语言指南】函数指针深度解析
java·c语言·算法
kangkang-7 小时前
PC端基于SpringBoot架构控制无人机(三):系统架构设计
java·架构·无人机