我正在尝试使用属性文件为我的应用程序设置会话超时。但是,当尝试从属性文件加载文件时,它将为该值呈现“null”。这是我的源代码
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文件
session.timeout = 45
web.xml文件
<?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>
我错过了任何配置吗?尝试调试时,控件将进入SessionListener类,但注释的值将呈现为null。或者类似于在Spring框架注入带注释的变量之前执行SessionListener类代码?
此外,如果存在,请建议我任何其他最佳方式,以设置除属性文件定义之外的会话超时。