继承ServletContextListener可以完成的事情

1.定时任务:

  定时任务是从某个固定的时间开始执行特定的程序,继承这个方法,可以实现刚启动项目的时候执行某特定的程序,完成给客户部署的时即可以看到某个页面的效果。

2.初始化系统常量等:

  这样来完成系统需要的一些数据,批量完成页面需要的某些变量,

  例如:Copyright © ${copyright_year}. ${applicationScope.cropyright_name}</div>

  而且这些元素是可以批量初始化到applicationScope中去的。所以有些项目根据jsp页面中的applicationScope.cropyright_name,全文查找是找不到页面中的元素在哪里存了。

  例如:

public void contextInitialized(ServletContextEvent servletContextEvent) {

 ServletContext servletContext = servletContextEvent.getServletContext();
ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
SysInformationMapper sysInformationMapper = applicationContext.getBean(SysInformationMapper.class);
SysInformationExample sysInformationExample = new SysInformationExample();
sysInformationExample.createCriteria().andEnabledEqualTo("1");
List<SysInformation> sysInformationList = sysInformationMapper.selectByExample(sysInformationExample);

if(sysInformationList!=null && sysInformationList.size()>0){
for(SysInformation sysInformation:sysInformationList){
servletContext.setAttribute(sysInformation.getInfoSign(),sysInformation.getInfoValue());
System.out.println(sysInformation.getInfoSign()+" >>> "+sysInformation.getInfoValue());
}
}
}

3.需要在web.xml中配置对应的listener

<listener>
<listener-class>com.huntech.web.listener.SystemConfigListener</listener-class>
</listener>
原文地址:https://www.cnblogs.com/wlhebut/p/7889600.html