BeanDe" />

日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不

當前位置:首頁 > 科技  > 軟件

徹底搞懂Spring的Bean加載

來源: 責編: 時間:2023-09-28 10:04:42 345觀看
導讀一、Bean 加載原理加載過程: 通過 ResourceLoader和其子類DefaultResourceLoader完成資源文件位置定位,實現從類路徑,文件系統,url等方式定位功能,完成定位后得到Resource對象,再交給BeanDefinitionReader,它再委托給
BeanDe

zkt28資訊網——每日最新資訊28at.com

一、Bean 加載原理

加載過程: 通過 ResourceLoader和其子類DefaultResourceLoader完成資源文件位置定位,實現從類路徑,文件系統,url等方式定位功能,完成定位后得到Resource對象,再交給BeanDefinitionReader,它再委托給
BeanDefinitionParserDelegate完成bean的解析并得到BeanDefinition對象,然后通過registerBeanDefinition方法進行注冊,IOC容器內ibu維護了一個HashMap來保存該BeanDefinition對象,Spring中的BeanDefinition其實就是我們用的JavaBean。
zkt28資訊網——每日最新資訊28at.com

什么是BeanDefinition對象

BeanDefinition是一個接口,描述了一個bean實例,它具有屬性值,構造函數參數值以及具體實現提供的更多信息。zkt28資訊網——每日最新資訊28at.com

在開始之前需要認真閱讀和理解這個過程,有了這個過程,閱讀源碼難度就小了一半。zkt28資訊網——每日最新資訊28at.com

大多源碼都進行了注釋,有的是官方英文注釋。zkt28資訊網——每日最新資訊28at.com

二、bean.xml

一個普通的bean配置文件,這里我要強調的是它里面的格式,因為解析標簽的時候會用到。它有<beans>``<bean>``<import>``<alias>等標簽,下文會對他們進行解析并翻譯成BeanDefinition對象。zkt28資訊網——每日最新資訊28at.com

<beans>  <!-- this definition could be inside one beanRefFactory.xml file -->  <bean id="a.qualified.name.of.some.sort"      class="org.springframework.context.support.ClassPathXmlApplicationContext">    <property name="configLocation" value="org/springframework/web/context/beans1.xml"/>  </bean>  <!-- while the following two could be inside another, also on the classpath,	perhaps coming from another component jar -->  <bean id="another.qualified.name"      class="org.springframework.context.support.ClassPathXmlApplicationContext">    <property name="configLocation" value="org/springframework/web/context/beans1.xml"/>    <property name="parent" ref="a.qualified.name.of.some.sort"/>  </bean>  <alias name="another.qualified.name" alias="a.qualified.name.which.is.an.alias"/></beans>

三、ResourceLoader.java

加載資源的策略接口(策略模式)。
DefaultResourceLoader is a standalone implementation that is usable outside an ApplicationContext, also used by ResourceEditorzkt28資訊網——每日最新資訊28at.com

An ApplicationContext is required to provide this functionality, plus extended ResourcePatternResolver support.zkt28資訊網——每日最新資訊28at.com

