spring项目中监听器的写法
2010-10-22 10:42
357 查看
先说说监听器的作用:在spring项目的有一个大家熟知的监听器:ContextLoaderListener. 该监听器的作用是在web容器自动运行,加载spring的相关的配置文件,完成类的初始化工作。
在项目中我们因为某些操作会频繁的使用某些查询语句,但是查询数据量大,非常的耗时,每一个操作都会造成用户的等待时间变长,造成很不不好的体验。解决的一种方法就是写一个监听器,在web容器启动时,让它去查询出数据,并把数据放到缓存中。这样用户每一次操作都会自动从缓存中取出数据。
具体写法:参考ContextLoaderListener,可以看到它继承的是ServletContextListener接口,并实现了contextInitialized(ServletContextEvent sce)和contextDestroyed(ServletContextEvent sce)方法 ,从方法的名称中我们大概就可以猜出这两个方法的大概作用。
下面看具体的代码:
代码 public class CategoryListener implements ServletContextListener {
private final static Log log = LogFactory.getLog(CategoryListener.class);
public static final String LOCAL_CATEGORY_MANAGER_BEAN_NAME="localCategoryManager";
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
try {
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
IHello hello= (IHello )wac.getBean(“hello”);
hello.query();//查询数据
} catch (Exception e) {
log.error(e);
}
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
//To change body of implemented methods use File | Settings | File Templates.
}
}
在项目中我们因为某些操作会频繁的使用某些查询语句,但是查询数据量大,非常的耗时,每一个操作都会造成用户的等待时间变长,造成很不不好的体验。解决的一种方法就是写一个监听器,在web容器启动时,让它去查询出数据,并把数据放到缓存中。这样用户每一次操作都会自动从缓存中取出数据。
具体写法:参考ContextLoaderListener,可以看到它继承的是ServletContextListener接口,并实现了contextInitialized(ServletContextEvent sce)和contextDestroyed(ServletContextEvent sce)方法 ,从方法的名称中我们大概就可以猜出这两个方法的大概作用。
下面看具体的代码:
代码 public class CategoryListener implements ServletContextListener {
private final static Log log = LogFactory.getLog(CategoryListener.class);
public static final String LOCAL_CATEGORY_MANAGER_BEAN_NAME="localCategoryManager";
public void contextInitialized(ServletContextEvent servletContextEvent) {
ServletContext servletContext = servletContextEvent.getServletContext();
try {
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
IHello hello= (IHello )wac.getBean(“hello”);
hello.query();//查询数据
} catch (Exception e) {
log.error(e);
}
}
public void contextDestroyed(ServletContextEvent servletContextEvent) {
//To change body of implemented methods use File | Settings | File Templates.
}
}
相关文章推荐
- spring项目中监听器的写法
- spring项目中监听器作用-ContextLoaderListener(项目启动时,加载一些东西到缓存中)
- spring项目中使用监听器时获取spring中注入的service
- OA中总结:s:select,关于使用modelDriven,项目分层,@Transactional,jspf,各个层上配置注解交给spring管理的方法,简单的OGNL表达式写法
- maven项目:spring配置里:连接远程oracle(虚拟机里),【虚拟机没开启(数据库连接不上)导致tomcat启动失败(web.xml配置Spring了监听器)】=(也不报具体错误,就是超时)
- spring项目中监听器作用-ContextLoaderListener与Tomcat容器中的ServletContextListener
- spring项目中监听器作用-ContextLoaderListener
- spring心得2--bean的生命周期@Spring监听器的作用@Spring初始化容器案例分析@web项目使用
- spring项目中监听器作用-ContextLoaderListener
- spring心得2--bean的生命周期@Spring监听器的作用@Spring初始化容器案例分析@web项目使用
- Spring在Web项目中的应用-添加ServletContext监听器
- spring心得2--bean的生命周期@Spring监听器的作用@Spring初始化容器案例分析@web项目使用
- spring项目中监听器作用-ContextLoaderListener(项目启动时,加载一些东西到缓存中)
- 框架 day56 BOS项目练习(activiti,历史数据操作,流程变量,任务组,监听器,网关,spring整合)
- spring项目中监听器作用-ContextLoaderListener(转)
- spring整合的项目中监听器Listener如何获取注入的bean(监听器获取Spring配置文件)
- spring项目中,web.xml中的 ContextLoaderListener监听器的原理
- spring项目中监听器作用-ContextLoaderListener(项目启动时,加载一些东西到缓存中)
- BOS项目练习(activiti,历史数据操作,流程变量,任务组,监听器,网关,spring整合)
- spring项目中监听器作用-ContextLoaderListener