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

相关推荐
风象南1 小时前
我把大脑开源给了AI
人工智能·后端
橙序员小站6 小时前
Agent Skill 是什么?一文讲透 Agent Skill 的设计与实现
前端·后端
怒放吧德德6 小时前
Netty 4.2 入门指南:从概念到第一个程序
java·后端·netty
雨中飘荡的记忆8 小时前
大流量下库存扣减的数据库瓶颈:Redis分片缓存解决方案
java·redis·后端
开心就好20259 小时前
UniApp开发应用多平台上架全流程:H5小程序iOS和Android
后端·ios
悟空码字9 小时前
告别“屎山代码”:AI 代码整洁器让老项目重获新生
后端·aigc·ai编程
小码哥_常9 小时前
大厂不宠@Transactional,背后藏着啥秘密?
后端
奋斗小强9 小时前
内存危机突围战:从原理辨析到线上实战,彻底搞懂 OOM 与内存泄漏
后端
小码哥_常10 小时前
Spring Boot接口防抖秘籍:告别“手抖”,守护数据一致性
后端
心之语歌10 小时前
基于注解+拦截器的API动态路由实现方案
java·后端