Struts2初始化加载数据到缓存(listen)

1.新建一个类,继承listen类,重写contextInitialized方法web

public class ContextLoaderListenerOverWrite extends ContextLoaderListener {  spring

    private GetCatchInfoService getCatchService;//新建一个service,专门启动加载到缓存
    public static  Map<Integer,CreateUser> iniUserCatch = new HashMap<Integer,CreateUser>(); //保存初始化信息
    @Override  
    /** 
     * @description 重写ContextLoaderListener的contextInitialized方法 
     */  
    public void contextInitialized(ServletContextEvent event) {  
        super.contextInitialized(event);  
        ApplicationContext applicationContext = WebApplicationContextUtils.getWebApplicationContext(event.getServletContext());  
        //获取bean  
        getCatchService = (GetCatchInfoService) applicationContext.getBean("getCatchService");   //在spring bean中封装这个service,在此读取
        /* 
         具体地业务代码 
         */  
        List<CreateUser> listall = getCatchService.listAll();
        for(CreateUser listuser : listall){
            iniUserCatch.put(listuser.getUserid(), listuser);
            System.out.println("wwwwwwwwwwwwwwwwwwwwwwwwwwwwww");
        }
        System.out.println("nnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn");
    }  
}  缓存

 

2.在spring中注入新建service(专门读取初始化信息加入缓存的)app

//类中service不能用注解 注解是私有,这里是单例的 -->
    <bean id="getCatchService" class="demo.ssi.service.GetCatchInfoService"/>ide

 

3.在web.xml中修改listenxml

由于若是新建listen启动会先加载新建的,以前的信息不能注入,为了先加载bean再加载缓存类,要将listen替换成新建的缓存类,由于缓存类已经继承了listen,因此会先加载bean再加载初始化类继承

<!-- <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener> -->
    <!-- 缓存中的类重写listen中的方法,替代原有的listen -->
    <listener>
        <listener-class>demo.ssi.util.ContextLoaderListenerOverWrite</listener-class>
    </listener>ip

相关文章
相关标签/搜索