springboot部署在tomcat中需要注意的地方
阿新 • • 發佈:2019-01-29
如果想要將springboot的web專案打包部署進Tomcat的webapps目錄下,單純更改packaging為war,補全web.xml檔案打包後將war包放進去是不行的,需要在springboot的啟動類中做以下的程式碼更改:
要此類要繼承SpringbootServletInitializer類,並重寫他的configure方法,具體程式碼如下:
import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.jetty.JettyEmbeddedServletContainerFactory; import org.springframework.boot.web.support.SpringBootServletInitializer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ImportResource; @SpringBootApplication//spring boot自帶的註解具有掃描主類所在的包的子包的bean的功能。 public class StartSpringBoot extends SpringBootServletInitializer{ //重寫configure方法,返回build.resource(啟動類的class物件),部署在tomcat中需要更改的地方 @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) { return builder.sources(StartSpringBoot.class); } public static void main(String[] args) throws Exception { SpringApplication.run(StartSpringBoot.class, args); }