博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Web系统Spring/SpringMVC启动完成监控
阅读量:6920 次
发布时间:2019-06-27

本文共 1428 字,大约阅读时间需要 4 分钟。

  hot3.png

废话不多说直接贴代码

spring-mvc.xml:

WebStartupListener.java

package com.transino;import com.transino.service.UserService;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.ApplicationListener;import org.springframework.context.event.ContextRefreshedEvent;/** * @author 郏高阳 * @date 2017.8.1 * @jdk.version 1.8 * @desc 系统启动监听器 */public class WebStartupListener implements ApplicationListener
{ private static final Logger logger = LoggerFactory.getLogger(WebStartupListener.class); @Autowired private UserService userService; @Override public void onApplicationEvent(ContextRefreshedEvent contextRefreshedEvent) { if (contextRefreshedEvent.getApplicationContext().getParent() == null) { logger.info("------ 系统启动完成 ------"); logger.info(userService.queryUserByName("王八")); } }}

常遇到的问题无法使用Spring提供的Annotation

applicationontext和使用MVC之后的webApplicationontext会两次调用上面的方法,如何区分这个两种容器呢? 

但是这个时候,会存在一个问题,在web 项目中(spring mvc),系统会存在两个容器,一个是root application context ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器)。 
这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免上面提到的问题,我们可以只在root application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理,修改后代码 
 

if (contextRefreshedEvent.getApplicationContext().getParent() == null) {        }

 

转载于:https://my.oschina.net/jgy/blog/1589260

你可能感兴趣的文章
实验箱FPGA部分测试报告及A8与FPGA链接测试报告
查看>>
数据库与vs的连接
查看>>
Awk实例 第一部分
查看>>
Tftp上传、下载
查看>>
Java的自动装箱与拆箱
查看>>
iOS,图片处理
查看>>
第一个目标——用pygame写俄罗斯方块
查看>>
java入门笔记
查看>>
shell编程技巧和陷阱
查看>>
bzoj 1297 [SCOI2009]迷路
查看>>
手机web——自适应网页设计(html/css控制)
查看>>
多种类型的神经网络(孪生网络)
查看>>
算法17-----判断3的幂
查看>>
实验四+018+李滨
查看>>
02 Java基本运算
查看>>
C#网络编程(接收文件) - Part.5
查看>>
uboot学习之六---uboot命令体系基础
查看>>
luasql使用问题
查看>>
用javascript写一个emoji表情插件
查看>>
Vim正则表达式匹配替换字符串
查看>>