1

I am trying to set the session timeout for my application using property files. However, when trying to load files from the property file it is rendering "null" for the value. Here is my source code

SessionListener.java

@Component
@PropertySource(value = "classpath:/app.properties")
public class SessionListener implements HttpSessionListener {

    @Value("${session.timeout}")
    private String sessionTimeout;

    // To change session timings change Constants
    @Override
    public void sessionCreated(HttpSessionEvent httpSessionEvent) {
        httpSessionEvent.getSession().setMaxInactiveInterval(Integer.parseInt(sessionTimeout)*60);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent httpSessionEvent) {
    }
}

app.properties file

session.timeout = 45

web.xml file

<?xml version="1.0" encoding="UTF-8" ?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">
    <welcome-file-list>
        <welcome-file>login</welcome-file>
    </welcome-file-list>
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.AppConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>


    <display-name>
        Application
    </display-name>
    <listener>
        <listener-class>com.SessionListener</listener-class>
    </listener>
</web-app>

Am I missing any configuration? When trying to debug the control is coming to the SessionListener class, however the value annotated is rendered as null. Or is it something like the SessionListener class code is executed before the spring framework could inject the annotated variables?

Moreover, please suggest me any other best way if exists, to set session timeout other than property file definition.

  • I fixed the issue by adding listener as mentioned in this link[https://stackoverflow.com/a/14130105/2559237] and removed it from web.xml However, the servlet version needs to be 3.0.1 for doing that. – Dhananjayan Santhanakrishnan Jul 09 '17 at 19:01

0 Answers0