通过ServletContextListener监听进行数据的初始化

49 篇文章 1 订阅

2、由于web容器在启动的过程中实现了对ServletContextListener这个接口的监听,当程序中有ServletContextListener这个接口被实例化的时候,web容器会让其运行contextInitialized()这个方法

package com.wangyong.app.web.listener;

import com.wangyong.app.sys.facade.app.*;
import com.wangyong.app.sys.push.SystemConfig;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.servlet.http.HttpSessionAttributeListener;
import javax.servlet.http.HttpSessionBindingEvent;


@Component
public class DateInitListener implements HttpSessionAttributeListener, ServletContextListener {

    private Logger logger = Logger.getLogger(DateInitListener.class);

    DSysIndustryFacade dSysIndustryFacade = null;

    DSysUserRoleFacade dSysUserRoleFacade = null;

    DSysAreaFacade dSysAreaFacade = null;

    DSysBankFacade dSysBankFacade = null;

    DSysDataDictionaryFacade dSysDataDictionaryFacade = null;

    @Override
    public void attributeAdded(HttpSessionBindingEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void attributeRemoved(HttpSessionBindingEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void attributeReplaced(HttpSessionBindingEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void contextDestroyed(ServletContextEvent arg0) {
        // TODO Auto-generated method stub
    }

    @Override
    public void contextInitialized(ServletContextEvent arg0) {
        System.out.println("******************************初始化系统******************************");
        WebApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(arg0.getServletContext());
        try{
            SystemConfig.beforeClass();
        }catch (Exception e){
            e.printStackTrace();
        }

        //初始化行业数据
        dSysIndustryFacade = (DSysIndustryFacade) applicationContext.getBean("dSysIndustryFacade");
        dSysIndustryFacade.initIndustry();

        //初始化系统角色数据
        dSysUserRoleFacade = (DSysUserRoleFacade) applicationContext.getBean("dSysUserRoleFacade");
        dSysUserRoleFacade.initUserRoleDate();

        //初始化区域数据
        dSysAreaFacade = (DSysAreaFacade) applicationContext.getBean("dSysAreaFacade");
        //数据格式key:120103   value:{"areaCode": "450203","areaName": "鱼峰区","locationName": "广西壮族自治区-柳州市-鱼峰区"}
        dSysAreaFacade.initAreaRedisData();
        //功能:redis缓存初始化area地区三级联动数据
        dSysAreaFacade.redisAreaLevelThreeLinkage();
        //单独缓存Tb_sys_area省市区三级数据	 * 分别为REDIS_SYS_AREA_PROVINCE(省份),REDIS_SYS_AREA_CITY(城市),REDIS_SYS_AREA_REGION(区县)
        dSysAreaFacade.redisSyaArea();

        //初始化银行总行数据
        dSysBankFacade = (DSysBankFacade) applicationContext.getBean("dSysBankFacade");
        dSysBankFacade.initBank();

        //初始化数据字典
        dSysDataDictionaryFacade = (DSysDataDictionaryFacade) applicationContext.getBean("dSysDataDictionaryFacade");
        dSysDataDictionaryFacade.initDataDictionar();
    }

}
因此只需要将需要初始化数据的业务放在contextInitialized()中

2、数据初始化的监听器开发完成后,就是需要将其配置到web容器中,使其在web容器启动后将监听进行启用加载

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
	http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
	<display-name>app</display-name>

	<!-- 加载Spring配置文件 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:spring-starter.xml</param-value>
	</context-param>

	<!-- Spring监听器 -->
	<listener>
		<description>Spring监听器</description>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<!-- 防止Spring内存溢出监听器 -->
	<listener>
		<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
	</listener>
	<!-- 添加自定义监听器 -->

	<listener>
		<listener-class>com.wangyong.app.web.listener.DateInitListener</listener-class>
	</listener>

        <!-- Spring MVC -->
	<servlet>
		<servlet-name>SpringMVC</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<description>SpringMVC</description>
			<param-name>contextConfigLocation</param-name>
			<param-value></param-value>
			<!-- param-value>classpath:spring-starter-conf/spring-mvc.xml</param-value-->
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>SpringMVC</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>
	

</web-app>

<listener>
        <listener-class>com.wangyong.app.web.listener.DateInitListener</listener-class>
    </listener>

<context-parm>加载配置文件

<listener>启用监听

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值