springboot项目打包war问题

借用上一篇的切换,其中有一些问题漏掉了。之前也因为这个掉过一个大坑。所以在此记录一下点击打开链接 

根据官方文档(http://docs.spring.io/spring-boot/docs/current/reference/html/howto-traditional-deployment.html)说明:

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.2.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. Example:




<?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>
        <param-value>demo.Application </param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.boot.legacy.context.web.SpringBootContextLoaderListener </listener-class>
    </listener>

    <filter>
        <filter-name>metricsFilter </filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy </filter-class>
    </filter>

    <filter-mapping>
        <filter-name>metricsFilter </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>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值