前言
我們知道session的超時(shí)時(shí)間是配置在web.xml里的,但改成spring boot方式后要如何配置超時(shí)時(shí)間呢漫贞?在百度和谷歌上找了一段時(shí)間,都是server.session.timeout,但配置后都是沒效果巨缘。因?yàn)閟pring boot 2.0和1.x相比調(diào)整了一些application.properties里屬性的名稱尿赚,而且網(wǎng)上spring boot 2.0的資料也比較少散庶。后來自己看了下源碼,終于被我找到正確的屬性名凌净,是server.servlet.session.timeout悲龟,親測(cè)有效。那么冰寻,我們?cè)趺纯匆粋€(gè)配置的屬性名是什么呢须教?
源代碼
我們知道application.properties里的屬性都會(huì)映射到一個(gè)配置屬性類里,server的配置屬性類是org.springframework.boot.autoconfigure.web.ServerProperties斩芭,從注解上可以看到前綴是server轻腺,還有幾個(gè)屬性對(duì)象,主要看這個(gè)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這個(gè)屬性對(duì)象里還有一些屬性對(duì)象贬养,主要看這個(gè)session。
/**
* Context path of the application.
*/
private String contextPath;
@NestedConfigurationProperty
private final Session session = new Session();
這個(gè)session里就有我們要找的timeout琴庵,單位是秒误算。
@DurationUnit(ChronoUnit.SECONDS)
private Duration timeout = Duration.ofMinutes(30);
從以上的對(duì)象所屬關(guān)系我們就可以看出這個(gè)timeout的屬性名稱為server.servlet.session.timeout,"."表示是前一個(gè)的屬性對(duì)象迷殿,像contextPath這種駝峰表示法儿礼,配置在application.properties里推薦采用context-path這種方式表示。
結(jié)后語
以后再想查屬性名稱就可以直接看源代碼了庆寺,比百度和谷歌來的靠譜多了蚊夫。