MAVEN下实现spring boot打包成war包(spring boot 01)
2015-08-03 22:08
721 查看
配置类(带有@SpringBootApplication)继承SpringBootServletInitializer,同时重写configure方法
e.g: @SpringBootApplication public class Application extends SpringBootServletInitializer { @Override protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { return application.sources(Application.class); } public static void main(String[] args) throws Exception { SpringApplication.run(Application.class, args); } }
修改pom.xml中package
<packaging>jar</packaging> --> <packaging>war</packaging>
标记嵌入的容器dependencies as “provided”
e.g: <dependencies> <!-- ... --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- ... --> </dependencies>
Create a deployable war file for older servlet containers
Older Servlet containers don’t have support for the ServletContextInitializer bootstrap process used in Servlet 3.0. You can still use Spring and Spring Boot in these containers but you are going to need to add a web.xml to your application and configure it to load an ApplicationContext via a DispatcherServlet.Spring 同时提醒我们,如果是比较早的servlet容器(低于Servlet 3.0),可能不支持ServletContextInitializer,这时我们需要给项目配置一个web.xml,并将配置类加载进项目。以下是spring官方给出的方案:
Deploying a WAR in an Old (Servlet 2.5) Container
Spring Boot uses Servlet 3.0 APIs to initialize the ServletContext (register Servlets etc.) so you can’t use the same application out of the box in a Servlet 2.5 container. It is however possible to run a Spring Boot application on an older container with some special tools. If you include org.springframework.boot:spring-boot-legacy as a dependency (maintained separately to the core of Spring Boot and currently available at 1.0.0.RELEASE), all you should need to do is create a web.xml and declare a context listener to create the application context and your filters and servlets. The context listener is a special purpose one for Spring Boot, but the rest of it is normal for a Spring application in Servlet 2.5.e.g: <?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> <context-param> <param-name>contextConfigLocation</param-name> <!-- demo.Application即为被标注@SpringBootApplication的类 --> <param-value>demo.Application</param-value> </context-param> <listener> <!-- 添加监听SpringBootContextLoaderListener --> <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener</listener-class> </listener> <filter> <filter-name>metricFilter</filter-name> <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class> </filter> <filter-mapping> <filter-name>metricFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>appServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextAttribute</param-name> <param-value>org.springframework.web.context.WebApplicationContext.ROOT</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>appServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
@see more
http://spring.io/guides/gs/convert-jar-to-war/http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#build-tool-plugins-maven-packaging
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file
http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-servlet-2-5
相关文章推荐
- 关于java ee 7 精粹
- java线程池原理
- java设计模式(二)
- 使用eclipse tool 逆向生成Hiberante类xml、和hbm.xml
- java 类加载机制
- Java进阶(五十一)必须记住的Myeclipse快捷键
- 必须记住的Myeclipse快捷键
- java IO总结
- JAVA 语言基础总结
- Java删除ArrayList中的重复元素的2种方法
- java基础第八讲——URL、XML的解析
- Java Timer 定时器的使用
- Java异常处理——浅谈
- 一句话解释什么是回调函数
- Java学习--(五)Buliding a Java Program(使用eclipse)
- Java并发编程:volatile关键字解析
- Java 反射机制原理
- JDK 和 JRE 概念
- java实现单链表
- Java1.5泛型指南中文版(Java1.5 Generic Tutorial)