Fat Jar jsp 지원을 위한 Spring Boot 심화
jsp의 지원에 대해 Spring Boot 공식은war의 포장 방식만 지원하고fatjar는 지원하지 않습니다.공식 문서 참조: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-jsp-limitations
여기springboot는 공식적으로tomcat의 문제라고 하는데, 실제로springboot이 스스로 포장 형식을 바꾸어서 일어난 것이다.이전 기사 참조: https://www.jb51.net/article/141479.htm
원래의 구조 아래tomcat은fatjar의
META-INF/resources 디렉터리 아래의 자원을 스캔할 수 있다.BOOT-INF/classes이 증가한 후에tomcat 스캐닝이 불가능합니다.그러면 이 문제를 어떻게 해결합니까?스프링bootfatjar/explodeddirectory에 대한 jsp 지원을 위한 방안을 제시합니다.
맞춤형 설정tomcat, BOOT-INF/classes를tomcat의 ResourceSet에 추가
tomcat에서 스캔한 모든 자원은 이른바
ResourceSet 안에 넣는다.예를 들어 servlet3규범에 응용된jar패키지META-INF/resources는 하나ResourceSet이다.스프링부트에서 나온 fatjar의
BOOT-INF/classes 디렉터리를 ResourceSet 에 추가할 방법을 강구해야 합니다.다음은tomcat을 실현하는 LifecycleListener 인터페이스를 통해 Lifecycle에 있습니다.CONFIGURE_START_이벤트 이벤트에서 BOOT-INF/classes의 URL을 얻고 이 URL을 WebResourceSet에 추가합니다.
/**
 * Add main class fat jar/exploded directory into tomcat ResourceSet.
 *
 * @author hengyunabc 2017-07-29
 *
 */
public class StaticResourceConfigurer implements LifecycleListener {
 private final Context context;
 StaticResourceConfigurer(Context context) {
  this.context = context;
 }
 @Override
 public void lifecycleEvent(LifecycleEvent event) {
  if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
   URL location = this.getClass().getProtectionDomain().getCodeSource().getLocation();
   if (ResourceUtils.isFileURL(location)) {
    // when run as exploded directory
    String rootFile = location.getFile();
    if (rootFile.endsWith("/BOOT-INF/classes/")) {
     rootFile = rootFile.substring(0, rootFile.length() - "/BOOT-INF/classes/".length() + 1);
    }
    if (!new File(rootFile, "META-INF" + File.separator + "resources").isDirectory()) {
     return;
    }
    try {
     location = new File(rootFile).toURI().toURL();
    } catch (MalformedURLException e) {
     throw new IllegalStateException("Can not add tomcat resources", e);
    }
   }
   String locationStr = location.toString();
   if (locationStr.endsWith("/BOOT-INF/classes!/")) {
    // when run as fat jar
    locationStr = locationStr.substring(0, locationStr.length() - "/BOOT-INF/classes!/".length() + 1);
    try {
     location = new URL(locationStr);
    } catch (MalformedURLException e) {
     throw new IllegalStateException("Can not add tomcat resources", e);
    }
   }
   this.context.getResources().createWebResourceSet(ResourceSetType.RESOURCE_JAR, "/", location,
     "/META-INF/resources");
  }
 }
}
@Configuration
@ConditionalOnProperty(name = "tomcat.staticResourceCustomizer.enabled", matchIfMissing = true)
public class TomcatConfiguration {
 @Bean
 public EmbeddedServletContainerCustomizer staticResourceCustomizer() {
  return new EmbeddedServletContainerCustomizer() {
   @Override
   public void customize(ConfigurableEmbeddedServletContainer container) {
    if (container instanceof TomcatEmbeddedServletContainerFactory) {
     ((TomcatEmbeddedServletContainerFactory) container)
       .addContextCustomizers(new TomcatContextCustomizer() {
        @Override
        public void customize(Context context) {
         context.addLifecycleListener(new StaticResourceConfigurer(context));
        }
       });
    }
   }
  };
 }
}demo 주소: https://github.com/hengyunabc/spring-boot-fat-jar-jsp-sample
총결산
BOOT-INF/classesStaticResourceConfigurer를 통해fatjar의/BOOT-INF/classes를tomcat의ResourceSet에 추가하여 문제를 해결이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
[MeU] Hashtag 기능 개발➡️ 기존 Tag 테이블에 존재하지 않는 해시태그라면 Tag , tagPostMapping 테이블에 모두 추가 ➡️ 기존에 존재하는 해시태그라면, tagPostMapping 테이블에만 추가 이후에 개발할 태그 기반 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.