如何让springmvc在启动的时候执行特定的业务处理
2017-10-04 13:54
363 查看
如何让springmvc在启动的时候执行特定的业务处理
java 的 web服务器启动时,经常会做一些特定的业务逻辑处理,比如数据库初始化,
初始化系统参数,读取配置文库等。
很多web服务的中间件,可以 通过这样的思路去实现。比如消息分发服务。
实现方法:
一、Web项目,非Spring
解决方法:实现【 ServletContextListener】 接口
(1)、把实现了ServletContextListener 的类配置到【 web.xml】 文件中
(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("系统初始化结束...");
}
}
二、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);
}
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");
}
}
}
运行时,输出的顺序如下:
1 => StartupListener.setApplicationContext
2 => StartupListener.setServletContext
3 => StartupListener.afterPropertiesSet
4.1 => MyApplicationListener.onApplicationEvent
4.2 => MyApplicationListener.onApplicationEvent
4.1 => MyApplicationListener.onApplicationEvent
注意:onApplicationEvent方法会触发多次,初始化这种事情,越早越好,建议在setApplicationContext方法中处理。
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>
(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("系统初始化结束...");
}
}
二、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);
}
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");
}
}
}
运行时,输出的顺序如下:
1 => StartupListener.setApplicationContext
2 => StartupListener.setServletContext
3 => StartupListener.afterPropertiesSet
4.1 => MyApplicationListener.onApplicationEvent
4.2 => MyApplicationListener.onApplicationEvent
4.1 => MyApplicationListener.onApplicationEvent
注意:onApplicationEvent方法会触发多次,初始化这种事情,越早越好,建议在setApplicationContext方法中处理。
相关文章推荐
- 如何让springmvc在启动的时候执行特定的业务处理
- 如何让spring mvc web应用启动时就执行特定处理
- 如何在项目启动的时候做一些业务处理 (实现ServletContextListener)
- 如何让spring mvc web应用启动时就执行特定处理
- 如何让spring mvc web应用启动时就执行特定处理
- 如何在java控制台程序退出时执行特定的处理
- 如何让未处理到的异常发生的时候,应用程序仍然能够继续执行?
- SpringMVC中如何在网站启动、结束时执行代码(详细,确保可用)
- 如何在java控制台程序退出时执行特定的处理
- 通过分析exevc系统调用处理过程来理解Linux内核如何装载和启动一个可执行程序
- 在springboot中,如何做到当项目启动的时候去执行这个逻辑
- spring mvc web应用启动时就执行特定处理(线程启动)
- 如何在java控制台程序退出时执行特定的处理
- spring mvc web应用启动时执行特定处理
- 该如何处理,启动任务计划的时候有“任务计划程序服务不可用。任务计划程序将尝试重新与其建立连接。”
- 如何在Linux启动的时候执行一个命令
- 【j2ee系列】springmvc中使用quartz,项目启动就执行某些任务
- Android init.rc如何启动service去执行sh脚本
- 如何在仅仅启动HDFS情况下处理HDFS中的文件
- Java中在特定区间产生随机数 (2009-03-26 13:31:49)转载▼ 标签: 杂谈 生成指定范围内的随机数 这个是最常用的技术之一。程序员希望通过随机数的方式来处理众多的业务逻辑,测试