• Spring底层原理(一)


    Spring底层原理(一)

    ApplitionContextBeanFactory
    • BeanFactoryApplicationContext的父接口
    • BeanFactory才是Spring的核心容器,ApplicationContext对其功能进行了组合

    类图

    在这里插入图片描述

    内部方法调用

    在这里插入图片描述

    BeanFactory的功能

    在这里插入图片描述

    • 获取bean
    • 检查是否包含bean
    • 获取bean别名

    表面上只有getBean,实际上控制反转、基本的依赖注入、直至Bean的生命周期的各种功能,都由它的实现类提供

    🔖单例bean的管理(了解即可)

    在这里插入图片描述

    在这里插入图片描述

    获取容器中的单例bean

    @SpringBootApplication
    public class SimpleApplication {
        public static void main(String[] args) throws NoSuchFieldException, IllegalAccessException {
            ConfigurableApplicationContext applicationContext = SpringApplication.run(SimpleApplication.class, args);
            Field field = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
            field.setAccessible(true);
            //获取BeanFactory
            ConfigurableListableBeanFactory beanFactory = applicationContext.getBeanFactory();
            Map<String, Object> map = (Map<String, Object>) field.get(beanFactory);
            map.entrySet().stream().filter(e -> e.getKey().startsWith("component"))
                    .forEach(e -> {
                        System.out.println(e.getKey() + "=" + e.getValue());
                    });
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    ApplicationContext的功能

    在这里插入图片描述

    • ApplicationContext除了可以对Bean管理外,还提供了如上四种功能
    国际化功能
    applicationContext.getMessage("msg", null, Locale.CHINA);
    applicationContext.getMessage("msg", null, Locale.US);
    applicationContext.getMessage("msg", null, Locale.JAPAN);
    
    • 1
    • 2
    • 3
    • 可以通过applicationContext提供的getMessage方法对不同语言的资源文件进行读取
    资源匹配功能
    Resource[] resources = applicationContext.getResources("classpath:application.yml");
    for (Resource resource : resources) {
         System.out.println(resource);//class path resource [application.yml]
    }
    
    • 1
    • 2
    • 3
    • 4
    • classpath:获取类路径下的资源
    • classpath*:获取类路径下的资源包含jar包
    • file:获取文件系统下的资源
    环境变量与配置文件读取功能

    在这里插入图片描述

    applicationContext.getEnvironment().getProperty("java_home");//C:\jdk-17
    applicationContext.getEnvironment().getProperty("server.port");//8080
    
    • 1
    • 2
    • 变量名忽略大小写
    事件发布功能

    事件发布指南

    • 事件发布的作用:解耦合
  • 相关阅读:
    springboot+jsp学生心理健康测评网
    java基于springboot的高速公路收费管理系统设计与实现
    现在回头看,你有没有写过自己觉得比较愚蠢的代码?
    如何通过函数获取股票量化交易行情数据?
    bootStrap-switchery插件状态回显问题
    计算机专业毕业设计题目大全——各种类型系统设计大全
    电脑技巧:推荐八个实用的在线学习网站
    算法——单调队列
    Java零基础入门------------笔记一(基础概念1.0)
    VS2019 C#中文控制台显示为问号
  • 原文地址:https://blog.csdn.net/qq_52751442/article/details/133994174