一、問題
在啟動(dòng)springcloud的gateway模塊的時(shí)候報(bào)錯(cuò)Please set spring.main.web-application-type=reactive or remove spring-boot-starter-web dependency.

二、問題產(chǎn)生的原因
gateway組件中的 spring-boot-starter-webflux 和 springboot作為web項(xiàng)目啟動(dòng)必不可少的 spring-boot-starter-web 出現(xiàn)沖突。
三、解決方案(任選一種就可以)
3.1 注釋pom.xml內(nèi)容
在gateway的pom文件上注釋掉spring-boot-starter-web代碼
? ? ? ? <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
3.2修改配置文件
在配置文件上加上
spring:
main:
web-application-type: reactive
解釋:這里面涉及到springboot的啟動(dòng)流程因?yàn)?span id="n5n3t3z" class="kdocs-bold" style="font-weight:bold;">spring-boot-starter-webflux包,在springboot啟動(dòng)類型里面表示的就是reactive。我們可以看源碼,了解一下springboot啟動(dòng)流程
一步一步點(diǎn)進(jìn)去


這是springApplication構(gòu)造方法,我們先看構(gòu)造方法

點(diǎn)進(jìn)去

3.2.1通過springboot源碼解釋
springApplication構(gòu)造源碼(不同版本會(huì)有差異,當(dāng)前版本是2.6.1)
public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
? ? ? ?
this.sources = new LinkedHashSet();
? ? ? ? // 在控制臺(tái)打印banner.txt文本內(nèi)容
this.bannerMode = Mode.CONSOLE;
this.logStartupInfo = true;
this.addCommandLineProperties = true;
this.addConversionService = true;
this.headless = true;
this.registerShutdownHook = true;
this.additionalProfiles = Collections.emptySet();
this.isCustomEnvironment = false;
this.lazyInitialization = false;
this.applicationContextFactory = ApplicationContextFactory.DEFAULT;
this.applicationStartup = ApplicationStartup.DEFAULT;
? ? ? ? // 開始輸resourceLoader注入了屬性null
this.resourceLoader = resourceLoader;
Assert.notNull(primarySources, "PrimarySources must not be null");
? ? ? ? //將啟動(dòng)類從數(shù)組重新封裝Set,注入到primarySources當(dāng)中
this.primarySources = new LinkedHashSet(Arrays.asList(primarySources));
? ? ? ? /**
? ? ? ? ? *webApplicationType有三種類型,REACTIVE,SERVLET,NONE
? ? ? ? ? *? 引入spring-boot-starter-web就是SERVLET
? ? ? ? ? *? 引入spring-boot-starter-webflux就是REACTIVE
? ? ? ? ? *? 沒有就是NONE
? ? ? ? */
this.webApplicationType = WebApplicationType.deduceFromClasspath();
? ? ? ? /**
? ? ? ? ? *從spring-cloud-context的jar包的META-INF/spring.factories文件中得到key為org.springfra ? ? ? ?? *mework.boot.BootstrapRegistryInitializer的全類名集合,進(jìn)行實(shí)例化,然后注入 bootstrapRegi
? ? ? ? ? *stryInitializers 屬性,其中核心方法getSpringFactoriesInstances
? ? ? ? ? *下面有g(shù)etSpringFactoriesInstances方法源碼詳解
? ? ? ? ? *這里簡(jiǎn)單的解釋一下getSpringFactoriesInstances方法就是從META-INF/spring.factories讀取配
? ? ? ? ? *置文件? ? ? ? ? ?
? ? ? ? ? */
this.bootstrapRegistryInitializers = new ArrayList(this.getSpringFactoriesInstances(BootstrapRegistryInitializer.class));
? ? ? ? /**
? ? ? ? ? *依然調(diào)用getSpringFactoriesInstances方法
? ? ? ? ? *從spring-boot的jar包的META-INF/spring.factories文件中得到key為org.springframework.? ? ? ? *context.ApplicationContextInitializer的全類名集合,然后進(jìn)行實(shí)例化,然后注入initializers
? ? ? ? ? *(初始化容器集合)屬性?
? ? ? ? ? */
this.setInitializers(this.getSpringFactoriesInstances(ApplicationContextInitializer.class));
? ? ? ? //跟上面一樣,這里是得到監(jiān)聽器的集合,并注入
this.setListeners(this.getSpringFactoriesInstances(ApplicationListener.class));
? ? ? ? //獲取當(dāng)前的main方法運(yùn)行的類,也就是我們的主類
this.mainApplicationClass = this.deduceMainApplicationClass();
}
上面說的META-INF/spring.factories都是在springboot.jar包中,不過BootstrapRegistryInitializer是在spring-cloud-context中。文章來源:http://www.zghlxwxcb.cn/news/detail-793869.html
3.2.2了解getSpringFactoriesInstances方法
隨便選一個(gè)點(diǎn)進(jìn)去文章來源地址http://www.zghlxwxcb.cn/news/detail-793869.html


private <T> Collection<T> getSpringFactoriesInstances(Class<T> type, Class<?>[] parameterTypes, Object... args) {
ClassLoader classLoader = this.getClassLoader();
? ? ? ? /** SpringFactoriesLoader.loadFactoryNames(type, classLoader)這里才是核心,
? ? ? ? ? *這個(gè)方法會(huì)掃描所有jar包類路徑下 META-INF/spring.factories
? ? ? ? ? */
Set<String> names = new LinkedHashSet(SpringFactoriesLoader.loadFactoryNames(type, classLoader));
List<T> instances = this.createSpringFactoriesInstances(type, parameterTypes, classLoader, args, names);
AnnotationAwareOrderComparator.sort(instances);
return instances;
}
到了這里,關(guān)于Please set spring.main.web-application-type=reactive or remove spring-boot-starter-web dependency.的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!