README
spring 文档首页
代码版本声明
spring-boot-2.3.3.RELEASE.jar
spring-boot-autoconfigure-2.3.3.RELEASE.jar
spring-context-5.2.8.RELEASE.jar
spring-core-5.2.8.RELEASE.jar
spring-beans-5.2.8.RELEASE.jar
spring-aop-5.2.8.RELEASE.jar
spring-web-5.2.8.RELEASE.jar
Bean 的作用范围
| 范围 | 描述 |
|---|---|
| singletion | (默认)将单个 bean 定义限定为每个 Spring IoC 容器的单个对象实例。 |
| prototype | 将单个 bean 定义限定为任意数量的对象实例。 |
| request | 将单个 bean 定义限定为单个 HTTP 请求的生命周期。也就是说,每个 HTTP 请求都有自己的 bean 实例,该实例是在单个 bean 定义的后面创建的。仅在 Web 感知 Spring 的上下文中有效 ApplicationContext。 |
| session | 将单个 bean 定义限定为 HTTP 的生命周期 Session。仅在 Web 感知 Spring 的上下文中有效 ApplicationContext。 |
| application | 将单个 bean 定义限定为 ServletContext. 仅在 Web 感知 Spring 的上下文中有效 ApplicationContext。 |
| websocket | 将单个 bean 定义限定为 WebSocket. 仅在 Web 感知 Spring 的上下文中有效 ApplicationContext。 |
Spring 中常用的接口
Bean 管理
BeanFactory 和 ApplicationContext 是 Spring 中最核心的接口,先放一张核心类图:

BeanFactory
这是 Spring 中最核心的接口,也是根接口。定义了获取 bean 及 bean 基础属性的相关方法。此接口需要重新实现的场景非常少,一般是 Spring 内部配置,程序中注入/获取使用即可。
常用方法:
- getBean 根据 beanId/beanClass 获取 Bean
- containsBean bean 是否已加载
- isSingleton 是否是单例
- isTypeMatch 类型是否匹配
- getType 获取 bean class
- getAliases 获取 bean 别名
ApplicationContext
继承了以下接口:ApplicationEventPublisher, BeanFactory, EnvironmentCapable, HierarchicalBeanFactory, ListableBeanFactory, MessageSource, ResourceLoader, ResourcePatternResolver
同时也实现了以上接口的所有功能,程序中通过此接口就可以使用以上接口的功能。
常用方法:
- getId 获取 spring 容器 ID
- getApplicationName 获取应用明
- getDisplayName 获取 displayName
- getStartupDate 获取启动时间
- getAutowireCapableBeanFactory 获取 autowire 的 beanFactory
Aware 接口
Aware:adj. 意识到的。在程序中一般代表自动发现(个人理解),在 Spring 中的功能就是自动注入。
Spring 定义了一批 Aware 接口,被 Spring 管理的 Bean 只需要实现 XXAware 接口,就可以获取相对应的资源。下面是一些常用的 Aware 接口极其作用:
- BeanNameAware's setBeanName
- BeanClassLoaderAware's setBeanClassLoader
- BeanFactoryAware's setBeanFactory
- EnvironmentAware's setEnvironment
- EmbeddedValueResolverAware's setEmbeddedValueResolver
- ResourceLoaderAware's setResourceLoader
- ApplicationEventPublisherAware's setApplicationEventPublisher
- MessageSourceAware's setMessageSource
- ApplicationContextAware's setApplicationContext
- ServletContextAware's setServletContext (仅适用于 Spring Web 环境下)
Bean 生命周期
FactoryBean(创建 Bean)
如果一个 Bean 实现了此接口,那么该 Bean 将不直接作为最终 Bean 的实例,而是把实现的 getObject 方法返回作为最终的实例。
此接口在框架内部大量使用,例如 AOP ProxyFactoryBean 或 JndiObjectFactoryBean,它也可以用于定制组件。Dubbo 中也通过此接口来实现动态加载生产者,具体参考 com.alibaba.dubbo.config.spring.ReferenceBean
InitializingBean(初始化 Bean)
如果 Bean 实现了此接口,在 Bean 创建完成之后会调用 afterPropertiesSet 方法,程序可以通过此接口方法做一些初始化相关的工作。
BeanPostProcessor(后置处理 Bean)
Factory Hook,允许自定义修改新的 Bean 实例。执行顺序在 afterPropertiesSet 之后。
可以通过此接口实现动态/依赖配置 Bean 的功能,例如 Dubbo 中消费者通过@Reference 引用生产者,具体参考 com.alibaba.dubbo.config.spring.beans.factory.annotation.ReferenceAnnotationBeanPostProcessor
DisposableBean(销毁 Bean)
当 Spring 容器关闭时会进行所有 Bean 的销毁,若 Bean 实现了此接口,则销毁时会调用 destroy 方法。程序可以通过此接口完成一些依赖关闭的功能。
其他
Environment
当前应用正在运行环境的接口,通过此接口可以获得配置文件和属性。该接口还继承了 PropertyResolver,可以获取 placeholder 中的属性值。
常用方法
- getActiveProfiles 获取当前激活的环境
- getDefaultProfiles 获取当前默认环境
- acceptsProfiles 检测环境是否处于激活状态
- containsProperty 是否包含属性 key
- getProperty 根据 key 获取属性
- getRequiredProperty 根据 key 获取属性,若值不存在则抛出
IllegalStateException - resolvePlaceholders 处理 el 表达式,并获取对应值。例如:传入 ${spring.profiles.active},就可以获取对应值。跟 getProperty 不同的是,getProperty 需要的是具体 key 名称,而不是表达式。
- resolveRequiredPlaceholders 同 resolvePlaceholders,不过值不存在会抛出
IllegalStateException
Lifecycle
Spring 容器的生命周期接口,Bean 实现此接口后可以收到 Spring 生命周期变化的调用。
常用方法:
- start 启动事件
- stop 停止事件
- isStarted 判断是否启动