欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

前言

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

在這些項目遷入spring boot框架時,會報錯。

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

1. mongodb

mongodb自動配置錯誤如下:

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包都有,相當于這些jar拼裝成了 spring-boot-starter-data-mongodb

在Spring Boot中自動引入了自動配置功能

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

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

啟動不再報錯連接localhost:27017,業(yè)務正常。

原理見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

進一步跟蹤:

AutoConfigurationImportSelector這個類有自動加載與排除的邏輯

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,當然還有一種方法

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)這種錯誤多半發(fā)生在引入了spring-boot-starter-mongodb等這樣的starter插件jar,沒有配置數(shù)據(jù)源url;或者舊業(yè)務升級spring boot(筆者就是這種情況)

解決方法:

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

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

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

相關(guān)文章

  • Java自旋鎖及自旋的好處詳解

    Java自旋鎖及自旋的好處詳解

    這篇文章主要介紹了Java自旋鎖及自旋的好處詳解,自旋就是自己在這里不停地循環(huán),直到目標達成,而不像普通的鎖那樣,如果獲取不到鎖就進入阻塞,需要的朋友可以參考下
    2023-10-10
  • Java日期時間字符串和毫秒相互轉(zhuǎn)換的方法

    Java日期時間字符串和毫秒相互轉(zhuǎn)換的方法

    這篇文章主要為大家詳細介紹了Java日期時間字符串和毫秒相互轉(zhuǎn)換的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Springboot接收POST請求,數(shù)據(jù)為json類型問題

    Springboot接收POST請求,數(shù)據(jù)為json類型問題

    在使用Spring框架中,當處理POST請求且內(nèi)容為JSON類型時,應使用@RequestBody注解而非@RequestParam,通過@RequestBody可以將JSON數(shù)據(jù)綁定到一個Map對象中,然后通過Map的get方法來獲取需要的參數(shù)
    2022-10-10
  • springboot如何通過URL方式訪問外部資源

    springboot如何通過URL方式訪問外部資源

    這篇文章主要介紹了springboot如何通過URL方式訪問外部資源,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Netty分布式Server啟動流程服務端初始化源碼分析

    Netty分布式Server啟動流程服務端初始化源碼分析

    本章主要講解server啟動的關(guān)鍵步驟,?讀者只需要了解server啟動的大概邏輯,?知道關(guān)鍵的步驟在哪個類執(zhí)行即可,?并不需要了解每一步的運作機制,?之后會對每個模塊進行深度分析
    2022-03-03
  • SpringCloud微服務網(wǎng)關(guān)限流方式

    SpringCloud微服務網(wǎng)關(guān)限流方式

    這篇文章主要介紹了SpringCloud微服務網(wǎng)關(guān)限流方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-08-08
  • 一篇文章教你使用枚舉來實現(xiàn)java單例模式

    一篇文章教你使用枚舉來實現(xiàn)java單例模式

    本篇文章主要介紹了Java實現(xiàn)單例的3種普遍的模式,餓漢式、懶漢式、枚舉式。具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能給你帶來幫助
    2021-07-07
  • java多線程并發(fā)executorservice(任務調(diào)度)類

    java多線程并發(fā)executorservice(任務調(diào)度)類

    這篇文章主要介紹了線程并發(fā)ScheduledExecutorService類,設(shè)置 ScheduledExecutorService ,2秒后,在 1 分鐘內(nèi)每 10 秒鐘蜂鳴一次
    2014-01-01
  • 在Java8中構(gòu)建Stream流的多種方式詳解

    在Java8中構(gòu)建Stream流的多種方式詳解

    當我們處理集合數(shù)據(jù)時,往往需要對其進行各種操作,如過濾、映射、排序、歸約等,在 Java 8 中引入的 Stream 流為我們提供了一種更加簡潔和靈活的方式來處理數(shù)據(jù),本文將介紹如何基于 Stream 構(gòu)建流,為你展示創(chuàng)建和操作流的多種方法
    2023-08-08
  • SpringBoot參數(shù)校驗的方法總結(jié)

    SpringBoot參數(shù)校驗的方法總結(jié)

    今天帶大家學習SpringBoot參數(shù)校驗的方法,文中有非常詳細的代碼示例,對正在學習java的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05

最新評論