java容器与spring容器_spring IOC容器实现探讨

publicObject getBean(String name, Class requiredType, Object[] args)throwsBeansException {

String beanName=transformedBeanName(name);

Object bean=null;//Eagerly check singleton cache for manually registered singletons.Object sharedInstance=null;//从单例缓存中获取synchronized(this.singletonCache) {

sharedInstance=this.singletonCache.get(beanName);

}if(sharedInstance!=null) {if(isSingletonCurrentlyInCreation(beanName)) {if(logger.isDebugEnabled()) {

logger.debug("Returning eagerly cached instance of singleton bean '"+beanName+"' that is not fully initialized yet - a consequence of a circular reference");

}

}else{if(logger.isDebugEnabled()) {

logger.debug("Returning cached instance of singleton bean '"+beanName+"'");

}

}

bean=getObjectForSharedInstance(name, sharedInstance);

}else{//Fail if we're already creating this singleton instance://We're assumably within a circular reference.if(isSingletonCurrentlyInCreation(beanName)) {thrownewBeanCurrentlyInCreationException(beanName);

}//Check if bean definition exists in this factory.//检测bean是否定义在父工厂if(getParentBeanFactory()!=null&&!containsBeanDefinition(beanName)) {//Not found -> check parent.if(getParentBeanFactory()instanceofAbstractBeanFactory) {//Delegation to parent with args only possible for AbstractBeanFactory.return((AbstractBeanFactory) getParentBeanFactory()).getBean(name, requiredType, args);

}elseif(args==null) {//No args -> delegate to standard getBean method.returngetParentBeanFactory().getBean(name, requiredType);

}else{thrownewNoSuchBeanDefinitionException(beanName,"Cannot delegate to parent BeanFactory because it does not supported passed-in arguments");

}

}//获取BeanDefinitionRootBeanDefinition mergedBeanDefinition=getMergedBeanDefinition(beanName,false);

checkMergedBeanDefinition(mergedBeanDefinition, beanName, requiredType, args);//Create bean instance.//创建bean,如果为为singletonif(mergedBeanDefinition.isSingleton()) {synchronized(this.singletonCache) {//Re-check singleton cache within synchronized block.sharedInstance=this.singletonCache.get(beanName);if(sharedInstance==null) {if(logger.isDebugEnabled()) {

logger.debug("Creating shared instance of singleton bean '"+beanName+"'");

}this.currentlyInCreation.add(beanName);try{

sharedInstance=createBean(beanName, mergedBeanDefinition, args);

//加进单例缓存

addSingleton(beanName, sharedInstance);

}catch(BeansException ex) {//Explicitly remove instance from singleton cache: It might have been put there//eagerly by the creation process, to allow for circular reference resolution.//Also remove any beans that received a temporary reference to the bean.destroyDisposableBean(beanName);throwex;

}finally{this.currentlyInCreation.remove(beanName);

}

}

}

bean=getObjectForSharedInstance(name, sharedInstance);

}//如果是prototypeelse{//It's a prototype -> create a new instance.bean=createBean(beanName, mergedBeanDefinition, args);

}

}//Check if required type matches the type of the actual bean instance.if(requiredType!=null&&!requiredType.isAssignableFrom(bean.getClass())) {thrownewBeanNotOfRequiredTypeException(name, requiredType, bean.getClass());

}returnbean;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值