1、spring的依赖查找和依赖注入
依赖查找 DL Dependency Lookup 依赖查找是一种主动的行为,来解决对象的依赖问题,其更加直观可读,但是代码侵入性较高,较高依赖 API接口
比如使用 BeanFactory.getBean() 方法实时查找就是一种依赖查找的方式,或者通过 FactoryObject 函数式接口进行延迟查找 (注意这里和懒加载是两回事)
依赖注入 DI Dependency Injection 依赖注入是一种被动的注入行为,来解决对象的依赖问,其代码入侵性较小,可以较低或者不依赖于 API 接口,但是可读性相对较差
如我们说的 Spring 就是依赖注入的方式,体现于 @Autowired 注解、@Recourse 注解等
依赖注入的三种方式: 接口注入、构造器注入、setter 注入
2、springcontext的生命周期
1:refresh
2:start
3:stop
4:close
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);
// 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();
}
}
}
总体的思路如下:
1:准备环境 prepareRefresh():
2:获取BeanFactory obtainFreshBeanFactory:ClassPathXmlApplicationContext在refreshBeanFactory的时候加载了configlocation路径上beanDefinition,
3:准备BeanFactory prepareBeanFactory,准备beanFactory,注册比较特殊的几个Bean,比如ClassLoader,PropertyEditorRegisrtrar,Environment LoadTimeWeaver,忽略掉一些不必要的接口实现
4:留给子类扩展BeanFactory:postProcessBeanFactory() 模版方法准备好了BeanFactory,但是留给了子类的拓展接口
5:执行BeanFactoryPostProcessor:invokeBeanFactoryPostProcessors();调用BeanFactoryBeanPostProcessor的实现,来处理BeanFactory,特别的是AnnotationConfigApplicationContext在这个步骤CommonConfigPostProcessor
6:初始化消息源:initMessageSource(),我们在i18章节讲到过
7:初始化事件广播器:initApplicationEventMutilcaster()
8:留给子类扩展 context: onRefresh();在 留给context的子类一个扩展口,来实例化一些特殊作用的bean,
9:注册事件监听器 :registerListener()
10:实例化所有非延迟加载的bean: finishBeanFactoryInitialization(beanFactory);这些bean都是在obtainBeanFactory加载的,在InvokeBeanFactoryPostProcessor时处理的@ComponentScan组建,@Configuration @Import @Bean等新加入的beanDefinition
11:清除上面扫描反射加载等操作的缓存:
12:lifeCycle组建启动
13:广播事件 refreshEvent ,
14:注册MBean
15:重制缓存:finanlly{}模块