2016-12-16 52 views
2

我的WAR應用程序使用非Spring庫(對於JSF)。該庫使用servletContext.getResource("page.html")進行初始化。 page.html是在WEB-INF/lib下一個JAR內,包裝成META-INF/resources/page.html嵌入式servlet容器在Spring Boot中不處理META-INF /資源

這時候我上部署servlet容器WAR做工精良。但是,當我將應用程序作爲可執行WAR運行時,它不起作用,因爲嵌入式servlet容器不會掃描classpath META-INF /資源。

例如,對於暗潮類路徑中的資源管理器不使用:

private ResourceManager getDocumentRootResourceManager() { 
    File root = getCanonicalDocumentRoot(); 
    if (root.isDirectory()) { 
     return new FileResourceManager(root, 0); 
    } 
    if (root.isFile()) { 
     return new JarResourceManager(root); 
    } 
    return ResourceManager.EMPTY_RESOURCE_MANAGER; 
} 

https://github.com/spring-projects/spring-boot/blob/master/spring-boot/src/main/java/org/springframework/boot/context/embedded/undertow/UndertowEmbeddedServletContainerFactory.java#L466

時間的問題:爲什麼嵌入式servlet容器忽略META-INF/resources?製作可執行的Servlet 3.0應用程序是一個問題。

類似的問題:

Embedded Tomcat, executable jar, ServletContext.getRealPath()

https://github.com/spring-projects/spring-boot/issues/4218

+0

我已經使用過很多的spring引導,並且還沒有聽說過可執行文件的戰爭。我想知道這是多麼的支持。 – eis

回答

1

我解決我的問題有以下配置:

@Bean 
public UndertowEmbeddedServletContainerFactory embeddedServletContainerFactory() { 
    UndertowEmbeddedServletContainerFactory factory = 
    new UndertowEmbeddedServletContainerFactory(); 
    factory.addDeploymentInfoCustomizers(new UndertowDeploymentInfoCustomizer() { 

     @Override 
     public void customize(DeploymentInfo deploymentInfo) { 
      deploymentInfo.setResourceManager(
       new ClassPathResourceManager(deploymentInfo.getClassLoader(), 
        "META-INF/resources")); 
    }); 
    return factory; 
} 

一般來說,我認爲這是未公開的Web容器如何嵌入式的行爲。如果嵌入式Web容器爲應用程序提供了一些功能子集,Spring Boot開發人員感到非常高興,但遷移現有應用程序的人希望嵌入式容器提供常規容器的所有功能。例如ServletContainerInitializers在嵌入時被忽略:https://github.com/spring-projects/spring-boot/issues/321