如何让spring mvc web应用启动时就执行特定处理
2017-10-27 21:43
429 查看
Asp.Net的应用中通过根目录下的Global.asax,在Application_Start方法中做一些初始化操作,比如:预先加载缓存项对网站热点数据进行预热,获取一些远程的配置信息等等。
Spring-MVC的应用中,要实现类似的功能,主要是通过实现下面这些接口(任选一,至少一个即可)
运行时,输出的顺序如下:
1 => StartupListener.setApplicationContext
2 => StartupListener.setServletContext
3 => StartupListener.afterPropertiesSet
4.1 => MyApplicationListener.onApplicationEvent
4.2 => MyApplicationListener.onApplicationEvent
4.1 => MyApplicationListener.onApplicationEvent
注意:onApplicationEvent方法会触发多次,初始化这种事情,越早越好,建议在setApplicationContext方法中处理。
框架:spring,spring mvc,mybatis
Spring-MVC的应用中,要实现类似的功能,主要是通过实现下面这些接口(任选一,至少一个即可)
一、ApplicationContextAware接口
package org.springframework.context; import org.springframework.beans.BeansException; import org.springframework.beans.factory.Aware; import org.springframework.context.ApplicationContext; public interface ApplicationContextAware extends Aware { void setApplicationContext(ApplicationContext var1) throws BeansException; }
二、ServletContextAware 接口
package org.springframework.web.context; import javax.servlet.ServletContext; import org.springframework.beans.factory.Aware; public interface ServletContextAware extends Aware { void setServletContext(ServletContext var1); }
三、InitializingBean 接口
package org.springframework.beans.factory; public interface InitializingBean { void afterPropertiesSet() throws Exception; }
四、ApplicationListener<ApplicationEvent> 接口
package org.springframework.context; import java.util.EventListener; import org.springframework.context.ApplicationEvent; public interface ApplicationListener<E extends ApplicationEvent> extends EventListener { void onApplicationEvent(E var1); }
示例程序:
package test.web.listener; import org.apache.logging.log4j.*; import org.springframework.beans.*; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.*; import org.springframework.context.event.ContextRefreshedEvent; import org.springframework.stereotype.Component; import org.springframework.web.context.ServletContextAware; import javax.servlet.ServletContext; @Component public class StartupListener implements ApplicationContextAware, ServletContextAware, InitializingBean, ApplicationListener<ContextRefreshedEvent> { protected Logger logger = LogManager.getLogger(); @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { logger.info("1 => StartupListener.setApplicationContext"); } @Override public void setServletContext(ServletContext context) { logger.info("2 => StartupListener.setServletContext"); } @Override public void afterPropertiesSet() throws Exception { logger.info("3 => StartupListener.afterPropertiesSet"); } @Override public void onApplicationEvent(ContextRefreshedEvent evt) { logger.info("4.1 => MyApplicationListener.onApplicationEvent"); if (evt.getApplicationContext().getParent() == null) { logger.info("4.2 => MyApplicationListener.onApplicationEvent"); } } }
运行时,输出的顺序如下:
1 => StartupListener.setApplicationContext
2 => StartupListener.setServletContext
3 => StartupListener.afterPropertiesSet
4.1 => MyApplicationListener.onApplicationEvent
4.2 => MyApplicationListener.onApplicationEvent
4.1 => MyApplicationListener.onApplicationEvent
注意:onApplicationEvent方法会触发多次,初始化这种事情,越早越好,建议在setApplicationContext方法中处理。
在web服务启动时把从数据库读取到的数据放到ServletContext中
这个是一个项目中的Controller,目的是将数据库中的音乐分类信息在服务器启动的时候读取到ServletContext中框架:spring,spring mvc,mybatis
package com.zhangjinbang.web; import java.util.List; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.context.ServletContextAware; import com.zhangjinbang.dao.MusicTypeMapper; import com.zhangjinbang.model.MusicType; import com.zhangjinbang.model.MusicTypeExample; @Controller @RequestMapping("/") public class SlumberController implements ServletContextAware{ private MusicTypeMapper musicTypeMapper; @Autowired public SlumberController(MusicTypeMapper musicTypeMapper) { super(); this.musicTypeMapper = musicTypeMapper; } @RequestMapping({"","home"}) public String home(){ return "index"; } //加载分类信息 @RequestMapping("start") public String asmr(HttpServletRequest request){ ServletContext application = request.getServletContext(); @SuppressWarnings("unchecked") List<MusicType> musicTypeList=(List<MusicType>)application.getAttribute("musicTypeList"); return "forward:/music/"+musicTypeList.get(0).getId(); } public void setServletContext(ServletContext servletContext) { MusicTypeExample example=new MusicTypeExample(); List<MusicType> musicTypeList=musicTypeMapper.selectByExample(example); servletContext.setAttribute("musicTypeList", musicTypeList); } }
参考
《如何让spring mvc web应用启动时就执行特定处理 》菩提树下的杨过.Net相关文章推荐
- 如何让spring mvc web应用启动时就执行特定处理
- 如何让spring mvc web应用启动时就执行特定处理
- spring mvc web应用启动时执行特定处理
- spring mvc web应用启动时就执行特定处理(线程启动)
- 如何让springmvc在启动的时候执行特定的业务处理
- 如何让springmvc在启动的时候执行特定的业务处理
- 转载:如何让spring mvc web应用启动时就执行
- 通过分析exevc系统调用处理过程来理解Linux内核如何装载和启动一个可执行程序
- 如何在java控制台程序退出时执行特定的处理
- 如何在java控制台程序退出时执行特定的处理
- 如何在java控制台程序退出时执行特定的处理
- Linux内核如何装载和启动一个可执行程序
- 「学习笔记——Linux」Linux下如何在特定时间执行特定任务
- 如何在用户登录时sap时触发一特定程序执行
- 如何清除特定语句的执行计划缓存
- 详解Spring Boot 项目启动时执行特定方法
- 详解如何在 Linux 启动时自动执行命令或脚本
- 如何使用Django 启动命令行及执行脚本
- Spring MVC 是如何处理 JSon webservice
- Linux内核如何装载和启动一个可执行程