bug 记录 Unable to start ServletWebServerApplicationContext due to multiple ServletWebServerFactory beans

错误描述:大致意思就是有多个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。

 

自己给自己挖坑~~~

 

posted @ 2020-05-05 11:55  花儿为何那样红  阅读(2906)  评论(0编辑  收藏  举报