如何让springmvc在启动的时候执行特定的业务处理
如何让springmvc在启动的时候执行特定的业务处理
java 的 web服务器启动时,经常会做一些特定的业务逻辑处理,比如数据库初始化,
初始化系统参数,读取配置文库等。
很多web服务的中间件,可以 通过这样的思路去实现。比如消息分发服务。
实现方法:
一、Web项目,非Spring
解决方法:实现【 ServletContextListener】 接口
(1)、把实现了ServletContextListener 的类配置到【 web.xml】 文件中
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <display-name></display-name>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- <listener>
- <listener-class>com.chinaso.init.StartInit</listener-class>
- </listener>
- <filter>
- <filter-name>struts2</filter-name>
- <filter-class>
- org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
- </filter-class>
- </filter>
- <filter-mapping>
- <filter-name>struts2</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<listener>
<listener-class>com.chinaso.init.StartInit</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
(2)、加入自己的实现逻辑
- public class StartInit implements ServletContextListener {
- static final Logger logger = LoggerFactory.getLogger(StartInit.class);
- // 系统初始化执行方法
- public void contextDestroyed(ServletContextEvent e) {
- logger.info("系统停止...");
- }
- public void contextInitialized(ServletContextEvent e) {
- logger.info("系统初始化开始...");
- // 获取项目根目录
- String root_path = e.getServletContext().getRealPath("/");
- logger.info("application path : {}",root_path);
- // 初始化 ConfigFactorty
- ConfigFactory.init(root_path);
- // 初始化数据链接信息
- DBManager.init();
- // 初始化定时统计任务
- TaskManager.init();
- // 初始化用户信息查询位置
- UserInfo.init();
- logger.info("系统初始化结束...");
- }
- }
public class StartInit implements ServletContextListener {
static final Logger logger = LoggerFactory.getLogger(StartInit.class);
// 系统初始化执行方法
public void contextDestroyed(ServletContextEvent e) {
logger.info("系统停止...");
} public void contextInitialized(ServletContextEvent e) {
logger.info("系统初始化开始..."); // 获取项目根目录
String root_path = e.getServletContext().getRealPath("/");
logger.info("application path : {}",root_path); // 初始化 ConfigFactorty
ConfigFactory.init(root_path);
// 初始化数据链接信息
DBManager.init();
// 初始化定时统计任务
TaskManager.init();
// 初始化用户信息查询位置
UserInfo.init(); logger.info("系统初始化结束...");
} }
二、Spring项目
Spring-MVC的应用中,要实现类似的功能,主要是通过实现下面这些接口(任选一,至少一个即可)
1、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;
- }
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;
}
2、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);
- }
package org.springframework.web.context; import javax.servlet.ServletContext;
import org.springframework.beans.factory.Aware; public interface ServletContextAware extends Aware {
void setServletContext(ServletContext var1);
}
3、InitializingBean 接口
- package org.springframework.beans.factory;
- public interface InitializingBean {
- void afterPropertiesSet() throws Exception;
- }
package org.springframework.beans.factory; public interface InitializingBean {
void afterPropertiesSet() throws Exception;
}
4、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 org.springframework.context; import java.util.EventListener;
import org.springframework.context.ApplicationEvent; public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
void onApplicationEvent(E var1);
}
java代码:
- 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");
- }
- }
- }
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方法中处理。
如何让springmvc在启动的时候执行特定的业务处理的更多相关文章
- springboot 学习之路 9 (项目启动后就执行特定方法)
目录:[持续更新.....] spring 部分常用注解 spring boot 学习之路1(简单入门) spring boot 学习之路2(注解介绍) spring boot 学习之路3( 集成my ...
- spring mvc web应用启动时就执行特定处理(线程启动)
package com.sdt.platform.index.controller; import java.net.URL; import java.util.List; import java.u ...
- Spring/SpringMVC在启动完成后执行方法
在某些情况下,有可能你会有这种需求:在Spring/SpringMVC项目中,当Spring/SpringMVC启动完成后,你需要执行一个方法来完成某些事件(比如创建网站地图,比如从订阅Redis服务 ...
- Springboot - 在启动完成后执行特定方法
1.实现方式 实现ApplicationRunner接口 实现CommandLineRunner接口 @Component @Slf4j public class AfterServiceStarte ...
- 如何让spring mvc web应用启动时就执行特定处理
Asp.Net的应用中通过根目录下的Global.asax,在Application_Start方法中做一些初始化操作,比如:预先加载缓存项对网站热点数据进行预热,获取一些远程的配置信息等等. Spr ...
- 转载:如何让spring mvc web应用启动时就执行
转载:如何让spring mvc web应用启动时就执行特定处理 http://www.cnblogs.com/yjmyzz/p/4747251.html# Spring-MVC的应用中 一.Appl ...
- springmvc拦截器入门及其执行顺序源码分析
springmvc拦截器是偶尔会用到的一个功能,本案例来演示一个较简单的springmvc拦截器的使用,并通过源码来分析拦截器的执行顺序的控制.具体操作步骤为:1.maven项目引入spring依赖2 ...
- redhat的启动方式和执行次序
rc.d的内容如下: init.d/ :各种服务器和程序的二进制文件存放目录. rcx.d/: 各个启动级别的执行程序连接目录.里头的东西都是指向init.d/的一些软连接.具体的后边叙述. 还有三个 ...
- java中服务器启动时,执行定时任务
package com.ripsoft.util; import java.util.Calendar; import java.util.Timer; import javax.servlet.Se ...
随机推荐
- phpstudy mysql无法启动
在安装好phpstudy后,Apache可以启动,Mysql无法启动. 以管理员的身份运行cmd,然后输入sc delete mysql 即可
- Bootstrap组件系列之福利篇几款好用的组件(推荐)
引用 :http://www.jb51.net/article/87189.htm 一.时间组件 bootstrap风格的时间组件非常多,你可以在github上面随便搜索“datepicker”关键字 ...
- Memcached: 目录
ylbtech-Memcached: 目录 1.返回顶部 2.返回顶部 3.返回顶部 4.返回顶部 5.返回顶部 6.返回顶部 7.返回顶部 8.返回顶部 9.返回 ...
- 第七周作业——简单FTP
开发简单的FTP: 1. 用户登陆 2. 上传/下载文件 3. 不同用户家目录不同 4. 查看当前目录下文件 5. 充分使用面向对象知识 1.目录结构zuoye-ftp├── chenliang #用 ...
- 第七章 伪分布式安装hive,sqoop
第一部分:先讲这么去安装hive.先去hive官网下载,我这里以hive-0.12.0为例子. 前面第二章讲了安装hadoop,hbase实例,我们继续讲这么安装hive,先说下hive配置文件 一, ...
- asterisk配置SIP服务器
服务器asterisk我们安装完以后,需要在windows(就是自己的本机)上在安装一个客户端xlite,直接下载安装就行,安装完以后,我们就需要自己手动配置一下sip服务器了. 下面给大家讲一下怎么 ...
- Error 20002 (severity 9):Adaptive Server connection failed
环境: Ubuntu12.10_x64 问题: 用tsql访问SQL Server >> tsql -H U sa Error (severity ): Adaptive Server c ...
- 使用jQuery Pagination Plugin实现分页效果
最近使用分页这个基础效果较为频繁,而项目前端页面使用的是纯静态的HTML,自己之前写的JSP中的分页就用不成了:项目中也引入了Bootstrap,本来想使用Bootstrap中的分页样式,但发现其样式 ...
- [linux] ping服务器脚本
#!/bin/bash IP=1.2.3.4 while true; do echo "**********************************************" ...
- 3_python之路之商城购物车
python之路之商城购物车 1.程序说明:Readme.txt 1.程序文件:storeapp_new.py userinfo.py 2.程序文件说明:storeapp_new.py-主程序 use ...