最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SpringBoot排除自動(dòng)加載數(shù)據(jù)源方式

 更新時(shí)間:2024年05月29日 09:29:55   作者:fenglllle  
這篇文章主要介紹了SpringBoot排除自動(dòng)加載數(shù)據(jù)源方式,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

前言

有些老項(xiàng)目使用Spring MVC里面有寫好的數(shù)據(jù)庫連接池,比如redis/mongodb/mybatis(mysql其他Oracle同理)。

在這些項(xiàng)目遷入spring boot框架時(shí),會(huì)報(bào)錯(cuò)。

原因是我們業(yè)務(wù)寫好了連接池,但spring boot在jar包存在的時(shí)候會(huì)主動(dòng)加載spring boot的autoconfiguration創(chuàng)建連接池,但我們并未配置Spring Boot參數(shù),也不需要配置。

1. mongodb

mongodb自動(dòng)配置錯(cuò)誤如下:

org.mongodb.driver.cluster               : Exception in monitor thread while connecting to server localhost:27017
com.mongodb.MongoSocketOpenException: Exception opening socket
Caused by: java.net.ConnectException: Connection refused (Connection refused)

但是我沒有引入spring-boot-starter-data-mongodb的jar包,后來發(fā)現(xiàn)我引入了spring-data-mongodb的jar

檢查spring-boot-starter-data-mongodb的jar,包括3部分,如下:

我的jar包都有,相當(dāng)于這些jar拼裝成了 spring-boot-starter-data-mongodb

在Spring Boot中自動(dòng)引入了自動(dòng)配置功能

需要手動(dòng)排除自動(dòng)配置的數(shù)據(jù)源,在SpringBootApplication中exclude

@SpringBootApplication(exclude = {MongoAutoConfiguration.class, MongoDataAutoConfiguration.class})

啟動(dòng)不再報(bào)錯(cuò)連接localhost:27017,業(yè)務(wù)正常。

原理見Spring Boot官方文檔

https://docs.spring.io/spring-boot/docs/current/reference/html/using-boot-auto-configuration.html#using-boot-disabling-specific-auto-configuration

2. mybatis

mybatis同理

Failed to auto-configure a DataSource: 'spring.datasource.url' is not specified and no embedded data

***************************
APPLICATION FAILED TO START
***************************
 
Description:
 
Cannot determine embedded database driver class for database type NONE
 
Action:
 
If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).

需要排除

@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})

3. 原理講解

原理是EnableAutoConfiguration

進(jìn)一步跟蹤:

AutoConfigurationImportSelector這個(gè)類有自動(dòng)加載與排除的邏輯

public String[] selectImports(AnnotationMetadata annotationMetadata) {
		if (!isEnabled(annotationMetadata)) {
			return NO_IMPORTS;
		}
		AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
				.loadMetadata(this.beanClassLoader);
		AutoConfigurationEntry autoConfigurationEntry = getAutoConfigurationEntry(autoConfigurationMetadata,
				annotationMetadata);
		return StringUtils.toStringArray(autoConfigurationEntry.getConfigurations());
	}

注意加載代碼

getAutoConfigurationEntry(autoConfigurationMetadata, annotationMetadata);
/**
	 * Return the {@link AutoConfigurationEntry} based on the {@link AnnotationMetadata}
	 * of the importing {@link Configuration @Configuration} class.
	 * @param autoConfigurationMetadata the auto-configuration metadata
	 * @param annotationMetadata the annotation metadata of the configuration class
	 * @return the auto-configurations that should be imported
	 */
	protected AutoConfigurationEntry getAutoConfigurationEntry(AutoConfigurationMetadata autoConfigurationMetadata,
			AnnotationMetadata annotationMetadata) {
		if (!isEnabled(annotationMetadata)) {
			return EMPTY_ENTRY;
		}
		AnnotationAttributes attributes = getAttributes(annotationMetadata);
		List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes);
		configurations = removeDuplicates(configurations);
		Set<String> exclusions = getExclusions(annotationMetadata, attributes);
		checkExcludedClasses(configurations, exclusions);
		configurations.removeAll(exclusions);
		configurations = filter(configurations, autoConfigurationMetadata);
		fireAutoConfigurationImportEvents(configurations, exclusions);
		return new AutoConfigurationEntry(configurations, exclusions);
	}

里面

getExclusions(annotationMetadata, attributes);
/**
	 * Return any exclusions that limit the candidate configurations.
	 * @param metadata the source metadata
	 * @param attributes the {@link #getAttributes(AnnotationMetadata) annotation
	 * attributes}
	 * @return exclusions or an empty set
	 */
	protected Set<String> getExclusions(AnnotationMetadata metadata, AnnotationAttributes attributes) {
		Set<String> excluded = new LinkedHashSet<>();
		excluded.addAll(asList(attributes, "exclude"));
		excluded.addAll(Arrays.asList(attributes.getStringArray("excludeName")));
		excluded.addAll(getExcludeAutoConfigurationsProperty());
		return excluded;
	}

