BeanFactory :是spring框架的基础设施,面向spring本身。会延迟加载,只是将xml解析,将类里面的信息记录下来。
AppliactionContext:应用上下文,面向spring开发者。加载就一次性实例化。
AppliactionContext 是 extends BeanFactory
代码层次:
一、立即实例化
ApplicationContext ac = new ClassPathXmlApplicationContext("jdbcconfig/springjdbc/springjdbc.xml");
在new的时候 通过构造方法就直接调用了 AbstractApplicationContext里面的 refresh()方法
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);/
/开始实例化类了会调用 AbstractBeanFactory 的 doGetBean()方法。
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) {
// Destroy already created singletons to avoid dangling resources.
destroyBeans();
// Reset 'active' flag.
cancelRefresh(ex);
// Propagate exception to caller.
throw ex;
}
}
}
二、延迟实例化
XmlBeanFactory bf = new XmlBeanFactory(res);
构造方法里面 相等于调用了
AbstractApplicationContext里面的 refresh()方法里面的
obtainFreshBeanFactory()
ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();
XmlWebApplicationContext==》AbstractRefreshableApplicationContext ==》 AbstractApplicationContext 父类
loadBeanDefinitions()方法 refreshBeanFactory()方法实现类 refreshBeanFactory()抽象方法、 refresh()方法
getBeanFactory()实现类
实例化过程
ac.getBean();
AbstractApplicationContext类里面的
public Object
getBean(String name) throws BeansException {
assertBeanFactoryActive();
return getBeanFactory().getBean(name);
}
AbstractRefreshableApplicationContext
public final ConfigurableListableBeanFactory getBeanFactory() {
synchronized (this.beanFactoryMonitor) {
if (this.beanFactory == null) {
throw new IllegalStateException("BeanFactory not initialized or already closed - " +
"call 'refresh' before accessing beans via the ApplicationContext");
}
return this.beanFactory;
}
}
AbstractBeanFactory extends
FactoryBeanRegistrySupport implements
ConfigurableBeanFactory
AbstractBeanFactory//
public Object getBean(String name) throws BeansException {
return doGetBean(name, null, null, false);
}
protected <T> T doGetBean()方法里面的
Object sharedInstance =
getSingleton(beanName);//获取实例化,如果已经实例化了,就不用再实例化了
sharedInstance =
getSingleton(beanName, new ObjectFactory<Object>() {//这个是创建Create bean instance.
Map<String, Object>
singletonObjects = new ConcurrentHashMap<String, Object>(64);//实例化的对象放在这里面
xbf.getBean();
T doGetBean(
final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)方法