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中修改listen,xml
由於若是新建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