看到了,exclude或者excludeName,當(dāng)然還有一種方法

private List<String> getExcludeAutoConfigurationsProperty() {
		if (getEnvironment() instanceof ConfigurableEnvironment) {
			Binder binder = Binder.get(getEnvironment());
			return binder.bind(PROPERTY_NAME_AUTOCONFIGURE_EXCLUDE, String[].class).map(Arrays::asList)
					.orElse(Collections.emptyList());
		}
		String[] excludes = getEnvironment().getProperty(PROPERTY_NAME_AUTOCONFIGURE_EXCLUDE, String[].class);
		return (excludes != null) ? Arrays.asList(excludes) : Collections.emptyList();
	}

通過application.properties文件配置spring.autoconfigure.exclude

private static final String PROPERTY_NAME_AUTOCONFIGURE_EXCLUDE = "spring.autoconfigure.exclude";

總結(jié)

出現(xiàn)這種錯(cuò)誤多半發(fā)生在引入了spring-boot-starter-mongodb等這樣的starter插件jar,沒有配置數(shù)據(jù)源url;或者舊業(yè)務(wù)升級spring boot(筆者就是這種情況)

解決方法:

不需要的jar不要引入即可解決問題

使用exclude排除,有三種實(shí)現(xiàn)方式exclude、excludeName、spring.autoconfigure.exclude

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java基數(shù)排序radix sort原理及用法解析

    Java基數(shù)排序radix sort原理及用法解析

    這篇文章主要介紹了Java基數(shù)排序radix sort原理及用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-06-06
  • springboot整合vue實(shí)現(xiàn)上傳下載文件

    springboot整合vue實(shí)現(xiàn)上傳下載文件

    這篇文章主要為大家詳細(xì)介紹了springboot整合vue實(shí)現(xiàn)上傳下載文件,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-11-11
  • MyBatis查詢時(shí)屬性名和字段名不一致問題的解決方法

    MyBatis查詢時(shí)屬性名和字段名不一致問題的解決方法

    這篇文章主要給大家介紹了關(guān)于MyBatis查詢時(shí)屬性名和字段名不一致問題的解決方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-01-01
  • mybatis框架xml下trim中的prefix與suffix等標(biāo)簽的用法

    mybatis框架xml下trim中的prefix與suffix等標(biāo)簽的用法

    這篇文章主要介紹了mybatis框架xml下trim中的prefix與suffix等標(biāo)簽的用法,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Spring單例類加載多例屬性問題實(shí)例解析

    Spring單例類加載多例屬性問題實(shí)例解析

    本文給大家介紹Spring單例類加載多例屬性問題實(shí)例解析,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
    2026-03-03
  • Java分布式事務(wù)管理框架之Seata

    Java分布式事務(wù)管理框架之Seata

    這篇文章主要介紹了Java分布式事務(wù)框架Seata,分布式事務(wù)是指事務(wù)的參與者、支持事務(wù)的服務(wù)器、資源服務(wù)器以及事務(wù)管理器分別位于不同的分布式系統(tǒng)的不同節(jié)點(diǎn)之上
    2022-07-07
  • java ConcurrentHashMap分段加鎖提高并發(fā)效率

    java ConcurrentHashMap分段加鎖提高并發(fā)效率

    這篇文章主要為大家介紹了java ConcurrentHashMap分段加鎖提高并發(fā)效率,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-12-12
  • Java實(shí)現(xiàn)四則混合運(yùn)算代碼示例

    Java實(shí)現(xiàn)四則混合運(yùn)算代碼示例

    這篇文章主要介紹了Java實(shí)現(xiàn)四則混合運(yùn)算代碼示例,文中展示了詳細(xì)代碼,具有一定參考價(jià)值,需要的朋友可以了解下。
    2017-10-10
  • Java中的動(dòng)態(tài)綁定機(jī)制

    Java中的動(dòng)態(tài)綁定機(jī)制

    這篇文章主要介紹了Java中的動(dòng)態(tài)綁定機(jī)制,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-06-06
  • MyBatis?Plus如何實(shí)現(xiàn)獲取自動(dòng)生成主鍵值

    MyBatis?Plus如何實(shí)現(xiàn)獲取自動(dòng)生成主鍵值

    這篇文章主要介紹了MyBatis?Plus如何實(shí)現(xiàn)獲取自動(dòng)生成主鍵值問題,具有很好的參考價(jià)值,希望對大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09

最新評論

洮南市| 政和县| 昆明市| 太仓市| 吴忠市| 靖州| 于都县| 德格县| 雷波县| 广平县| 屏东县| 阿坝县| 伊金霍洛旗| 庐江县| 台中市| 昭苏县| 宁河县| 大同县| 盖州市| 古浪县| 天全县| 临猗县| 正蓝旗| 固安县| 松潘县| 天全县| 祁阳县| 吉安县| 平陆县| 东港市| 射洪县| 故城县| 嘉鱼县| 吉隆县| 日照市| 资溪县| 阿图什市| 西乌珠穆沁旗| 石嘴山市| 寿阳县| 灵寿县|