激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 深入Spring Boot實現對Fat Jar jsp的支持

深入Spring Boot實現對Fat Jar jsp的支持

2021-05-07 11:26hengyunabc Java教程

這篇文章主要介紹了深入Spring Boot實現對Fat Jar jsp的支持,小編覺得挺不錯的,現在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

spring boot 對于jsp支持的限制

對于jsp的支持,Spring Boot官方只支持了war的打包方式,不支持fat jar。參考官方文檔: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-developing-web-applications.html#boot-features-jsp-limitations

這里spring boot官方說是tomcat的問題,實際上是spring boot自己改變了打包格式引起的。參考之前的文章:http://www.zmynmublwnt.cn/article/161572.html

原來的結構之下,tomcat是可以掃描到fat jar里的meta-inf/resources目錄下面的資源的。在增加了boot-inf/classes之后,則tomcat掃描不到了。

那么怎么解決這個問題呢?下面給出一種方案,來實現對spring boot fat jar/exploded directory的jsp的支持。

個性化配置tomcat,把boot-inf/classes 加入tomcat的resourceset

在tomcat里,所有掃描到的資源都會放到所謂的resourceset里。比如servlet 3規范里的應用jar包的meta-inf/resources就是一個resourceset

現在需要想辦法把spring boot打出來的fat jar的boot-inf/classes目錄加到resourceset里。

下面通過實現tomcat的 lifecyclelistener接口,在lifecycle.configure_start_event事件里,獲取到boot-inf/classes的url,再把這個url加入到webresourceset里。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/**
 * 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");
 
  }
 }
}

為了讓spring boot embedded tomcat加載這個 staticresourceconfigurer,還需要一個embeddedservletcontainercustomizer的配置:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
@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));
        }
       });
    }
   }
 
  };
 }
}

 這樣子的話,spring boot就可以支持fat jar里的jsp資源了。

demo地址:https://github.com/hengyunabc/spring-boot-fat-jar-jsp-sample

總結

  1. spring boot改變了打包結構,導致tomcat沒有辦法掃描到fat jar里的/boot-inf/classes
  2. 通過一個staticresourceconfigurer把fat jar里的/boot-inf/classes加到tomcat的resourceset來解決問題

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://blog.csdn.net/hengyunabc/article/details/80341946

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 久久人人爽人人爽人人片av高清 | 国产免费一级淫片a级中文 99国产精品自拍 | 亚洲国产精品一区 | 污版视频在线观看 | 久久精精品 | 欧美成a人片在线观看久 | 欧美高清另类自拍视频在线看 | 日本在线播放一区二区三区 | 日本va在线观看 | 国产一区二区二 | 91网视频| 中文字幕在线观看精品 | 免费观看国产精品视频 | 黑人日比视频 | 久久影院yy6080 | 国产成人aⅴ | 久久久久久久久久久久免费 | 免费一区在线 | 欧美精品一区自拍a毛片在线视频 | 久久久久女人精品毛片九一 | 一级网站 | 九九热视频在线 | 精品久久中文网址 | 91看片入口 | 视频一区二区三区在线播放 | 成人在线视频黄色 | 成人福利在线免费观看 | 日韩色电影| 久综合色 | 国产视频在线一区 | 一区二区三区在线视频观看58 | 韩国精品视频在线观看 | 国产精品一区二区免费在线观看 | 成人资源在线观看 | 亚洲成人免费电影 | 免费人成年短视频在线观看网站 | 精品国产91久久久 | 免费永久看羞羞片网站入口 | 国产精品久久久久久久久久久久久久久 | 全黄裸片武则天一级第4季 偿还电影免费看 | 国产婷婷一区二区三区 |