理解源码的过程中,配合流程图会更容易哦:
Bean定义的几种方式
spring提供了3种声明bean的方式:
- 在配置类中(@Configuration注解的类)使用@bean注解声明bean;
- 使用@ComponentScan注解,默认它会扫描指定路径中的带有@Component注解及其子注解(@Controller @Service @Repository)的类,并将它们注册为bean
- 使用@Import注解 其value可以配置三种类:
- 要导入的具体类,spring会将该类注册为bean,bean名称为类全限定名
- 或者实现ImportSelector接口的类,spring会调用ImportSelector.selectImports方法,将返回的类注册为bean
- 或者实现ImportBeanDefinitionRegistrar接口的类,spring会调用registerBeanDefinitions方法,注册自定义bean
例如有配置类,包括了以上所有的声明bean的方式:
@Configuration
@Import(value = {PersonForImportDirectClass.class,
PersonForImportSelector.class,
PersonImportBeanDefinitionRegistrar.class})
@ComponentScan
public class MainConfig {
@Bean
public PersonForBeanAnno personForBeanAnno(){
return new PersonForBeanAnno();
}
}
其中依赖的类列举如下:
public class PersonForImportDirectClass {
}
public class PersonImportSelector implements ImportSelector {
@Override
public String[] selectImports(AnnotationMetadata importingClassMetadata) {
return new String[]{"com.zyy.sc.analysis.framework.debugiocstart.PersonForImportSelector"};
}
}
public class PersonImportBeanDefinitionRegistrar implements ImportBeanDefinitionRegistrar {
@Override
public void registerBeanDefinitions(AnnotationMetadata importingClassMetadata, BeanDefinitionRegistry registry) {
RootBeanDefinition beanDefinition = new RootBeanDefinition(PersonForImportDefinitionRegistrar.class);
registry.registerBeanDefinition("personForImportDefinitionRegistrar", beanDefinition);
}
}
@Component
public class PersonWithComponentAnno {
}
public class PersonForBeanAnno {
}
public class PersonForImportDefinitionRegistrar {
}
public class PersonForImportSelector {
}
使用测试代码打印spring容器中的bean定义名称:
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(MainConfig.class);
printCustomSpringBeanName(context);
}
private static void printCustomSpringBeanName(ApplicationContext context) {
String[] names = context.getBeanDefinitionNames();
for (String name : names) {
if(name.contains("spring")){
continue;
}
System.out.println("beanName: " + name);
}
}
}
打印结果:
beanName: mainConfig
beanName: personWithComponentAnno
beanName: com.zyy.sc.analysis.framework.debugiocstart.PersonForImportDirectClass
beanName: com.zyy.sc.analysis.framework.debugiocstart.PersonForImportSelector
beanName: personForBeanAnno
beanName: personForImportDefinitionRegistrar
可以看到3种方式定义的5个bean都注册到容器中了。
AnnotationConfigApplicationContext 实例化解析
当我们new AnnotationConfigApplicationContext(MainConfig.class);
来初始化context时
AnnotationConfigApplicationContext的构造方法:
public AnnotationConfigApplicationContext(Class<?>... annotatedClasses) {
//创建beanFactory,注册spring容器的内部组件。
this();
//注册配置类
register(annotatedClasses);
//刷新容器
refresh();
}
执行this()时,会先初始化其父类GenericApplicationContext,此时就会创建一个beanfactory出来:
GenericApplicationContext的构造方法:
public GenericApplicationContext() {
//实例化DefaultListableBeanFactory,保存到context的beanFactory属性中。
this.beanFactory = new DefaultListableBeanFactory();
}
然后执行AnnotationConfigApplicationContext的无参构造方法:
public AnnotationConfigApplicationContext() {
//实例化AnnotatedBeanDefinitionReader并保存到reader属性中,这个方法里面会注册spring的一系列内部组件, 注册的地方就是当前传入this,最终会注册到 this.beanFactory 对象中。
this.reader = new AnnotatedBeanDefinitionReader(this);
//实例化ClassPathBeanDefinitionScanner 并保存到scanner中,这个类就是用来扫描bean的,默认会将@Component注解包含到includeFilters里面去。
this.scanner = new ClassPathBeanDefinitionScanner(this);
}
在new AnnotatedBeanDefinitionReader(this);
中,注册spring内部组件的核心代码是AnnotationConfigUtils的registerAnnotationConfigProcessors方法(为了源码主线清晰,省略一些源码):
public static Set<BeanDefinitionHolder> registerAnnotationConfigProcessors(
BeanDefinitionRegistry registry, @Nullable Object source) {
//获取到前面实例化的beanFactory
DefaultListableBeanFactory beanFactory = unwrapDefaultListableBeanFactory(registry);
....
Set<BeanDefinitionHolder> beanDefs = new LinkedHashSet<>(4);
//注册spring内部组件 ConfigurationClassPostProcessor,这是一个BeanDefinitionRegistryPostProcessor,我们声明的bean,就是通过这个组件注册到spring容器中的。
if (!registry.containsBeanDefinition(CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(ConfigurationClassPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME));
}
//注册spring内部组件AutowiredAnnotationBeanPostProcessor, 这是一个BeanPostProcessor,它用来解析Autowired自动装配的。
if (!registry.containsBeanDefinition(AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(AutowiredAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, AUTOWIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
}
//注册spring内部组件RequiredAnnotationBeanPostProcessor,这是一个BeanPostProcessor,它用来解析自动装配时Required注解的(不常用)
if (!registry.containsBeanDefinition(REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(RequiredAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, REQUIRED_ANNOTATION_PROCESSOR_BEAN_NAME));
}
//如果支持JSR-250规范,用CommonAnnotationBeanPostProcessor这个 BeanPostProcessor 来处理如@Resource @PostConstruct @PreDestroy 等这些注解。
// Check for JSR-250 support, and if present add the CommonAnnotationBeanPostProcessor.
if (jsr250Present && !registry.containsBeanDefinition(COMMON_ANNOTATION_PROCESSOR_BEAN_NAME)) {
RootBeanDefinition def = new RootBeanDefinition(CommonAnnotationBeanPostProcessor.class);
def.setSource(source);
beanDefs.add(registerPostProcessor(registry, def, COMMON_ANNOTATION_PROCESSOR_BEAN_NAME));
}
//其他的内部处理器类似,后面涉及到具体处理器时再详细解释。
....
return beanDefs;
}
这里面的注册方法registerPostProcessor(registry, def, CONFIGURATION_ANNOTATION_PROCESSOR_BEAN_NAME)
,核心逻辑在 DefaultListableBeanFactory.registerBeanDefinition方法上(同样保留主线):
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
//在beanDefinitionMap中获取 BeanDefinition。beanDefinitionMap 是我们的beanFactory核心属性,它保存了所有注册的BeanDefinition。
BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName);
if (existingDefinition != null) {
//如果存在时,根据allowBeanDefinitionOverriding属性来决定是否覆盖,默认是可以覆盖的。
if (!isAllowBeanDefinitionOverriding()) {
throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
"Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +
"': There is already [" + existingDefinition + "] bound.");
}
//省略一些警告日志
.....
//使用相同名字覆盖已经存在的 BeanDefinition 。
this.beanDefinitionMap.put(beanName, beanDefinition);
}
else { 是一个新的 BeanDefinition,
//如果容器已经在开始创建bean实例了,为了保证bean的实例创建稳定,需要同步后在处理。
if (hasBeanCreationStarted()) {
// Cannot modify startup-time collection elements anymore (for stable iteration)
synchronized (this.beanDefinitionMap) {
//注册处理
....
}
}
else {
//如果还是在注册阶段,则beanDefinitionMap加入bean定义信息,beanDefinitionNames加入beanName,以及manualSingletonNames删除beanName。正常情况下一般都是走这个分支的多。
// Still in startup registration phase
this.beanDefinitionMap.put(beanName, beanDefinition);
this.beanDefinitionNames.add(beanName);
this.manualSingletonNames.remove(beanName);
}
this.frozenBeanDefinitionNames = null;
}
if (existingDefinition != null || containsSingleton(beanName)) {
//如果是存在 BeanDefinition 且单实例缓存中存在这个bean,则需要清理这个beanName对应的所有缓存信息。
resetBeanDefinition(beanName);
}
}
至此,AnnotatedBeanDefinitionReader这个实例化完成,其核心是往beanfactory中注册了spring内部处理组件。
执行new ClassPathBeanDefinitionScanner(this);
时,会调用到ClassPathBeanDefinitionScanner的构造方法:
public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
Environment environment, @Nullable ResourceLoader resourceLoader) {
Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
this.registry = registry;
//如果useDefaultFilters属性为会注册默认的includeFilters,将@Component注解加入进去
if (useDefaultFilters) {
registerDefaultFilters();
}
setEnvironment(environment);
setResourceLoader(resourceLoader);
}
registerDefaultFilters方法核心代码就一行:
protected void registerDefaultFilters() {
this.includeFilters.add(new AnnotationTypeFilter(Component.class));
...
}
现在 ClassPathBeanDefinitionScanner 也实例化好了。AnnotationConfigApplicationContext 初始化第一步this()
就完成了。
AnnotationConfigApplicationContext初始化第二步register(annotatedClasses);
,将配置类也注册到beanfactory中,具体会调用第一步中实例化的 AnnotatedBeanDefinitionReader的register方法:
public void register(Class<?>... annotatedClasses) {
for (Class<?> annotatedClass : annotatedClasses) {
registerBean(annotatedClass);
}
}
核心逻辑方法是 doRegisterBean (保留主线代码):
<T> void doRegisterBean(Class<T> annotatedClass, @Nullable Supplier<T> instanceSupplier, @Nullable String name,
@Nullable Class<? extends Annotation>[] qualifiers, BeanDefinitionCustomizer... definitionCustomizers) {
//通过配置类,构建AnnotatedGenericBeanDefinition对象
AnnotatedGenericBeanDefinition abd = new AnnotatedGenericBeanDefinition(annotatedClass);
//设置AnnotatedGenericBeanDefinition的一系列的属性
...
//BeanDefinitionHolder 翻译为BeanDefinition持有器,就是将BeanDefinition,beanName,aliases包装成一个对象。spring中类似的辅助类有很多。
BeanDefinitionHolder definitionHolder = new BeanDefinitionHolder(abd, beanName);
definitionHolder = AnnotationConfigUtils.applyScopedProxyMode(scopeMetadata, definitionHolder, this.registry);
//这儿就是真正的注册,其中this.registry就是在实例化AnnotatedBeanDefinitionReader传入的 AnnotationConfigApplicationContext,但其注册相关的方法都是委托给其属性beanFactory,因此最终会调用DefaultListableBeanFactory.registerBeanDefinition方法上(该方法的源码在前面已经分析过了)
BeanDefinitionReaderUtils.registerBeanDefinition(definitionHolder, this.registry);
}
现在,我们的配置类也注册到了beanfactory中了。
接下来就是最重要也最复杂的refresh方法,spring容器的bean定义加载、单例bean的初始化等都是在这里实现,它是由 AbstractApplicationContext 这个抽象来实现的:
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
// Prepare this context for refreshing.
prepareRefresh();
// Tell the subclass to refresh the internal bean factory.
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
// Prepare the bean factory for use in this context.
prepareBeanFactory(beanFactory);
try {
// Allows post-processing of the bean factory in context subclasses.
postProcessBeanFactory(beanFactory);
//调用BeanFactoryPostProcessors。
// Invoke factory processors registered as beans in the context.
invokeBeanFactoryPostProcessors(beanFactory);
// Register bean processors that intercept bean creation.
registerBeanPostProcessors(beanFactory);
// Initialize message source for this context.
initMessageSource();
//初始化多播器
// Initialize event multicaster for this context.
initApplicationEventMulticaster();
// Initialize other special beans in specific context subclasses.
onRefresh();
//注册监听器到前面初始化的多播器中,并用多播器发布早期的事件。
// Check for listener beans and register them.
registerListeners();
// Instantiate all remaining (non-lazy-init) singletons.
finishBeanFactoryInitialization(beanFactory);
// Last step: publish corresponding event.
finishRefresh();
}
catch (BeansException ex) {
if (logger.isWarnEnabled()) {
logger.warn("Exception encountered during context initialization - " +
"cancelling refresh attempt: " + ex);
}
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
finally {
// Reset common introspection caches in Spring's core, since we
// might not ever need metadata for singleton beans anymore...
resetCommonCaches();
}
}
}
这里面包括十几个大的步骤,这儿我们先分析简单spring中的多播器和监听器。
在上一篇的基础概念文章中,已经介绍了sprig的ApplicationEventMulticaster & ApplicationListener 组件,这儿就直接分析源码。
initApplicationEventMulticaster方法的代码:
protected void initApplicationEventMulticaster() {
//获取beanFactory
ConfigurableListableBeanFactory beanFactory = getBeanFactory();
//如果容器中已经有了名为“applicationEventMulticaster”的bean,则将其赋值给context的applicationEventMulticaster属性。
if (beanFactory.containsLocalBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME)) {
this.applicationEventMulticaster =
beanFactory.getBean(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, ApplicationEventMulticaster.class);
if (logger.isDebugEnabled()) {
logger.debug("Using ApplicationEventMulticaster [" + this.applicationEventMulticaster + "]");
}
}
else {
//如果没有则创建一个 SimpleApplicationEventMulticaster 实例,则将其赋值给context的applicationEventMulticaster属性。然后将其注册到beanfactory的单例缓存中去。注意一点的是,它并没有注册beandefinition到beanfactory中。
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isDebugEnabled()) {
logger.debug("Unable to locate ApplicationEventMulticaster with name '" +
APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
"': using default [" + this.applicationEventMulticaster + "]");
}
}
}
registerListeners方法的代码:
protected void registerListeners() {
//注册静态指定的ApplicationListener(也就是在初始化多播器之前,硬编码调用context.addApplicationListener()方法传入的监听器)
// Register statically specified listeners first.
for (ApplicationListener<?> listener : getApplicationListeners()) {
getApplicationEventMulticaster().addApplicationListener(listener);
}
//注册 通过bean声明方式,注册到beanfactory的ApplicationListener,注意和上面的区别,上面注册的监听器实例,而这儿注册的是监听器的beanName。
// Do not initialize FactoryBeans here: We need to leave all regular beans
// uninitialized to let post-processors apply to them!
String[] listenerBeanNames = getBeanNamesForType(ApplicationListener.class, true, false);
for (String listenerBeanName : listenerBeanNames) {
getApplicationEventMulticaster().addApplicationListenerBean(listenerBeanName);
}
//发布早期事件。早期事件就是在多播器初始化之前,调动context.publishEvent方法时,会将事件保存在earlyApplicationEvents属性中。当程序执行到这儿时,多播器和监听器都已经准备好了,此时就可以将前面保存的事件真正的发布出去了。
// Publish early application events now that we finally have a multicaster...
Set<ApplicationEvent> earlyEventsToProcess = this.earlyApplicationEvents;
this.earlyApplicationEvents = null;
if (earlyEventsToProcess != null) {
for (ApplicationEvent earlyEvent : earlyEventsToProcess) {
getApplicationEventMulticaster().multicastEvent(earlyEvent);
}
}
}
发布事件的核心代码在 SimpleApplicationEventMulticaster.multicastEvent 方法中:
public void multicastEvent(final ApplicationEvent event, @Nullable ResolvableType eventType) {
ResolvableType type = (eventType != null ? eventType : resolveDefaultEventType(event));
//遍历和事件类型匹配的监听器,如果多播器设置了taskExecutor属性,则异步执行监听器代码,否则就直接调用监听器处理事件。
for (final ApplicationListener<?> listener : getApplicationListeners(event, type)) {
Executor executor = getTaskExecutor();
if (executor != null) {
executor.execute(() -> invokeListener(listener, event));
}
else {
invokeListener(listener, event);
}
}
}
invokeListener方法最终会调到doInvokeListener方法上:
private void doInvokeListener(ApplicationListener listener, ApplicationEvent event) {
try {
//调用监听器的onApplicationEvent方法。
listener.onApplicationEvent(event);
}
catch (ClassCastException ex) {
...
}
}
到此,refresh方法中比较简单的 initApplicationEventMulticaster() 和 registerListeners() 这两步操作源码解析完成。当然其中还有些细节没有详细解析(不影响理解spring的原理),比如getApplicationListeners(event, type)这个获取和类型匹配的监听器中,用到了缓存,事件类型支持校验等,这个就留给你自己去分析吧。
下一篇文章,将解析 invokeBeanFactoryPostProcessors(beanFactory); 这一步操作,它会调用已经注册到容器中的ConfigurationClassPostProcessor,来解析配置类,将声明的bean注册到beanfactory中去。