您的位置:首页 > 编程语言 > Java开发

SpringBoot项目打包部署外部JavaEE容器

2017-05-24 16:54 951 查看

前言

springBoot项目开发过程中使用的是内嵌的tomcat,jetty容器,当项目需要发布的时候怎么呢?

只需要两步

步骤

修改启动类,继承 SpringBootServletInitializer 并重写 configure 方法

/**
* Author: 遇见小星
* Email: tengxing7452@163.com
* Date: 17-5-10
* Time: 上午10:40
* Describe: 程序入口
*/
@SpringBootApplication
@EnableAutoConfiguration
public class Mainspringboot extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(Mainspringboot.class);
}


修改pom.xml文件

<!-- <packaging>jar</packaging> -->
<packaging>war</packaging>


tomcat插件依赖包添加生命周期privided

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>


结束语

SpringBootServletInitializer类implements WebApplicationInitializer接口,而WebApplicationInitializer里有void onStartup(ServletContext var1) 方法

void onStartup(ServletContext var1) throws ServletException;


SpringBootServletInitializer 实现了这个onStartup方法,主要在启动容器时负责加载相关配置

public void onStartup(ServletContext servletContext) throws ServletException {
this.logger = LogFactory.getLog(this.getClass());
final WebApplicationContext rootAppContext = this.createRootApplicationContext(servletContext);
if(rootAppContext != null) {
servletContext.addListener(new ContextLoaderListener(rootAppContext) {
public void contextInitialized(ServletContextEvent event) {
}
});
} else {
this.logger.debug("No ContextLoaderListener registered, as createRootApplicationContext() did not return an application context");
}

}


代码

servletContext.addListener(new ContextLoaderListener(rootAppContext) {
public void contextInitialized(ServletContextEvent event) {
}
});


中ContextLoaderListener实现了ServletContextListener接口,这样就可以通过addListener()的方式将springboot项目下的相关配置进行加载。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: