Spring 打包为 WAR 的引导应用部署在 Jetty 9.2 上

Spring Boot app packaged as WAR deployed on Jetty 9.2

我创建了一个 Spring 引导应用程序。它在独立模式下正常运行。我的下一步是生成一个 WAR 文件,该文件可以由嵌入式 Jetty 服务器实例(版本 9.2)加载。

我设法生成了一个 WAR 文件。但是,默认情况下 Spring 引导注释不会被解释并且 Spring 引导本身不会加载(即当我打开与应用程序关联的 URL 时,会列出 WAR 文件) .

我发现使 Spring 启动的一种方法是在 WEB-INF[= 中添加一个 web.xml 文件47=] 具有启用 component-scan 的应用程序上下文。这样,Spring Boot 由嵌入式 Jetty 服务器加载。但是,看起来有些应用程序属性(来自 META-INFO/application.properties 文件)未被评估:例如 spring.jpa.hibernate.ddl-auto=update 没有效果, create-drop 是改为使用。

我能够重新配置一些 bean 以使某些应用程序属性起作用,但最后一个与休眠相关的属性不起作用。似乎所有 Spring 自动配置都未启用。

有没有办法将 Spring 引导应用程序打包为 WAR 部署在 Jetty 上,而无需 web.xml 或至少使用启用所有 Spring 自动配置的 通用 配置?

下面是与创建嵌入式 Jetty 服务器相关的代码:

Server server = createHttpServer(properties, restPort, httpsEnabled);
server.setStopAtShutdown(true);

HandlerList handlerList = new HandlerList();
addWarsToHandlerList(handlerList);
server.setHandler(handlerList);
server.start();

方法 addWarsToHandlerList 只是为所有 WAR 存档调用以下方法以部署到应用程序服务器上:

private static void addWarFile(HandlerList handlerList, File file) {
    String contextPath = "/" + FilenameUtils.getBaseName(file.getName());
    WebAppContext webApp = createWebAppContext(contextPath);
    webApp.setWar(file.getAbsolutePath());
    handlerList.addHandler(webApp);
    logger.debug("Deploying " + contextPath + " using war file " + file);
}

以下是Web配置。首先是我的 web.xml:

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>

    <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>

和上一个使用的applicationContext.xml文件:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-4.2.xsd">

    <context:component-scan base-package="my.package"/>

    <bean id="appConfigProperties"
          class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="location" value="classpath:application.properties"/>
    </bean>

    <bean id="filterMultipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
        <property name="maxUploadSize" value="100000000"/>
    </bean>

</beans>

欢迎任何评论、建议等。

Spring 自动配置已加载,但 application.properties 文件未加载。一个解决方案是在我的应用程序中使用@PropertySource class:

@PropertySource("classpath:application.properties") public class Application extends WebMvcConfigurerAdapter { ... }