spring IOC 전체 프로 세 스

7164 단어 spring
전체 절 차 는 다음 과 같은 세 단계 로 나 뉜 다.
1. XmlBeanFactory factory = new XmlBeanFactory (new ClassPathResource ("beans. xml");1. 설정 파일 을 찾 는 방법
	1.1            ClassPathResource   
	
		A.      ClassPathResource      :intputStreamResource   Resource 
		Resource        spring          ,           
		   :   ,   ,   ,       。
		
		B.            ,       Resource   。
		  : FileSystemResource
		   :ClassPathResource
		URL  :URLResource
		Byte  :ByteArrayResource
		
		C.   java           ,      ,         。
		     ,            。
	
	1.2         XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this);
		
		     reader.loadBeanDefinitions(resource)         
		
		    :
			A.           DocumentLoader documentLoader = new DefaultDocumentLoader();
			
			B.   documentLoader       :
				
				public Document loadDocument(InputSource inputSource, EntityResolver entityResolver, ErrorHandler errorHandler, int validationMode, boolean namespaceAware)
				      :
					inputSource:    
					entityResolver:     ,   new ResourceEntityResolver(resourceLoader);
					validationMode:      ,   xsd  ,  3
						                    DOCType
					namespaceAware:      ,   false
				        
				B.1      :     DocumentBuilderFactory
					DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
					factory.setValidating(true);
					factory.setNamespaceAware(true);
					factory.setAttribute(SCHEMA_LANGUAGE_ATTRIBUTE, XSD_SCHEMA_LANGUAGE);
				B.2      :     DocumentBuilder
					DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
					DocumentBuilder docBuilder = factory.newDocumentBuilder();    :DocumentBuilderImpl
					docBuilder.setEntityResolver(entityResolver);
					docBuilder.setErrorHandler(errorHandler);
				B.3      :       Document
					builder.parse(inputSource);
					
					    :    DOMParser    
						domParser.parse(is);
						Document doc = domParser.getDocument();
						domParser.dropDocumentReferences();
						    XIncludeAwareParserConfiguration  
						
						  doc    :DeferredDocumentImpl,        :
						
						The Document interface represents the entire HTML or XML document
						Conceptually, it is the root of the document tree, and provides the
						primary access to the document's data.
				
2.      BeanDefinitions 
	return registerBeanDefinitions(doc, resource);
	
	2.1     
		  :BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
		
		BeanDefinitionDocumentReader    :    BeanDefinition xml  
		       :DefaultBeanDefinitionDocumentReader
		documentReader       :
			private XmlReaderContext readerContext;  XmlBeanDefinitionReader  NamespaceHandlerResolver    
							
			private BeanDefinitionParserDelegate delegate;       xml BeanDefinition  
		
	2.2    :
		documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
		
		   createReaderContext(resource)    :XmlReaderContext
		
		    : Element root = doc.getDocumentElement();          
		    CoreDocumentImpl  
			
		doRegisterBeanDefinitions(root); //     
		
		   parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate)
		
		       :
		A.     :
			import  :
			alias  :
			bean  :     bean     
			beans  :    
		B.      :
	
	2.3 bean     
		A. BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
			       :
				private final BeanDefinition beanDefinition;
				private final String beanName;
				private final String[] aliases;
				
			  delegate     ,  BeanDefinitionHolder  
			                   ,  :claas,id,name 
			     :
				AbstractBeanDefinition bd = createBeanDefinition(className, parent);
				bd.setDescription
				parseBeanDefinitionAttributes
				parseMetaElements
				parseLookupOverrideSubElements
				parseReplacedMethodSubElements
				parseConstructorArgElements
				parsePropertyElements
				parseQualifierElements
				bd.setResource(this.readerContext.getResource());
				bd.setSource(extractSource(ele));
			
			  :           beanName,             beanName
			
			           :BeanDefinition
			BeanDefinition      , spring       ,RootBeanDefinition,ChildBeanDefinition  GenericBeanDefinition
			BeanDefinition             class,scope,lazy-init     ,BeanDefinition    beanClass,scope,lazyInit   
			BeanDefinition                 。
			  RootBeanDefinition        
			
			spring  RootBeanDefinition                         
			    BeanDefinition       BeanDefinitionRegistry 。
			spring   BeanDefinitionRegistry    spring      ,    map     
			        BeanDefinitionRegistry        。
			
		B. bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
			  ,
		C. BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
			  :
			C.1   beanDefinition
				registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());
				
					this.beanDefinitionMap.put(beanName, beanDefinition);
					this.beanDefinitionNames.add(beanName);
			C.2     
				registry.registerAlias(beanName, alias);
					this.aliasMap.put(alias, name);

2. Object bean = factory. getBean ("myBean");1. beanName 을 변환 합 니 다. 들 어 오 는 인자 가 별명 일 수도 있 고 Factory Bean 일 수도 있 기 때문에 1.1 을 해석 해 야 합 니 다. beanName 이 라면 1.2 를 처리 할 필요 가 없습니다. "& beanName" 이 라면 & 1.3 을 삭제 하고 별명 이 라면 beanName 으로 변환 합 니 다.
2.          
	        ,       ,     singtonFactories   。
	       bean             ,                 , spring   bean      
	bean      bean ObjectFactory          ,     bean           bean     ObjectFactory
	
	2.1  singletonObjects  map    ,       
	2.2       , singletonFactories   map     ObjectFactory  
	2.3   2.2      ObjectFactory      :singletonObject = singletonFactory.getObject();
	2.4            :this.earlySingletonObjects.put(beanName, singletonObject);
	2.5   2.2      ObjectFactory        ,this.singletonFactories.remove(beanName);
	
	
3. bean     :bean = getObjectForBeanInstance(sharedInstance, name, beanName, null);
	  2    bean,      bean,    FactoryBean,
	   FactoryBean,    getObject()     
	
	       ,object = postProcessObjectFromFactoryBean(object, beanName);
	          
	

4.          ,   
5.   parentBeanFactory
	        ,    parentBeanFactory,        bean,       
	      ,  parentBeanFactory   ,       bean
	
6.  xml     GenericBeanDefinition    RootBeanDefinition
	final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName);		
7.     
	         :registerDependentBean(dep, beanName);
	
8.      scope  bean   
	    :
		sharedInstance = getSingleton(beanName, () -> {
			try {
				return createBean(beanName, mbd, args);
			}
			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.
				destroySingleton(beanName);
				throw ex;
			}
		});
		bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd);
		
	    :  
	  scope  :  
9.     

좋은 웹페이지 즐겨찾기