美文网首页
从session.timeout看application.pro

从session.timeout看application.pro

作者: 寒飞子 | 来源:发表于2018-07-20 10:55 被阅读712次

    前言

    我们知道session的超时时间是配置在web.xml里的,但改成spring boot方式后要如何配置超时时间呢?在百度和谷歌上找了一段时间,都是server.session.timeout,但配置后都是没效果。因为spring boot 2.0和1.x相比调整了一些application.properties里属性的名称,而且网上spring boot 2.0的资料也比较少。后来自己看了下源码,终于被我找到正确的属性名,是server.servlet.session.timeout,亲测有效。那么,我们怎么看一个配置的属性名是什么呢?

    源代码

    我们知道application.properties里的属性都会映射到一个配置属性类里,server的配置属性类是org.springframework.boot.autoconfigure.web.ServerProperties,从注解上可以看到前缀是server,还有几个属性对象,主要看这个servlet。

    @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true)
    public class ServerProperties {
        /**
         * Server HTTP port.
         */
        private Integer port;
        @NestedConfigurationProperty
        private Ssl ssl;
    
        private final Servlet servlet = new Servlet();
    
        private final Tomcat tomcat = new Tomcat();
    
        private final Jetty jetty = new Jetty();
    
        private final Undertow undertow = new Undertow();
    

    其中servlet这个属性对象里还有一些属性对象,主要看这个session。

        /**
         * Context path of the application.
         */
        private String contextPath;
    
        @NestedConfigurationProperty
        private final Session session = new Session();
    

    这个session里就有我们要找的timeout,单位是秒。

        @DurationUnit(ChronoUnit.SECONDS)
        private Duration timeout = Duration.ofMinutes(30);
    

    从以上的对象所属关系我们就可以看出这个timeout的属性名称为server.servlet.session.timeout,"."表示是前一个的属性对象,像contextPath这种驼峰表示法,配置在application.properties里推荐采用context-path这种方式表示。

    结后语

    以后再想查属性名称就可以直接看源代码了,比百度和谷歌来的靠谱多了。

    相关文章

      网友评论

          本文标题:从session.timeout看application.pro

          本文链接:https://www.haomeiwen.com/subject/mjlfmftx.html