public interface ResourceLoader {	/** Pseudo URL prefix for loading from the class path: "classpath:". */	String CLASSPATH_URL_PREFIX = ResourceUtils.CLASSPATH_URL_PREFIX;               // 返回一個Resource 對象 (明確配置文件位置的對象)	Resource getResource(String location);        // 返回ResourceLoader的ClassLoader	@Nullable	ClassLoader getClassLoader();}

然后我們看看DefaultResourceLoader對于getResource()方法的實現。zkt28資訊網——每日最新資訊28at.com

public Resource getResource(String location) {		Assert.notNull(location, "Location must not be null");		for (ProtocolResolver protocolResolver : this.protocolResolvers) {			Resource resource = protocolResolver.resolve(location, this);			if (resource != null) {				return resource;			}		}               // 如果location 以 / 開頭		if (location.startsWith("/")) {			return getResourceByPath(location);		}                // 如果location 以classpath: 開頭		else if (location.startsWith(CLASSPATH_URL_PREFIX)) {			return new ClassPathResource(location.substring(CLASSPATH_URL_PREFIX.length()), getClassLoader());		}		else {			try {				// Try to parse the location as a URL...				URL url = new URL(location);				return (ResourceUtils.isFileURL(url) ? new FileUrlResource(url) : new UrlResource(url));			}			catch (MalformedURLException ex) {				// No URL -> resolve as resource path.				return getResourceByPath(location);			}		}	}

可以看到,它判斷了三種情況:/ classpath: url格式匹配, 然后調用相對應的處理方法,我只分析classpath:,因為這是最常用的。所以看一看ClassPathResource實現:zkt28資訊網——每日最新資訊28at.com

public ClassPathResource(String path, @Nullable ClassLoader classLoader) {		Assert.notNull(path, "Path must not be null");		String pathToUse = StringUtils.cleanPath(path);		if (pathToUse.startsWith("/")) {			pathToUse = pathToUse.substring(1);		}		this.path = pathToUse;		this.classLoader = (classLoader != null ? classLoader : ClassUtils.getDefaultClassLoader());	}

看了上面的代碼,意味著你配置靜態資源文件路徑的時候,不用糾結classpath:后面用不用寫/,因為如果寫了它會給你過濾掉。zkt28資訊網——每日最新資訊28at.com

那url如何定位的呢?zkt28資訊網——每日最新資訊28at.com

跟蹤getResourceByPath(location)方法:zkt28資訊網——每日最新資訊28at.com

@Override	protected Resource getResourceByPath(String path) {		if (path.startsWith("/")) {			path = path.substring(1);		}		// 這里使用文件系統資源對象來定義bean文件		return new FileSystemResource(path);	}

好了,很明顯…跑偏了,因為我們想要的是xml文件及路徑的解析,不過還好,換湯不換藥。下文中會涉及到。zkt28資訊網——每日最新資訊28at.com

觸發bean加載

回到正題,我們在使用spring手動加載bean.xml的時候,用到:zkt28資訊網——每日最新資訊28at.com

ApplicationContext applicationContext = new ClassPathXmlApplicationContext("bean.xml");

那就從ClassPathXmlApplicationContext開始:zkt28資訊網——每日最新資訊28at.com

四、ClassPathXmlApplicationContext.java

這個類里面只有構造方法(多個)和一個getConfigResources()方法,構造方法最終都統一打到下面這個構造方法中(Spring源碼經常這樣,適配器模式):zkt28資訊網——每日最新資訊28at.com

public ClassPathXmlApplicationContext(			String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)			throws BeansException {	// 動態的確定用哪個加載器去加載 配置文件		1.super(parent);	// 告訴讀取器 配置文件在哪里, 定位加載配置文件		2.setConfigLocations(configLocations);	// 刷新		if (refresh) {			// 在創建IOC容器前,如果容器已經存在,則需要把已有的容器摧毀和關閉,以保證refresh			//之后使用的是新的IOC容器			3.refresh();		}	}

注意: 這個類非常關鍵,我認為它定義了一個xml加載bean的一個Life Cycle:zkt28資訊網——每日最新資訊28at.com

  • super() 方法完成類加載器的指定。
  • setConfigLocations(configLocations);方法對配置文件進行定位和解析,拿到Resource對象。
  • refresh();方法對標簽進行解析拿到BeanDefition對象,在通過校驗后將其注冊到IOC容器。(主要研究該方法)

我標記的1. 2. 3. 對應后面的方法x, 方便閱讀。zkt28資訊網——每日最新資訊28at.com

先深入了解下setConfigLocations(configLocations);方法:zkt28資訊網——每日最新資訊28at.com

方法2. setConfigLocations()

// 解析Bean定義資源文件的路徑,處理多個資源文件字符串數組	public void setConfigLocations(@Nullable String... locations) {		if (locations != null) {			Assert.noNullElements(locations, "Config locations must not be null");			this.configLocations = new String[locations.length];			for (int i = 0; i < locations.length; i++) {				// resolvePath 為同一個類中將字符串解析為路徑的方法				this.configLocations[i] = resolvePath(locations[i]).trim();			}		}		else {			this.configLocations = null;		}	}

然后我們繼續上面看ClassPathXmlApplicationContext的refresh()方法:zkt28資訊網——每日最新資訊28at.com

方法3. refresh()

public void refresh() throws BeansException, IllegalStateException {		synchronized (this.startupShutdownMonitor) {			// 為refresh 準備上下文			prepareRefresh();			// 通知子類去刷新 Bean工廠			ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();			// 用該 上下文來 準備bean工廠			prepareBeanFactory(beanFactory);			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) {				if (logger.isWarnEnabled()) {					logger.warn("Exception encountered during context initialization - " +							"cancelling refresh attempt: " + ex);				}				// Destroy already created singletons to avoid dangling resources.				destroyBeans();				// Reset 'active' flag.				cancelRefresh(ex);				// Propagate exception to caller.				throw ex;			}			finally {				// Reset common introspection caches in Spring's core, since we				// might not ever need metadata for singleton beans anymore...				resetCommonCaches();			}		}	}

**注:**下面的方法全都是圍繞refresh()里深入閱讀,該方法套的很深,下面的閱讀可能會引起不適。zkt28資訊網——每日最新資訊28at.com

然后看看refresh()方法中的obtainFreshBeanFactory()方法。zkt28資訊網——每日最新資訊28at.com

方法3.1 obtainFreshBeanFactory()

// 調用--刷新bean工廠	protected ConfigurableListableBeanFactory obtainFreshBeanFactory() {		// 委派模式:父類定義了refreshBeanFactory方法,具體實現調用子類容器		refreshBeanFactory();		return getBeanFactory();	}

然后看obtainFreshBeanFactory()的 refreshBeanFactory()方法。zkt28資訊網——每日最新資訊28at.com

方法3.1.1 refreshBeanFactory()

// 刷新bean工廠	protected final void refreshBeanFactory() throws BeansException {		// 如果存在容器,就先銷毀并關閉		if (hasBeanFactory()) {			destroyBeans();			closeBeanFactory();		}		try {			// 創建IOC容器			DefaultListableBeanFactory beanFactory = createBeanFactory();			beanFactory.setSerializationId(getId());			// 對容器進行初始化			customizeBeanFactory(beanFactory);			// 調用載入Bean定義的方法,(使用了委派模式)			loadBeanDefinitions(beanFactory);			synchronized (this.beanFactoryMonitor) {				this.beanFactory = beanFactory;			}		}		catch (IOException ex) {			throw new ApplicationContextException("I/O error parsing bean definition source for " + getDisplayName(), ex);		}	}

然后再跟進refreshBeanFactory() 的loadBeanDefinitions()方法。zkt28資訊網——每日最新資訊28at.com

方法3.1.1.1 loadBeanDefinitions()

通過 XmlBeanDefinitionReader 加載 BeanDefinition。zkt28資訊網——每日最新資訊28at.com

// 通過 XmlBeanDefinitionReader 加載 BeanDefinition	@Override	protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) throws BeansException, IOException {		// Create a new XmlBeanDefinitionReader for the given BeanFactory.		// 為beanFactory 創建一個新的 XmlBeanDefinitionReader		XmlBeanDefinitionReader beanDefinitionReader = new XmlBeanDefinitionReader(beanFactory);		// Configure the bean definition reader with this context's		// resource loading environment.		beanDefinitionReader.setEnvironment(this.getEnvironment());		// 為 Bean讀取器設置Spring資源加載器 (因為祖父類是ResourceLoader的子類,所以也是ResourceLoader)		beanDefinitionReader.setResourceLoader(this);		//  為 Bean讀取器設置SAX xml解析器DOM4J		beanDefinitionReader.setEntityResolver(new ResourceEntityResolver(this));		// Allow a subclass to provide custom initialization of the reader,		// then proceed with actually loading the bean definitions.		// 初始化 BeanDefinition讀取器		initBeanDefinitionReader(beanDefinitionReader);		// 真正加載 bean定義		loadBeanDefinitions(beanDefinitionReader);	}

再跟進loadBeanDefinitions(DefaultListableBeanFactory beanFactory)方法中的loadBeanDefinitions(XmlBeanDefinitionReader reader)方法。zkt28資訊網——每日最新資訊28at.com

方法3.1.1.1.1 loadBeanDefinitions()

XMLBean讀取器加載BeanDefinition 資源。zkt28資訊網——每日最新資訊28at.com

// XMLBean讀取器加載Bean 定義資源	protected void loadBeanDefinitions(XmlBeanDefinitionReader reader) throws BeansException, IOException {		// 獲取Bean定義資源的定位		Resource[] configResources = getConfigResources();		if (configResources != null) {			// XMLBean讀取器調用其父類 AbstractBeanDefinitionReader 讀取定位的Bean定義資源			reader.loadBeanDefinitions(configResources);		}		// 如果子類中獲取的bean定義資源定位為空,		// 則獲取 FileSystemXmlApplicationContext構造方法中 setConfigLocations 方法設置的資源		String[] configLocations = getConfigLocations();		if (configLocations != null) {			// XMLBean讀取器調用其父類 AbstractBeanDefinitionReader 讀取定位的Bean定義資源			reader.loadBeanDefinitions(configLocations);		}	}
@Override	public int loadBeanDefinitions(Resource... resources) throws BeanDefinitionStoreException {		Assert.notNull(resources, "Resource array must not be null");		int count = 0;		//		for (Resource resource : resources) {			count += loadBeanDefinitions(resource);		}		return count;	}

再跟下去loadBeanDefinitions(): 這只是一個抽象方法,找到XmlBeanDefinitionReader子類的實現:zkt28資訊網——每日最新資訊28at.com

@Override	public int loadBeanDefinitions(Resource resource) throws BeanDefinitionStoreException {		return loadBeanDefinitions(new EncodedResource(resource));	}

再深入loadBeanDefinitions:zkt28資訊網——每日最新資訊28at.com

通過明確的xml文件加載beanzkt28資訊網——每日最新資訊28at.com

// 通過明確的xml文件加載bean	public int loadBeanDefinitions(EncodedResource encodedResource) throws BeanDefinitionStoreException {		Assert.notNull(encodedResource, "EncodedResource must not be null");		if (logger.isTraceEnabled()) {			logger.trace("Loading XML bean definitions from " + encodedResource);		}		Set<EncodedResource> currentResources = this.resourcesCurrentlyBeingLoaded.get();		if (currentResources == null) {			currentResources = new HashSet<>(4);			this.resourcesCurrentlyBeingLoaded.set(currentResources);		}		if (!currentResources.add(encodedResource)) {			throw new BeanDefinitionStoreException(					"Detected cyclic loading of " + encodedResource + " - check your import definitions!");		}		try {			// 將資源文件轉為InputStream的IO流			InputStream inputStream = encodedResource.getResource().getInputStream();			try {				// 從流中獲取 xml解析資源				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();			}		}	}

再深入到doLoadBeanDefinitions():zkt28資訊網——每日最新資訊28at.com

真正開始加載 BeanDefinitions。zkt28資訊網——每日最新資訊28at.com

protected int doLoadBeanDefinitions(InputSource inputSource, Resource resource)			throws BeanDefinitionStoreException {		try {			// 將xml 文件轉換為DOM對象			Document doc = doLoadDocument(inputSource, resource);			// 對bean定義解析的過程,該過程會用到 Spring的bean配置規則			int count = registerBeanDefinitions(doc, resource);			if (logger.isDebugEnabled()) {				logger.debug("Loaded " + count + " bean definitions from " + resource);			}			return count;		}        ...  ...  ..}

doLoadDocument()方法將流進行解析,返回一個Document對象:return builder.parse(inputSource);為了避免擾亂思路,這里的深入自己去完成。zkt28資訊網——每日最新資訊28at.com

還需要再深入到:registerBeanDefinitions()。zkt28資訊網——每日最新資訊28at.com

注冊 BeanDefinitions。zkt28資訊網——每日最新資訊28at.com

public int registerBeanDefinitions(Document doc, Resource resource) throws BeanDefinitionStoreException {		BeanDefinitionDocumentReader documentReader = createBeanDefinitionDocumentReader();		// 得到容器中注冊的bean數量		int countBefore = getRegistry().getBeanDefinitionCount();		// 解析過程入口,這里使用了委派模式		documentReader.registerBeanDefinitions(doc, createReaderContext(resource));		// 統計解析的bean數量		return getRegistry().getBeanDefinitionCount() - countBefore;	}

再深入registerBeanDefinitions()方法(該方法是委派模式的結果):zkt28資訊網——每日最新資訊28at.com

@Override	public void registerBeanDefinitions(Document doc, XmlReaderContext readerContext) {		// 獲得XML描述符		this.readerContext = readerContext;		doRegisterBeanDefinitions(doc.getDocumentElement());	}

再深入doRegisterBeanDefinitions(doc.getDocumentElement());:zkt28資訊網——每日最新資訊28at.com

真正開始注冊 BeanDefinitions :zkt28資訊網——每日最新資訊28at.com

protected void doRegisterBeanDefinitions(Element root) {		// Any nested <beans> elements will cause recursion in this method. In		// order to propagate and preserve <beans> default-* attributes correctly,		// keep track of the current (parent) delegate, which may be null. Create		// the new (child) delegate with a reference to the parent for fallback purposes,		// then ultimately reset this.delegate back to its original (parent) reference.		// this behavior emulates a stack of delegates without actually necessitating one.		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);				// We cannot use Profiles.of(...) since profile expressions are not supported				// in XML config. See SPR-12458 for details.				if (!getReaderContext().getEnvironment().acceptsProfiles(specifiedProfiles)) {					if (logger.isDebugEnabled()) {						logger.debug("Skipped XML bean definition file due to specified profiles [" + profileSpec +								"] not matching: " + getReaderContext().getResource());					}					return;				}			}		}		// 在bean解析定義之前,進行自定義解析,看是否是用戶自定義標簽		preProcessXml(root);		// 開始進行解析bean定義的document對象		parseBeanDefinitions(root, this.delegate);		// 解析bean定義之后,進行自定義的解析,增加解析過程的可擴展性		postProcessXml(root);		this.delegate = parent;	}

接下來看parseBeanDefinitions(root, this.delegate)。zkt28資訊網——每日最新資訊28at.com

document的根元素開始進行解析翻譯成BeanDefinitions。zkt28資訊網——每日最新資訊28at.com

// 從document的根元素開始進行解析翻譯成BeanDefinitions	protected void parseBeanDefinitions(Element root, BeanDefinitionParserDelegate delegate) {		// bean定義的document對象使用了spring默認的xml命名空間		if (delegate.isDefaultNamespace(root)) {			// 獲取bean定義的document對象根元素的所有字節點			NodeList nl = root.getChildNodes();			for (int i = 0; i < nl.getLength(); i++) {				Node node = nl.item(i);				// 獲得document節點是xml元素節點				if (node instanceof Element) {					Element ele = (Element) node;					// bean定義的document的元素節點使用的是spring默認的xml命名空間					if (delegate.isDefaultNamespace(ele)) {						// 使用spring的bean規則解析元素 節點						parseDefaultElement(ele, delegate);					}					else {						// 沒有使用spring默認的xml命名空間,則使用用戶自定義的解析規則解析元素節點						delegate.parseCustomElement(ele);					}				}			}		}		else {			delegate.parseCustomElement(root);		}	}	private void parseDefaultElement(Element ele, BeanDefinitionParserDelegate delegate) {		// 解析 <import> 標簽元素,并進行導入解析		if (delegate.nodeNameEquals(ele, IMPORT_ELEMENT)) {			importBeanDefinitionResource(ele);		}		// alias		else if (delegate.nodeNameEquals(ele, ALIAS_ELEMENT)) {			processAliasRegistration(ele);		}		// bean		else if (delegate.nodeNameEquals(ele, BEAN_ELEMENT)) {			processBeanDefinition(ele, delegate);		}		// beans		else if (delegate.nodeNameEquals(ele, NESTED_BEANS_ELEMENT)) {			// recurse			doRegisterBeanDefinitions(ele);		}	}

importBeanDefinitionResource(ele);``processAliasRegistration(ele);``processBeanDefinition(ele, delegate);這三個方法里分別展示了標簽解析的詳細過程。
這下看到了,它其實使用DOM4J來解析import bean alias等標簽,然后遞歸標簽內部直到拿到所有屬性并封裝到BeanDefition對象中。比如說processBeanDefinition方法。zkt28資訊網——每日最新資訊28at.com

給我一個element 解析成 BeanDefinition。zkt28資訊網——每日最新資訊28at.com

// 給我一個element 解析成 BeanDefinition	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.				// 注冊: 將db注冊到ioc,委托模式				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));		}	}

繼續深入registerBeanDefinition()。zkt28資訊網——每日最新資訊28at.com

注冊BeanDefinitions 到 bean 工廠。zkt28資訊網——每日最新資訊28at.com

// 注冊BeanDefinitions 到 bean 工廠	// definitionHolder : bean定義,包含了 name和aliases	// registry: 注冊到的bean工廠	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);			}		}	}

再深入registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition())。zkt28資訊網——每日最新資訊28at.com

注冊BeanDefinitions 到IOC容器。zkt28資訊網——每日最新資訊28at.com

注意:該方法所在類是接口,我們查看的是DefaultListableBeanFactory.java所實現的該方法。zkt28資訊網——每日最新資訊28at.com

// 實現BeanDefinitionRegistry接口,注冊BeanDefinitions 	@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");		// 校驗是否是 AbstractBeanDefinition)		if (beanDefinition instanceof AbstractBeanDefinition) {			try {				// 標記 beanDefinition 生效				((AbstractBeanDefinition) beanDefinition).validate();			}			catch (BeanDefinitionValidationException ex) {				throw new BeanDefinitionStoreException(beanDefinition.getResourceDescription(), beanName,						"Validation of bean definition failed", ex);			}		}		// 判斷beanDefinitionMap 里是否已經有這個bean		BeanDefinition existingDefinition = this.beanDefinitionMap.get(beanName);		//如果沒有這個bean		if (existingDefinition != null) {			//如果不允許bd 覆蓋已注冊的bean, 就拋出異常			if (!isAllowBeanDefinitionOverriding()) {				throw new BeanDefinitionOverrideException(beanName, beanDefinition, existingDefinition);			}			// 如果允許覆蓋, 則同名的bean, 注冊的覆蓋先注冊的			else if (existingDefinition.getRole() < beanDefinition.getRole()) {				// e.g. was ROLE_APPLICATION, now overriding with ROLE_SUPPORT or ROLE_INFRASTRUCTURE				if (logger.isInfoEnabled()) {					logger.info("Overriding user-defined bean definition for bean '" + beanName +							"' with a framework-generated bean definition: replacing [" +							existingDefinition + "] with [" + beanDefinition + "]");				}			}			else if (!beanDefinition.equals(existingDefinition)) {				if (logger.isDebugEnabled()) {					logger.debug("Overriding bean definition for bean '" + beanName +							"' with a different definition: replacing [" + existingDefinition +							"] with [" + beanDefinition + "]");				}			}			else {				if (logger.isTraceEnabled()) {					logger.trace("Overriding bean definition for bean '" + beanName +							"' with an equivalent definition: replacing [" + existingDefinition +							"] with [" + beanDefinition + "]");				}			}			// 注冊到容器,beanDefinitionMap 就是個容器			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<>(this.beanDefinitionNames.size() + 1);					updatedDefinitions.addAll(this.beanDefinitionNames);					updatedDefinitions.add(beanName);					this.beanDefinitionNames = updatedDefinitions;					if (this.manualSingletonNames.contains(beanName)) {						Set<String> updatedSingletons = new LinkedHashSet<>(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 (existingDefinition != null || containsSingleton(beanName)) {			resetBeanDefinition(beanName);		}	}

這個方法中對所需要加載的bean進行校驗,沒有問題的話就put到beanDefinitionMap中,beanDefinitionMap其實就是IOC.這樣我們的Bean就被加載到IOC容器中了。zkt28資訊網——每日最新資訊28at.com

本文鏈接:http://m.www897cc.com/showinfo-26-11792-0.html徹底搞懂Spring的Bean加載

聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。郵件:2376512515@qq.com

上一篇: 一個關于 i++ 和 ++i 的面試題打趴了所有人

下一篇: 繼續聊聊云平臺運維規范

標簽:
  • 熱門焦點
  • K60 Pro官方停產 第三方瞬間漲價

    雖然沒有官方宣布,但Redmi的一些高管也已經透露了,Redmi K60 Pro已經停產且不會補貨,這一切都是為了即將到來的K60 Ultra鋪路,屬于廠家的正常操作。但有意思的是該機在停產之后
  • vivo TWS Air開箱體驗:真輕 臻好聽

    在vivo S15系列新機的發布會上,vivo的最新款真無線藍牙耳機vivo TWS Air也一同發布,本次就這款耳機新品給大家帶來一個簡單的分享。外包裝盒上,vivo TWS Air保持了vivo自家產
  • 19個 JavaScript 單行代碼技巧,讓你看起來像個專業人士

    今天這篇文章跟大家分享18個JS單行代碼,你只需花幾分鐘時間,即可幫助您了解一些您可能不知道的 JS 知識,如果您已經知道了,就當作復習一下,古人云,溫故而知新嘛。現在,我們就開始今
  • 使用LLM插件從命令行訪問Llama 2

    最近的一個大新聞是Meta AI推出了新的開源授權的大型語言模型Llama 2。這是一項非常重要的進展:Llama 2可免費用于研究和商業用途。(幾小時前,swyy發現它已從LLaMA 2更名為Lla
  • 騰訊蓋樓,字節拆墻

    來源 | 光子星球撰文 | 吳坤諺編輯 | 吳先之&ldquo;想重溫暴刷深淵、30+技能搭配暴搓到爽的游戲體驗嗎?一起上晶核,即刻暴打!&rdquo;曾憑借直播騰訊旗下代理格斗游戲《DNF》一
  • 攜眾多高端產品亮相ChinaJoy,小米帶來一場科技與人文的視聽盛宴

    7月28日,全球數字娛樂領域最具知名度與影響力的年度盛會中國國際數碼互動娛樂展覽會(簡稱ChinaJoy)在上海新國際博覽中心盛大開幕。作為全球領先的科
  • 2納米決戰2025

    集微網報道 從三強爭霸到四雄逐鹿,2nm的廝殺聲已然隱約傳來。無論是老牌勁旅臺積電、三星,還是誓言重回先進制程領先地位的英特爾,甚至初成立不久的新
  • 國行版三星Galaxy Z Fold5/Z Flip5發布 售價7499元起

    2023年8月3日,三星電子舉行Galaxy新品中國發布會,正式在國內推出了新一代折疊屏智能手機三星Galaxy Z Fold5與Galaxy Z Flip5,以及三星Galaxy Tab S9
  • 由于成本持續增加,筆記本產品價格預計將明顯上漲

    根據知情人士透露,由于材料、物流等成本持續增加,筆記本產品價格預計將在2021年下半年有明顯上漲。進入6月下旬以來,全球半導體芯片缺貨情況加劇,顯卡、處理器
Top 日韩成人免费在线_国产成人一二_精品国产免费人成电影在线观..._日本一区二区三区久久久久久久久不
欧美aaaaaaaa牛牛影院| 国产亚洲一本大道中文在线| 久久久免费精品| 麻豆国产va免费精品高清在线| 欧美成人免费一级人片100| 欧美精品一区在线发布| 国产精品久久91| 国产亚洲一区在线播放| 亚洲国产高清视频| 伊人春色精品| 99re8这里有精品热视频免费| 亚洲一区在线播放| 久久久亚洲一区| 欧美精品少妇一区二区三区| 国产精品久久久久久久久久久久久久 | 国产嫩草影院久久久久| 在线观看日韩国产| 99视频一区二区三区| 欧美一级片在线播放| 免费不卡欧美自拍视频| 国产精品捆绑调教| 亚洲国产精品久久久| 亚洲欧美激情四射在线日| 麻豆9191精品国产| 国产精品日韩一区二区| 亚洲精品1区2区| 欧美在线视频一区二区三区| 欧美成人免费视频| 国产农村妇女精品| 亚洲美女福利视频网站| 欧美在线免费观看| 欧美日韩直播| 亚洲第一区在线观看| 午夜精品久久一牛影视| 欧美精品成人91久久久久久久| 国产色综合天天综合网| 99国产精品视频免费观看一公开| 久久精品道一区二区三区| 欧美日韩亚洲国产精品| 亚洲电影视频在线| 欧美制服丝袜第一页| 欧美日本在线看| 亚洲国产二区| 久久久精品久久久久| 国产精品日韩欧美一区| 亚洲每日更新| 麻豆精品91| 韩国一区二区三区在线观看| 亚洲欧美日韩国产中文| 欧美日韩播放| 亚洲国产精品第一区二区三区| 欧美一区二区国产| 国产精品99免费看| 亚洲免费高清视频| 欧美成在线视频| 在线观看91精品国产入口| 欧美一区二区三区视频在线| 欧美色偷偷大香| 亚洲美女视频网| 欧美大片国产精品| 黄色另类av| 久久精品人人做人人综合| 国产美女精品| 亚洲欧美日韩精品一区二区 | 欧美大片免费观看| 在线观看国产日韩| 久久久亚洲欧洲日产国码αv | 国产香蕉97碰碰久久人人| 亚洲女性喷水在线观看一区| 欧美三级电影一区| 夜夜嗨av一区二区三区中文字幕| 欧美国产日韩免费| 亚洲黄色在线观看| 美女精品一区| 在线精品一区| 免费中文日韩| 亚洲欧洲免费视频| 欧美国产日韩精品免费观看| 亚洲人成人一区二区在线观看| 欧美成人免费网| 91久久线看在观草草青青| 欧美不卡视频一区| 亚洲黄色精品| 欧美大胆人体视频| 亚洲美女少妇无套啪啪呻吟| 欧美乱在线观看| 99国产精品| 国产精品久久毛片a| 亚洲一区免费视频| 国产精品天美传媒入口| 亚洲一区三区在线观看| 国产农村妇女毛片精品久久麻豆 | 久久精品国产精品亚洲| 国内精品久久久久久久果冻传媒 | 亚洲欧美日韩精品久久久| 国产欧美一区二区三区在线老狼| 欧美与欧洲交xxxx免费观看| 国内伊人久久久久久网站视频 | 欧美精品情趣视频| 中文在线一区| 国产精品自拍网站| 久久久久se| 亚洲黄一区二区三区| 欧美日韩爆操| 香蕉久久夜色| 激情综合网址| 欧美激情综合亚洲一二区| 一区二区三区视频免费在线观看| 国产精品wwwwww| 欧美在线一区二区| 亚洲高清视频一区| 欧美日韩在线大尺度| 午夜久久福利| **性色生活片久久毛片| 欧美日韩国产欧| 欧美一级一区| 亚洲激情视频网站| 国产精品久久九九| 久久久亚洲欧洲日产国码αv| 亚洲欧洲另类| 国产精品五区| 毛片一区二区| 亚洲一区二区三区四区五区黄| 国产亚洲精品一区二555| 欧美sm重口味系列视频在线观看| 一区二区欧美精品| 国产在线精品二区| 欧美精品999| 欧美专区日韩专区| 亚洲精品国产精品乱码不99| 国产精品久久久久aaaa九色| 久久久久久久999| 99视频精品免费观看| 国产欧美一区二区视频| 欧美第一黄网免费网站| 亚洲欧美伊人| 最新亚洲一区| 国产亚洲精品久久久| 欧美激情精品久久久| 亚洲欧美一区二区三区极速播放| 亚洲国产精品久久久| 国产精品久久久久久久久婷婷| 久久夜色精品国产| 亚洲一区在线直播| 亚洲高清自拍| 国产欧美欧美| 欧美日韩国产三区| 久久久五月天| 亚洲欧美国产另类| 亚洲精品国产视频| 国产一区二区精品久久91| 欧美日产在线观看| 久久久久九九九九| 亚洲综合色激情五月| 亚洲激情黄色| 国产一区二区三区直播精品电影| 欧美日韩国产精品一卡| 久久免费视频网| 先锋a资源在线看亚洲| 日韩手机在线导航| 在线免费观看成人网| 国产精品综合网站| 欧美日韩国产在线播放| 久久婷婷国产综合国色天香| 亚洲在线一区二区三区| 亚洲另类在线一区| 一区二区在线观看av| 国产女主播一区二区三区| 欧美三级欧美一级| 欧美激情黄色片| 麻豆国产精品777777在线| 久久国产精品99国产| 亚洲一区二区在| 日韩网站在线观看| 亚洲激情在线播放| 一区在线播放视频| 国内自拍亚洲| 国产色综合久久| 国产精品视频免费一区| 国产精品xxxxx| 欧美日韩成人综合天天影院| 欧美成在线视频| 欧美v日韩v国产v| 老鸭窝毛片一区二区三区| 久久久久久久久岛国免费| 欧美在线观看视频在线| 亚洲欧美日韩国产中文| 亚洲一区二区不卡免费| 99热这里只有精品8| 亚洲免费成人| 亚洲区一区二区三区| 亚洲国产成人精品女人久久久| 黄色国产精品| 一区二区三区自拍| 一区二区在线观看视频在线观看| 国产真实乱子伦精品视频| 国产午夜精品福利 | 一本色道久久综合亚洲精品小说| 亚洲黄网站在线观看| 亚洲国产欧美在线人成| 亚洲国产精品久久久久久女王| 在线免费观看日韩欧美| 亚洲国产精品va在线看黑人 | 亚洲国产一区二区精品专区|