如何让spring mvc web应用启动时就执行特定处理
来源:互联网 发布:勇敢一点 薛之谦 知乎 编辑:程序博客网 时间:2024/05/24 15:36
一、Web项目,非Spring
1.需求:很多时候,在一个 web 项目启动的时候,我们都要【初始化很多系统参数,比如读取配置文件】,或者初始化数据库表...
2.解决方法:实现【 ServletContextListener】 接口
2.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>
2.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("系统初始化结束...");
- }
- }
作者:菩提树下的杨过
出处:http://yjmyzz.cnblogs.com
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
Asp.Net的应用中通过根目录下的Global.asax,在Application_Start方法中做一些初始化操作,比如:预先加载缓存项对网站热点数据进行预热,获取一些远程的配置信息等等。
二、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;
}
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);
}
3、InitializingBean 接口
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
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方法中处理。
- 如何让spring mvc web应用启动时就执行特定处理
- spring mvc web应用启动时就执行特定处理(线程启动)
- spring mvc web应用启动时执行特定处理
- Spring MVC让Web容器启动时自动执行代码
- 如何让springmvc在启动的时候执行特定的业务处理
- spring mvc web应用异常处理
- Spring Boot学习--项目启动时执行特定方法
- spring启动完成后执行特定代码
- spring启动自动后执行特定方法
- Tomcat web应用启动时执行代码
- 如何让对话框在一启动时就最大化?
- Spring MVC Web应用上下文
- [spring mvc]tomcat生成处理线程执行spring mvc时的思考
- [JAVA]如何让tomcat启动时执行某一方法
- spring集成mvc时web工程配置文件的启动顺序
- Spring MVC +Web 的执行过程
- spring项目停止时执行特定代码
- web项目启动时,配置达到启动项目就可以执行任务的目的....
- NopCommerce数据迁移之【开启迁移功能】
- MM模块中常用到的透明表
- OpenStack设计与实现(三)KVM和QEMU浅析
- Java包装类、自动装箱与拆箱知识总结
- ADS中debug,debugrel,release三个版本的区别
- 如何让spring mvc web应用启动时就执行特定处理
- 初次接触分块思想
- 删除windows下过长路径的目录
- 一个简易的具有点击方法的UIImageView的封装
- 让那些白骨,别忘了回家
- Android四大基本组件介绍与生命周期
- 在信号处理函数中调用非可重入函数getpwnam
- 避免缓冲区溢出攻击的方法
- gradle DSL method not found: android()