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.UserRegisteredEventsource=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")
相关推荐
IT_陈寒23 分钟前
JavaScript项目实战经验分享
前端·人工智能·后端
用户47949283569151 小时前
6w star,GitHub 趋势第一的 Ponytail,这个agent插件到底在火什么
前端·后端
吃饱了得干活2 小时前
Spring Cloud Gateway 微服务网关:路由、断言、过滤器
java·spring cloud
神奇小汤圆2 小时前
2026一线大厂Java八股文精选(附答案,高质量整理)
后端
Warson_L3 小时前
LangGraph入门学习资料
后端
神奇小汤圆3 小时前
Spring Boot → Solon 注解迁移实战指南:一张对照表说清楚
后端
kfaino3 小时前
码农的AI翻身(四)你好,我叫 Attention
人工智能·后端
lwx572803 小时前
探秘InnoDB:搞懂它的内存、线程、磁盘与日志刷盘策略
java·后端
云技纵横5 小时前
Spring Boot Actuator 被打穿:线上开了这些端点,等于裸奔
后端
Flynt5 小时前
从Spring Boot 4.0升到4.1,我在Maven和gRPC上栽了跟头
java·spring boot·后端