Spring进阶(BeanFactory与ApplicationContext)

前言:

在使用Spring(包括SpringBoot与SpringCloud)做了一些项目之后,总感觉有点机械式的完成任务,为了更好的理解Spring,开始更加深入学习Spring底层实现原理。

BeanFactory:

可以将方法、类在启动Spring之后注册成Bean对象交由Spring管理,这个过程涉及到BeanFactory的相关操作。

BeanFactory能做哪些事:

一般启动SpringBoot项目都需要这样写:

java 复制代码
@SpringBootApplication
public class A01Application {
    public static void main(String[] args) {
        ConfigurableApplicationContext context = SpringApplication.run(A01Application.class, args);
    }
}

使用 SpringApplication.run 方法,其中返回值为 ConfigurableApplicationContext,ConfigurableApplicationContext关系图如下:

发现两个关键点,该接口继承了一个核心接口ApplicationContext,并且ApplicationContext又间接继承了BeanFactory接口,因此可以说ApplicationContext接口是对BeanFactory进行了功能方面的扩展!

因此Spring的核心是BeanFactory!!

当然BeanFactory是一个接口,具体的实现类时则为DefaultListableBeanFactory。

java 复制代码
public class DefaultListableBeanFactory extends AbstractAutowireCapableBeanFactory implements ConfigurableListableBeanFactory, BeanDefinitionRegistry, Serializable 

当然该实现类不只实现了一个接口,还包括一系列的扩展类和接口,其中注册的Bean默认是单例的,主要是由 DefaultSingletonBeanRegistry类完成注册以及存放

java 复制代码
    private final Map<String, Object> singletonObjects = new ConcurrentHashMap(256);

可以利用反射的原理,将私有成员变量拿出来一探究竟:

java 复制代码
 Field singletonObject = DefaultSingletonBeanRegistry.class.getDeclaredField("singletonObjects");
        singletonObject.setAccessible(true);
        ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
        Map<String,Object> map = (Map<String,Object>)singletonObject.get(beanFactory);
        map.forEach((k,v)->{
            System.out.println(k+"="+v);
        });

当然由于SpringApplication容器中初始化之后Bean对象设计的较多,看起来是比较费劲~

ApplicationContext:

接下来需要研究一下ApplicationContext对BeanFactory的扩展:

MessageSource:国际化资源,根据不同的区域提供翻译功能。

java 复制代码
context.getMessage("hi",null, Locale.CHINA);

ResourcePatternResolver:根据通配符匹配多个资源(磁盘路径等)

java 复制代码
context.getResources("classpath:application.properties")

ApplicationEventPublicsher:发布事件

1.创建事件:

java 复制代码
public class UserRegisteredEvent extends ApplicationEvent {

    public UserRegisteredEvent(Object source) {
        super(source);
    }
}

2.发送事件:

java 复制代码
context.publishEvent(new UserRegisteredEvent(context));

3.接收事件:

java 复制代码
@Component
public class Component2 {

    @EventListener
    public void reviver(UserRegisteredEvent event) {
        System.out.println(event);
    }
}

结果如下:

test.a01.UserRegisteredEvent[source=org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@2cc3ad05, started on Sun May 17 00:25:57 CST 2026]

EnvironmentCapable:读取环境资源(系统资源、yaml等)

java 复制代码
context.getEnvironment().getProperty("JAVA_HOME")
相关推荐
吴声子夜歌1 小时前
Java——反射
java·反射
zhangxingchao1 小时前
AI应用开发一: AI 编程、大模型调用和 Agent
前端·人工智能·后端
JAVA面经实录9172 小时前
完整版JVM 深度学习体系(二)
java·jvm
m0_702036532 小时前
html标签如何提升可访问性_aria-label与title区别【指南】
jvm·数据库·python
.ZGR.2 小时前
线程池相关知识及并发统计案例实现
java·开发语言
会编程的土豆2 小时前
Gin 核心概念速记
数据库·后端·gin·goland
Mr_pyx2 小时前
面试题记录
jvm·数据结构·算法·spring·mybatis
ㄟ留恋さ寂寞2 小时前
Golang格式化输出占位符都有什么_Golang fmt占位符教程【通俗】
jvm·数据库·python
慕言手记2 小时前
IDEA 插件常用-2026版
java·ide·spring boot·intellij-idea·idea·intellij idea