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的方式大概就这么多,欢迎各位小伙伴点赞+关注!!!

相关推荐
亭台烟雨中几秒前
简单实现shardingSphere + MybatisPlus分库分表2025
java·数据库·分库分表
forestsea17 分钟前
Maven 构建性能优化深度剖析:原理、策略与实践
java·性能优化·maven
这里有鱼汤28 分钟前
为什么我现在做Python项目都用UV?你看完就懂了
后端·python
URBBRGROUN46728 分钟前
邮件限流器
java·前端·数据库
Seven9730 分钟前
线程池中execute和submit的区别?
java
面朝大海,春不暖,花不开32 分钟前
使用Spring Boot Actuator构建用户应用
java·spring boot·后端
程序员JerrySUN32 分钟前
深入理解 Linux Kernel Panic:常见原因与实战分析
android·java·linux
寻月隐君41 分钟前
探索Solana SDK实战:Web3开发的双路径与轻量模块化
后端·web3·github
斯普信专业组1 小时前
Etcd数据持久化机制:WAL与Snapshot解析
java·数据库·etcd