spring初始化的时候从数据库中取出数据存到session的context域中

WebApplicationContext

WebApplicationContext 可以获取bean,然后执行方法获取数据。

web.xml

   <listener>
        <listener-class>com.powernode.workbench.controller.TblDicController</listener-class>
    </listener>

监听器层

package com.node.workbench.controller;
/**
 * Description:
 * date: 2020/7/29 16:20
 * Version
 * Since JDK 1.8
 *
 * @Author Lin
 */

import com.powernode.workbench.pojo.TblDicValue;
import com.powernode.workbench.service.tbldic.impl.TblDicValueServiceImpl;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.util.Map;
import java.util.TreeSet;
/**
 * ClassName: TblDicController
 * Description: 线索的控制层 监听器
 * Date: 2020/7/29 16:20
 * @Author Lin
 */
public class TblDicController   implements ServletContextListener {
//  初始化的时候启动
    @Override
    public void contextInitialized(ServletContextEvent sce) {
        //获取context域
        ServletContext servletContext = sce.getServletContext();
//        自定义一个ioc 
        WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        /*反射机制 获取到类的对象 */
        TblDicValueServiceImpl bean = webApplicationContext.getBean(TblDicValueServiceImpl.class);
//        反射机制 获取类的方法
        Map<String, TreeSet<TblDicValue>> stringTreeSetMap = bean.TblDicValueAll();
//      向context域中塞进 数据库的文件
        servletContext.setAttribute("dicValue",stringTreeSetMap);
//        Map<String, TreeSet<TblDicValue>> tblDicValueTreeMap = tblDicValueService.TblDicValueAll();
        System.out.println(stringTreeSetMap);
//        request.getSession().getServletContext().setAttribute("Valuecode",tblDicValueTreeMap);
    }
    //  销毁的的时候启动
    @Override
    public void contextDestroyed(ServletContextEvent sce) {

    }
}

服务层

package com.powernode.workbench.service.tbldic.impl;
/**
 * Description:
 * date: 2020/7/29 9:24
 * Version
 * Since JDK 1.8
 *
 * @Author Lin
 */

import com.powernode.workbench.mapper.TblDicTypeMapper;
import com.powernode.workbench.mapper.TblDicValueMapper;
import com.powernode.workbench.pojo.TblDicType;
import com.powernode.workbench.pojo.TblDicValue;
import com.powernode.workbench.pojo.TblDicValueExample;
import com.powernode.workbench.service.tbldic.TblDicValueService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.TreeSet;

/**
 * ClassName: TblDicValueServiceImpl
 * Description: 
 * Date: 2020/7/29 9:24
 * @Author Lin
 */
@Service
public class TblDicValueServiceImpl implements TblDicValueService {

    @Autowired
    private TblDicTypeMapper tblDicTypeMapper;
    @Autowired
    private TblDicValueMapper tblDicValueMapper;
    public Map<String, TreeSet<TblDicValue>> TblDicValueAll() {
//            创建tree set集合 用来存储 divValue的值

        Map<String, TreeSet<TblDicValue>> tblDicValueTreeMap = new HashMap<>();
//            dictype  一对多, 一个type 下的code 对应 value下的 typeCode  typecode 可以获得多个value

        List<TblDicType> tblDicTypes = tblDicTypeMapper.selectByExample(null);
        for (TblDicType s:tblDicTypes) {
//            获取 dictype 下面的code
            String code = s.getCode();
            //根据code 遍历dic value数据库
            TblDicValueExample tblDicValueExample = new TblDicValueExample();
            TblDicValueExample.Criteria criteria = tblDicValueExample.createCriteria();
            criteria.andTypecodeEqualTo(code);
            List<TblDicValue> tblDicValues = tblDicValueMapper.selectByExample(tblDicValueExample);
//          dicvalue 存入到set集合
            TreeSet<TblDicValue> tblDicValuesTreeSet = new TreeSet<>();
            tblDicValuesTreeSet.addAll(tblDicValues);
//                System.out.println(tblDicValuesTreeSet);
//            把Treeset集合 以<code DicValue >放进 TreeMap 集合
            tblDicValueTreeMap.put(code,tblDicValuesTreeSet);

        }
//         System.out.println("输出 treeMap集合 -------"+objectObjectTreeMap);

        return tblDicValueTreeMap;
    }


}

WebApplicationContextUtils获取context域

好多时候,我们都想在过滤器里面获取到spring的容器,从而可以拿到spring容器里面的一些bean。但要认识到一点,过滤器里面是否可以直接拿到,tomcat容器启动的时候,首先加载context-param的配置文件,也就是spring容器。然后加载监听器,再加载过滤器。所以过滤器肯定可以拿到spring的容器。

WebApplicationContext代表着spring容器,
而且它是以WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE 为键,
存放在ServletContext里面的,所以我们可以使用	

WebApplicationContext wac = 
(WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);

获得spring容器,但是这样很麻烦,好多人并不知道它的键是什么。所以我们可以使用工具类实现:

 这个工具类在org.springframework.web.context.support包中。
WebApplicationContext wac =WebApplicationContextUtils.getWebApplicationContext(servletContext);
  • 当 ServletContext 属性列表中不存在 WebApplicationContext 时,getWebApplicationContext() 方法不会抛出异常,它简单地返回 null。

  • 如果后续代码直接访问返回的结果将引发一个 NullPointerException 异常,而
    WebApplicationContextUtils 另一个getRequiredWebApplicationContext(ServletContext sc) 方法要求ServletContext 属性列表中一定要包含一个有效的 WebApplicationContext 对象,否则马上抛出一个IllegalStateException 异常。

  • 我们推荐使用后者,因为它能提前发现错误的时间,强制开发者搭建好必备的基础设施。

package cn.sccl.common.web;

import java.util.List;
import java.util.Map;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.ContextLoaderListener;
import org.springframework.web.context.support.WebApplicationContextUtils;

import cn.sccl.common.service.BizCodeManager;
import cn.sccl.common.web.util.Log4jWebConfigurer;
import cn.sccl.pms.model.Division;
import cn.sccl.pms.model.DivisionQuery;
import cn.sccl.pms.service.DivisionManager;

public class StartupListener extends ContextLoaderListener implements ServletContextListener {

	@Override
	public void contextInitialized(ServletContextEvent event) {
		super.contextInitialized(event);
		ServletContext context = event.getServletContext();//获取servletContext
		//也可以在实现了HttpServlet接口中获取,ServletContext servletContext = this.getServletContext();  
		setupContext(context);
	}

	protected void setupContext(final ServletContext context) {
//		 WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		 //推荐使用这种,因为getRequiredWebApplicationContext要求servletContext中必须要有ApplicationContext
		ApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(context);
	 
		// 行政区划   
		DivisionManager divisionManager = (DivisionManager) ctx.getBean("divisionManager");//得到manager
		List<Division> divisions = divisionManager.query(new DivisionQuery());
		//将数据放入ServletContext 页面中就在application中获取,因为application和servletContext对应
		context.setAttribute("divisions", divisions);
	}


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值