SpringBoot项目在eclipse启动成功,在idea中失败

来源:互联网 发布:linux使用教程.pdf 编辑:程序博客网 时间:2024/06/05 19:32

           最近用了SpringBoot的项目,使用了特定的方式来配置,让我们可以不用再看那么一坨xml真是神清气爽。之前的开发一直都是在eclipse上,因为习惯了IDEA,所以在idea上试了一下,结果出了点小问题。

             首先,springboot有两种启动方式,既可以直接通过main方法启动,也可以在tomcat里启动,在main方法里启动很简单,直接run启动类的main方法就可以了。在tomcat里启动是需要配置一下的,需要实现以下SpringBootServletInitializer

@EnableSwagger2@SpringBootApplication@EnableAutoConfigurationpublic class ClaimApplication extends SpringBootServletInitializer {    @Override    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {        return application.sources(ClaimApplication.class);    }}


        在eclipse中两种启动方式都可以,由于用idea打开了项目直接采用的是main方法的方式,导致了失败。其中过程中有几种报错是缺少一些jar包,所以当时认为是maven配置的问题,结果从maven配置入手重新下载了jar,结果无效。一直也很奇怪为什么同样的项目,在eclipse中可以,到了idea中就不行了。也许是idea配置的问题,因为eclipse中默认使用命名空间的方式,一个窗口一个命名空间,而idea中每个项目一个配置,后来在网上查了一些关于idea配置的问题,还是没有解决。因为启动程序的时候一直报的是因为找不到某个jar包,还有一个错误是这样的:      

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:690)at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)at org.springframework.boot.SpringApplication.run(SpringApplication.java:970)at org.springframework.boot.SpringApplication.run(SpringApplication.java:959)at com.mzjf.ClaimApplication.main(ClaimApplication.java:30)Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185)at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158)at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)... 7 common frames omitted

           查了查原因大概意思就是不能启动containerFactory,初始化容器失败。而网上说的是因为多引了CXF包,如下:

 <!-- Jetty is needed if you're are not using the CXFServlet -->   <dependency>     <groupId>org.apache.cxf</groupId>     <artifactId>cxf-rt-transports-http-jetty</artifactId>     <version>${cxf.version}</version>  </dependency>

          引入这个包后,当springboot启动的时候,发现classpath下存在Jetty的jar包,于是推测我们要使用的容器不是默认的tomcat而是jetty,于是使用jetty容器,但是在初始化jetty容器的时候,却由于在springboot中没有加入jetty starter导致一些jetty容器依赖的某些jar包环境又不存在,最终导致初始化容器失败。但是我并没有引入这个jar包,但是仿佛找到的思路,也许我也是多引了某个jar,而且和启动有关。所以顺势找到了我多引用的一个jar包。

          

          

               当我把这个jar删掉的时候

org.springframework.context.ApplicationContextException: Unable to start embedded container; nested exception is org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:133)at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:474)at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:118)at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:690)at org.springframework.boot.SpringApplication.run(SpringApplication.java:322)at org.springframework.boot.SpringApplication.run(SpringApplication.java:970)at org.springframework.boot.SpringApplication.run(SpringApplication.java:959)at com.mzjf.ClaimApplication.main(ClaimApplication.java:30)at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:498)at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)Caused by: org.springframework.context.ApplicationContextException: Unable to start EmbeddedWebApplicationContext due to missing EmbeddedServletContainerFactory bean.at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.getEmbeddedServletContainerFactory(EmbeddedWebApplicationContext.java:185)at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.createEmbeddedServletContainer(EmbeddedWebApplicationContext.java:158)at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.onRefresh(EmbeddedWebApplicationContext.java:130)... 12 common frames omitted

          

             然后加入jar

            

            使用main方法启动成功。

 

             至此问题解决,同时采用tomcat部署时应该采用上面的spring-boot-starter-tomcat,异常问题解决了,springboot两种方式都可以正常启动。


总结:

1、删除导致spring boot可能认为不使用默认容器的jar包,上例中可以删除依赖的tomcat的jar包

2、在springboot中加入spring-boot-starter-jetty,明确我们想使用什么容器,不要让spring boot引起误会和错误的可能猜测

3、Springboot默认集成的是Tomcat容易,如果想换成Jetty容器,首先则需要把默认的Tomcat容器去除,然后引入Jetty依赖


9 0
原创粉丝点击