服务器之家

服务器之家 > 正文

深入Spring Boot实现对Fat Jar jsp的支持

时间:2021-05-07 11:26     来源/作者:hengyunabc

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.zzvips.com/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

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
Intellij idea2020永久破解,亲测可用!!!
Intellij idea2020永久破解,亲测可用!!! 2020-07-29
返回顶部