Spring Boot————Web应用启动时自动执行ApplicationListener用法

原文:《web服务启动spring自动执行ApplicationListener的用法》

引言

我们知道,一般来说一个项目启动时需要加载或者执行一些特殊的任务来初始化系统,通常的做法就是用servlet去初始化,但是servlet在使用Spring bean时不能直接注入,还需要在web.xml配置,比较麻烦。今天介绍一下使用spring启动初始化的方法。其实很简单,只需两步就可以了。

在开发时有时候需要在整个应用开始运行时执行一些特定代码,比如初始化环境,准备测试数据、加载一些数据到内存等等。

在spring中可以通过ApplicationListener来实现相关的功能,加载完成后触发contextrefreshedevent事件(上下文件刷新事件)

但是这个时候,会存在一个问题,在web 项目中(spring mvc),系统会存在两个容器,一个是root application context ,另一个就是我们自己的 projectName-servlet  context(作为root application context的子容器)。

这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免上面提到的问题,我们可以只在root application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理,修改后代码。

实现方式

1、实现ApplicationListener接口:

public class Init implements ApplicationListener<ContextRefreshedEvent>{@Overridepublic void onApplicationEvent(ContextRefreshedEvent event) {//root application context 没有parentif(event.getApplicationContext().getDisplayName().equals("RootWebApplicationContext")){//TODO 这里写下将要初始化的内容}}
}

2、将Init类注册到Spring 容器中

@Configuration
public class ListenerConfig {@Beanpublic Init init() {return new Init();}
}

或通过注解方式:

@Component
public class Init implements ApplicationListener<ContextRefreshedEvent> {// 省略内部代码
}

二次调用问题

上面第一步中的实现代码已经通过判断父容器来解决了二次调用的问题,关于二次调用的解释,可以参考下面这篇文章:

《Spring ApplicationListener使用方法及问题》

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/558874.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

LeetCode算法入门- 4Sum -day11

LeetCode算法入门- 4Sum -day11 Given an array nums of n integers and an integer target, are there elements a, b, c, and d in nums such that a b c d target? Find all unique quadruplets in the array which gives the sum of target. Note: The solution se…

Spring Boot————ApplicationListener实现逃课事件监听

引言 上一篇文章转了一篇关于ApplicationListener用于在Web项目启动时做一些初始化的用法。 但是&#xff0c;在实际生产过程中&#xff0c;当一个事件产生&#xff0c;又是如何被onApplicationEvent()方法监听到&#xff0c;并执行一系列动作呢&#xff1f;简单搜索了一下&a…

Java核心篇之Redis--day4

Java核心篇之Redis–day4 Redis有哪些数据结构&#xff1f; 字符串String、字典Hash、列表List、集合Set、有序集合SortedSet。 1.String&#xff1a;字符串&#xff0c;常用命令&#xff1a;get&#xff0c;set&#xff0c;decr&#xff0c;incr&#xff0c;mget&#xff08;查…

软件版本GA、RC、beta等含义

原文《软件版本GA、RC、beta等含义》 GA General Availability&#xff0c;正式发布的版本&#xff0c;官方开始推荐广泛使用&#xff0c;国外有的用GA来表示release版本。 RELEASE 正式发布版&#xff0c;官方推荐使用的版本&#xff0c;有的用GA来表示。比如spring。 Sta…

Java核心篇之泛型--day5

Java核心篇之泛型–day5 泛型是JDK5时引入的一个新特性&#xff0c;泛型提供了编译时类型安全检查的机制&#xff0c;该机制允许程序猿在编译时检测到非法的类型输入。 泛型的本质是参数化类型&#xff0c;也就是说操作的类型被指定为一个参数。 假定我们有一个需求&#xff…

Spring Boot————AOP入门案例及切面优先级设置

看了这篇文章&#xff0c;如果你还是不会用AOP来写程序&#xff0c;请你打我&#xff01;&#xff01; .||| 引言 Spring AOP是一个对AOP原理的一种实现方式&#xff0c;另外还有其他的AOP实现如AspectJ等。 AOP意为面向切面编程&#xff0c;是通过预编译方式和运行期动态代…

Java核心篇之HashMap--day6

Java核心篇之HashMap–day6 HashMap是一种键值对的数据结构&#xff0c;以数组与链表的形式&#xff08;key&#xff1a;value&#xff09;实现&#xff0c;查询性能和添加性能很好。他是通过将key进行hashcode&#xff08;&#xff09;映射函数来找到表中对应的位置。 HashM…

Spring Boot————Spring Data JPA简介

引言 JPA是Java 持久化API的缩写&#xff0c;是一套Java数据持久化的规范&#xff0c; Spring Data Spring Data项目的目的是为了简化构建基于Spring 框架应用的数据访问技术&#xff0c;包括对关系型数据库的访问支持。另外也包含非关系型数据库、Map-Reduce框架、云数据服…

LeetCode算法入门- Valid Parentheses -day11

LeetCode算法入门- Valid Parentheses -day11 题目描述&#xff1a; Given a string containing just the characters ‘(’, ‘)’, ‘{’, ‘}’, ‘[’ and ‘]’, determine if the input string is valid. An input string is valid if: Open brackets must be closed …

Spring Boot————Spring Boot启动流程分析

一、引言 Spring Boot 的启动虽然仅仅是执行了一个main方法&#xff0c;但实际上&#xff0c;运行流程还是比较复杂的&#xff0c;其中包含几个非常重要的事件回调机制。在实际生产开发中&#xff0c;有时候也会利用这些启动流程中的回调机制&#xff0c;做一些项目初始化的工…

LeetCode算法入门- Longest Valid Parentheses -day12

LeetCode算法入门- Longest Valid Parentheses -day12 Given a string containing just the characters ‘(’ and ‘)’, find the length of the longest valid (well-formed) parentheses substring. 题目描述&#xff1a; 题目的意思是给定一个括号序列的字符串&#xff…

Spring Boot————应用启动时的监听机制测试

引言 本文承接前面的《Spring Boot————Spring Boot启动流程分析》&#xff0c;主要测试一下ApplicationContextInitializer、SpringApplicationRunListener、ApplicationRunner、CommandLineRunner这四个接口实现之下的组件是何时在Spring Boot项目启动时创建并执行相关方…

LeetCode算法入门- Longest Common Prefix -day13

LeetCode算法入门- Longest Common Prefix -day13 题目描述&#xff1a; Write a function to find the longest common prefix string amongst an array of strings. If there is no common prefix, return an empty string “”. Example 1: Input: [“flower”,“flow”…

HTMLCSS————块元素与内联元素

一、块元素与div标签 div是一个块元素&#xff0c;块元素会独占一行&#xff0c;无论它的内容有多少&#xff0c;都会独占一整行。 类似的块元素还有&#xff1a;<p>、<h1>、<h2>、<h3> 然而&#xff0c;<div>标签没有任何语义&#xff0c;就…

LeetCode算法入门- Compare Version Numbers -day14

LeetCode算法入门- Compare Version Numbers -day14 题目描述&#xff1a; Compare two version numbers version1 and version2. If version1 > version2 return 1; if version1 < version2 return -1;otherwise return 0. You may assume that the version strings a…

HTMLCSS————CSS常用选择器及优先级

选择器优先级 内联样式&#xff08;1000&#xff09;> id选择器&#xff08;100&#xff09;> 类和伪类选择器&#xff08;10&#xff09; > 元素选择器&#xff08;1&#xff09;>通配 * 选择器&#xff08;0&#xff09;> 继承的样式 一、元素选择器 作用&…

LeetCode算法入门- Merge Two Sorted Lists -day15

LeetCode算法入门- Merge Two Sorted Lists -day15 题目描述&#xff1a; Merge two sorted linked lists and return it as a new list. The new list should be made by splicing together the nodes of the first two lists. Example: Input: 1->2->4, 1->3-&g…

2018年度总结

2018年&#xff0c;已经成为过去式&#xff0c;这360多天依旧过的很快&#xff0c;快到当我手扶键盘回想这一年发生的点点滴滴时&#xff0c;都没有任何感慨。可能我天生是个无感之人&#xff0c;或许&#xff0c;这一年的时光&#xff0c;无数的事故、故事已经让我变得不那么感…

LeetCode算法入门- Generate Parentheses -day16

LeetCode算法入门- Generate Parentheses -day16 题目描述 Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. For example, given n 3, a solution set is: [ “((()))”, “(()())”, “(())()”, “()(())”, …

2019技术学习规划

引言 前段时间总结了一下2018年的大事小情&#xff08;《2018年度总结》&#xff09;&#xff0c;整体来说还是正能量满满&#xff0c;阅读量涨得也是蛮快的。今天&#xff0c;抽出点时间思考了一下未来一年的规划。那作为技术人才&#xff0c;规划也自然都是技术相关的&#…