錯誤描述:大致意思就是有多個ServletWebServerFactory spring不知道啟動那個
org.springframework.context.ApplicationContextException: Unable to start web server; nested exception is org.springframework.context.ApplicationContextException: Unable to start ServletWebServerApplicationContext
due to multiple ServletWebServerFactory beans : tomcatServletWebServerFactory,webServerFactory at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.onRefresh(ServletWebServerApplicationContext.java:155) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:540) at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:140) at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248)
起因:
最近公司要做小程序,由於微信要求接口必須是https的,然后就開始springboot整合https,整合https具體細節就不說了。
便於用戶體驗,讓用戶可以http也可以正常訪問https。配置如下:
@Bean public Connector connector(){ Connector connector=new Connector("org.apache.coyote.http11.Http11NioProtocol"); connector.setScheme("http"); connector.setPort(80); connector.setSecure(false); connector.setRedirectPort(serverPortHttps); return connector; } @Bean public TomcatServletWebServerFactory tomcatServletWebServerFactory(Connector connector){ TomcatServletWebServerFactory tomcat = new TomcatServletWebServerFactory() { @Override protected void postProcessContext(Context context) { SecurityConstraint securityConstraint = new SecurityConstraint(); securityConstraint.setUserConstraint("CONFIDENTIAL"); SecurityCollection collection = new SecurityCollection(); collection.addPattern("/*"); securityConstraint.addCollection(collection); context.addConstraint(securityConstraint); } }; tomcat.addAdditionalTomcatConnectors(connector); return tomcat; }
好了啟動。。。。。噩夢開始報錯 ····????
代碼寫的名名白白就只有一個 TomcatServletWebServerFactory 怎么會有多個。。。。
好吧!還是先去網上查一下。。。
找了半天基本都是這樣的錯誤
Unable to start ServletWebServerApplicationContext due to missing ServletWebServerFactory bean.
人家都是缺少。。。我這。。。
上述錯誤基本都是缺少web包
網上沒找到怎么辦呢?
去看看源碼吧。
進去看看
報錯的地方是找到了
開調試確實是有兩個servlet, 為啥會有兩個呢。
讓后我把 TomcatServletWebServerFactory bean 注釋掉,結果就可以正常啟動。
咦好奇怪。
然后我就去看我的配置。
天~~·我發現了什么。。。
我啥時候配置的。。。終於找到了問題的原因,之前配置個TomcatServletWebServerFactory 忘記了,所以啟動會有兩個servlet。
自己給自己挖坑~~~