【Spring】- IOC容器初始化過程
IOC容器案例:XmlBeanFactory
XmlBeanFactory容器初始化過程:
XmlBeanFactory beanFactory = new XmlBeanFactory(new ClassPathResource("com/zhiwei/ioc/applicationContext.xml"));
步驟1:XmlBeanFactory 使用 ClassPathResource 對映Spring XML配置檔案
new ClassPathResource("com/zhiwei/ioc/applicationContext.xml")
步驟2:初始化空白的IOC容器,然後使用XmlBeanDefinitionReader解析Spring XML配置檔案,並注入到IOC容器
public XmlBeanFactory(Resource resource, BeanFactory parentBeanFactory) throws BeansException { //傳遞父BeanFactory初始化父類 super(parentBeanFactory); //BeanDefinition解析 this.reader.loadBeanDefinitions(resource); }
步驟3: 檢視XmlBeanDefinitionReader.loadBeanDefinitions() 方法
public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException { Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get(); if (currentResources == null) { currentResources = new HashSet<EncodedResource>(4); this.resourcesCurrentlyBeingLoaded.set(currentResources); } if (!currentResources.add(encodedResource)) { throw new BeanDefinitionStoreException( "Detected cyclic loading of " + encodedResource + " - check your import definitions!"); } try { InputStream inputStream = encodedResource.getResource().getInputStream(); try { InputSource inputSource = new InputSource(inputStream); if (encodedResource.getEncoding() != null) { inputSource.setEncoding(encodedResource.getEncoding()); } return doLoadBeanDefinitions(inputSource, encodedResource.getResource()); } finally { inputStream.close(); } } catch (IOException ex) { throw new BeanDefinitionStoreException( "IOException parsing XML document from " + encodedResource.getResource(), ex); } finally { currentResources.remove(encodedResource); if (currentResources.isEmpty()) { this.resourcesCurrentlyBeingLoaded.remove(); } } }
方法解讀: resourcesCurrentlyBeingLoaded:通過ThreadLocal繫結EncodedResource,XmlBeanDefinitionReader就持有配置資源對映,方便後續DOM XML解析,將配置資源封裝為 inputSource物件,然後傳遞給doLoadBeanDefinitions方法
步驟4:檢視XmlBeanDefinitionReader.doLoadBeanDefinitions方法 原始碼:
protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)
throws BeanDefinitionStoreException {
Document doc = doLoadDocument(inputSource, resource);
return registerBeanDefinitions(doc, resource);
}
解析: doLoadDocument方法底層通過DOM解析的方法解析Spring XML配置檔案,並返回Document物件,接著呼叫registerBeanDefinitions進行Bean實際註冊工作
步驟5:檢視XmlBeanDefinitionReader.registerBeanDefinitions方法實現
public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {
BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();
int countBefore = getRegistry().getBeanDefinitionCount();
documentReader.registerBeanDefinitions(doc, createReaderContext(resource));
return getRegistry().getBeanDefinitionCount() - countBefore;
}
解析: getRegistry():可用於獲取IOC容器,原因XmlBeanFactory例項化: private final XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(this); 將XML的Bean定義閱讀器與IOC容器繫結在一起 registerBeanDefinitions內部呼叫registerBeanDefinitions註冊Bean,注意createReaderContext方法會建立XmlReaderContext,該物件會與XmlBeanDefinitionReader 繫結,從而實現與IOC容器的間接繫結,這一點對於後續的Bean實際IOC容器註冊非常重要
步驟6:檢視BeanDefinitionDocumentReader.registerBeanDefinitions方法
注意:BeanDefinitionDocumentReader主要作用就是將Spring XMl配置檔案元素對映為IOC容器中的BeanDefinition資料結構,實現真正意義上的解析、註冊Bean的功能
@Override
public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {
this.readerContext = readerContext;
logger.debug("Loading bean definitions");
Element root = doc.getDocumentElement();
doRegisterBeanDefinitions(root);
}
解析:將readerContext作為BeanDefinitionDocumentReader的內建元件,然後獲取XML檔案的Root節點,接著呼叫doRegisterBeanDefinitions方法處理
步驟7: BeanDefinitionDocumentReader.doRegisterBeanDefinitions方法程式碼
protected void doRegisterBeanDefinitions(Element root) {
BeanDefinitionParserDelegate parent = this.delegate;
this.delegate = createDelegate(getReaderContext(), root, parent);
if (this.delegate.isDefaultNamespace(root)) {
String profileSpec = root.getAttribute(PROFILE_ATTRIBUTE);
if (StringUtils.hasText(profileSpec)) {
String[] specifiedProfiles = StringUtils.tokenizeToStringArray(
profileSpec, BeanDefinitionParserDelegate.MULTI_VALUE_ATTRIBUTE_DELIMITERS);
if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {
if (logger.isInfoEnabled()) {
logger.info("Skipped XML bean definition file due to specified profiles [" + profileSpec +
"] not matching: " + getReaderContext().getResource());
}
return;
}
}
}
preProcessXml(root);
parseBeanDefinitions(root, this.delegate);
postProcessXml(root);
this.delegate = parent;
}
解析:實際解析XMl檔案的時BeanDefinitionParserDelegate物件,檢視createDelegate方法:
protected BeanDefinitionParserDelegate createDelegate(
XmlReaderContext readerContext, Element root, BeanDefinitionParserDelegate parentDelegate) {
BeanDefinitionParserDelegate delegate = new BeanDefinitionParserDelegate(readerContext);
delegate.initDefaults(root, parentDelegate);
return delegate;
}
該方法執行時不僅僅返回BeanDefinitionParserDelegate 物件,還會在建立後進行Beans標籤的屬性資訊的初始化工作,該方法核心程式碼:
preProcessXml(root); //自定義標籤前置處理
parseBeanDefinitions(root, this.delegate); //Spring原始標籤處理
postProcessXml(root); //自定義標籤後置處理
步驟8:檢視BeanDefinitionDocumentReader.parseBeanDefinitions方法程式碼
protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {
if (delegate.isDefaultNamespace(root)) {
NodeList nl = root.getChildNodes();
for (int i = 0; i < nl.getLength(); i++) {
Node node = nl.item(i);
if (node instanceof Element) {
Element ele = (Element) node;
if (delegate.isDefaultNamespace(ele)) {
parseDefaultElement(ele, delegate);
}
else {
delegate.parseCustomElement(ele);
}
}
}
}
else {
delegate.parseCustomElement(root);
}
}
解析:該方法遍歷XML檔案對映的DOM樹的節點,delegate.isDefaultNamespace(root)主要用於XML檔案名稱空間校驗,delegate.isDefaultNamespace(ele)用於區別配置檔案中的元素是否時Spring自帶的,若是Spring自身元素則parseDefaultElement方法處理,否則parseCustomElement處理(例如自定義標籤)
步驟9:檢視BeanDefinitionDocumentReader.parseDefaultElement方法程式碼
private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {
if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {
importBeanDefinitionResource(ele);
}
else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {
processAliasRegistration(ele);
}
else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {
processBeanDefinition(ele, delegate);
}
else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {
// recurse
doRegisterBeanDefinitions(ele);
}
}
解析:該方法作為一箇中轉介面,根據標籤元素的不同選擇相應的方法處理,這裡主要探索Bean標籤的處理方法processBeanDefinition(ele, delegate);
步驟10:檢視BeanDefinitionDocumentReader.processBeanDefinition方法程式碼:
protected void processBeanDefinition(Element ele, BeanDefinitionParserDelegate delegate) {
BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
if (bdHolder != null) {
bdHolder = delegate.decorateBeanDefinitionIfRequired(ele, bdHolder);
try {
// Register the final decorated instance.
BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry());
}
catch (BeanDefinitionStoreException ex) {
getReaderContext().error("Failed to register bean definition with name '" +
bdHolder.getBeanName() + "'", ele, ex);
}
// Send registration event.
getReaderContext().fireComponentRegistered(new BeanComponentDefinition(bdHolder));
}
}
解析:
BeanDefinitionHolder bdHolder = delegate.parseBeanDefinitionElement(ele);
主要時根據Root元素節點遍歷,然後將資料封裝到 BeanDefinitionHolder 這個資料結構,真正IOC容器的Bean註冊在 BeanDefinitionReaderUtils.registerBeanDefinition(bdHolder, getReaderContext().getRegistry()); 方法
步驟11:檢視BeanDefinitionReaderUtils.registerBeanDefinition方法程式碼
public static void registerBeanDefinition(
BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
throws BeanDefinitionStoreException {
// Register bean definition under primary name.
String beanName = definitionHolder.getBeanName();
registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());
// Register aliases for bean name, if any.
String[] aliases = definitionHolder.getAliases();
if (aliases != null) {
for (String alias : aliases) {
registry.registerAlias(beanName, alias);
}
}
}
解析:這裡Bean註冊分為Bean註冊和Bean別名註冊,主要看registry.registerBeanDefinition方法
步驟12:檢視BeanDefinitionRegistry.registerBeanDefinition(這裡使用DefaultListableBeanFactory基礎IOC容器)
@Override
public void registerBeanDefinition(String beanName, BeanDefinition beanDefinition)
throws BeanDefinitionStoreException {
Assert.hasText(beanName, "Bean name must not be empty");
Assert.notNull(beanDefinition, "BeanDefinition must not be null");
if (beanDefinition instanceof AbstractBeanDefinition) {
try {
((AbstractBeanDefinition) beanDefinition).validate();
}
catch (BeanDefinitionValidationException ex) {
throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
"Validation of bean definition failed", ex);
}
}
BeanDefinition oldBeanDefinition;
oldBeanDefinition = this.beanDefinitionMap.get(beanName);
if (oldBeanDefinition != null) {
if (!isAllowBeanDefinitionOverriding()) {
throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,
"Cannot register bean definition [" + beanDefinition + "] for bean '" + beanName +
"': There is already [" + oldBeanDefinition + "] bound.");
}
else if (oldBeanDefinition.getRole() < beanDefinition.getRole()) {
// e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE
if (this.logger.isWarnEnabled()) {
this.logger.warn("Overriding user-defined bean definition for bean '" + beanName +
"' with a framework-generated bean definition: replacing [" +
oldBeanDefinition + "] with [" + beanDefinition + "]");
}
}
else if (!beanDefinition.equals(oldBeanDefinition)) {
if (this.logger.isInfoEnabled()) {
this.logger.info("Overriding bean definition for bean '" + beanName +
"' with a different definition: replacing [" + oldBeanDefinition +
"] with [" + beanDefinition + "]");
}
}
else {
if (this.logger.isDebugEnabled()) {
this.logger.debug("Overriding bean definition for bean '" + beanName +
"' with an equivalent definition: replacing [" + oldBeanDefinition +
"] with [" + beanDefinition + "]");
}
}
this.beanDefinitionMap.put(beanName, beanDefinition);
}
else {
if (hasBeanCreationStarted()) {
// Cannot modify startup-time collection elements anymore (for stable iteration)
synchronized (this.beanDefinitionMap) {
this.beanDefinitionMap.put(beanName, beanDefinition);
List<String> updatedDefinitions = new ArrayList<String>(this.beanDefinitionNames.size() + 1);
updatedDefinitions.addAll(this.beanDefinitionNames);
updatedDefinitions.add(beanName);
this.beanDefinitionNames = updatedDefinitions;
if (this.manualSingletonNames.contains(beanName)) {
Set<String> updatedSingletons = new LinkedHashSet<String>(this.manualSingletonNames);
updatedSingletons.remove(beanName);
this.manualSingletonNames = updatedSingletons;
}
}
}
else {
// Still in startup registration phase
this.beanDefinitionMap.put(beanName, beanDefinition);
this.beanDefinitionNames.add(beanName);
this.manualSingletonNames.remove(beanName);
}
this.frozenBeanDefinitionNames = null;
}
if (oldBeanDefinition != null || containsSingleton(beanName)) {
resetBeanDefinition(beanName);
}
}
解析: 首先校驗生成的BeanDefinition型別,接著校驗新建立的Bean是否在IOC容器中註冊過,如果註冊過則按照Bean的相應配置策略進行處理,例如重寫,接著根據hasBeanCreationStarted方法判斷IOC容器是否啟動過,如果未啟動則beanDefinitionMap Map記錄BeanName與BeanDefinition物件的關係,beanDefinitionNames記錄註冊的Bean名稱,這裡Map的key為BeanName這就時IOC容器中BeanName唯一的原因,如果IOC容器實現啟動則beanDefinitionMap直接存放,manualSingletonNames需要已有的BeanName進行校驗,最終等於beanDefinitionNames資料,至此整個XMl解析與IOC容器的初始化