【3】BeanFactory 和AppliactionContext区别
来源:互联网 发布:ps3模拟器og数据损坏 编辑:程序博客网 时间:2024/06/15 23:09
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)方法
0 0
- 【3】BeanFactory 和AppliactionContext区别
- Spring学习之AppliactionContext与BeanFactory的区别
- BeanFactory和ApplicationContext区别
- spring_applicationContext和beanFactory区别
- BeanFactory 和ApplicationContext区别
- BeanFactory和ApplicationContext 区别
- BeanFactory 和 ApplicationContext的区别
- BeanFactory 和 ApplicationContext的区别
- BeanFactory 和 ApplicationContext的区别
- BeanFactory 和 ApplicationContext的区别
- BeanFactory 和 ApplicationContext的区别
- BeanFactory 和FactoryBean的区别
- BeanFactory和FactoryBean的区别
- Spring--ApplicationContext和BeanFactory区别
- ApplicationContext和BeanFactory的区别
- BeanFactory 和 ApplicationContext的区别
- BeanFactory 和 ApplicationContext的区别
- BeanFactory和FactoryBean的区别
- RAS数字系统简图
- HDU 2087剪花布条
- 反向代理服务器的工作原理
- IOS-UITextView、UITextField键盘回收的几种方法
- Linux学习笔记(一)
- 【3】BeanFactory 和AppliactionContext区别
- cp提示overwrite
- UML建模学习
- 获取不固定大小的网络图片,然后在UITableviewCell中显示
- 【Windows】线程漫谈——线程基础(一)
- 实时检测网络状态
- 浅析CVE-2009-2692
- php 伪静态(url rewrite)apache配置!
- Android编译器优化导致的奇怪问题