Spring/SpringMVC在启动完成后执行方法
在某些情况下,有可能你会有这种需求:在Spring/SpringMVC项目中,当Spring/SpringMVC启动完成后,你需要执行一个方法来完成某些事件(比如创建网站地图,比如从订阅Redis服务器等),这个时候,可以使用Tomcat/Servlet容器提供的事件回调机制来完成,但是这样有个问题是:无法使用Spring提供的Annotation,解决方法是:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package com.storezhang.web; import com.storezhang.util.TimeUtils;
import com.storezhang.video.util.SiteMapUtils;
import java.util.Timer;
import java.util.TimerTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Service; /**
* 启动监听器
*
* @author Storezhang
*/
@Service
public class StartupListener implements ApplicationListener<ContextRefreshedEvent> { @Autowired
private SiteMapUtils sites; @Override
public void onApplicationEvent(ContextRefreshedEvent evt) {
if (evt.getApplicationContext().getParent() == null) {
createSitemap();
}
} private void createSitemap() {
Timer timer = new Timer("createSitemap", true);
timer.schedule(new TimerTask() {
@Override
public void run() {
System.out.println("--->Create sitemap...");
sites.createSiteMap();
System.out.println("--->Success create sitemap...");
}
}, 1 * TimeUtils.MIN);
}
}
后续研究:
applicationontext和使用MVC之后的webApplicationontext会两次调用上面的方法,如何区分这个两种容器呢?
但是这个时候,会存在一个问题,在web 项目中(spring mvc),系统会存在两个容器,一个是root application context ,另一个就是我们自己的 projectName-servlet context(作为root application context的子容器)。
这种情况下,就会造成onApplicationEvent方法被执行两次。为了避免上面提到的问题,我们可以只在root application context初始化完成后调用逻辑代码,其他的容器的初始化完成,则不做任何处理,修改后代码
如下:
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if(event.getApplicationContext().getParent() == null){//root application context 没有parent,他就是老大.
//需要执行的逻辑代码,当spring容器初始化完成后就会执行该方法。
}
}
Spring/SpringMVC在启动完成后执行方法的更多相关文章
- 当springMVC 容器初始化完成后执行某个方法
分类: spring java2013-06-19 16:40 8289人阅读 评论(4) 收藏 举报 在某些应用中,我们希望,当spring 容器将所有的bean都初始化完成后,做一个操作(例如:将 ...
- springboot项目启动成功后执行一段代码的两种方式
springboot项目启动成功后执行一段代码的两种方式 实现ApplicationRunner接口 package com.lnjecit.lifecycle; import org.springf ...
- 如何让springmvc在启动的时候执行特定的业务处理
如何让springmvc在启动的时候执行特定的业务处理 java 的 web服务器启动时,经常会做一些特定的业务逻辑处理,比如数据库初始化, 初始化系统参数,读取配置文库等. 很多web服务的中间件, ...
- (一)在Spring Boot应用启动之后立刻执行一段逻辑
在Spring Boot应用启动之后立刻执行一段逻辑 1.CommandLineRunner 2.ApplicationRunner 3.传递参数 码农小胖哥:如何在Spring Boot应用启动之后 ...
- spring boot启动后执行方法
@Componentpublic class InitProject implements ApplicationRunner { private static final Logger logger ...
- Springboot - 在启动完成后执行特定方法
1.实现方式 实现ApplicationRunner接口 实现CommandLineRunner接口 @Component @Slf4j public class AfterServiceStarte ...
- 利用spring实现服务启动就自动执行某些操作的2种方式
第一种方式,用bean的init-method属性 <bean class="com.emax.paycenter.log.LogBridge" init-method=&q ...
- 如何在Spring Boot应用启动之后立刻执行一段逻辑
1. 前言 不知道你有没有接到这种需求,项目启动后立马执行一些逻辑.比如简单的缓存预热,或者上线后的广播之类等等.如果你使用 Spring Boot 框架的话就可以借助其提供的接口CommandLin ...
- spring 容器加载完成后执行某个方法
理论 刚好再开发过程中遇到了要在项目启动后自动开启某个服务,由于使用了spring,我在使用了spring的listener,它有onApplicationEvent()方法,在Spring容器将所有 ...
随机推荐
- yii2.0权限控制 ACF权限
ACF是一种通过yii\filters\AccessControl类来实现的简单授权 有两种角色 ?:未经认证的游客用户 @:已认证的用户 ACF可同过对角色设置权限控制访问 1)记得引入yii\fi ...
- 每天一个linux命令12之top
top命令是Linux下常用的性能分析工具,能够实时显示系统中各个进程的资源占用状况,类似于Windows的任务管理器.下面详细介绍它的使用方法.top是一个动态显示过程,即可以通过用户按键来不断刷新 ...
- 关于scanf函数的返回值问题
如: scanf("%d%d",&a,&b); 1.如果a和b都被成功读入,则scanf的返回值为2 2.如果只有a被成功读入,那么返回值为1 3.如果a和b都未被 ...
- maven-pom-properties
出处: http://blog.csdn.net/taiyangdao/article/details/52358083
- Android 架构 3.实现
以实现最小化可用产品(MVP)的目标,用最简单的方式来搭建架构和实现代码.IDE采用Android Studio,Demo实现的功能为用户注册.登录和展示一个券列表,数据采用我们现有项目的测试数据,接 ...
- 使用gettext提取c#中的多语言占位符(nopCommerce示例篇)
i18n国际化通常的作法是使用gettext,即在源码中使用特殊的关键字来标识这个字符串将可能被翻译,如 @if (Model.IsCustomerForumModerator) { <li c ...
- 【mybatis】mybatis 中update 更新操作,null字段不更新,有值才更新
示例代码如下: <update id="updateGoodsConfigQuery" parameterType="com.pisen.cloud.luna.ms ...
- ASP.NET MVC DropdownList的使用
1:直接使用HTML代码写 <select name="year"> <option value="2011">2010</opt ...
- Scut游戏服务器免费开源框架--快速开发(3)
Scut快速开发(3) 1 开发环境 Scut Lib版本:5.2.3.2 需要安装的软件 a) IIS和消息队列(MSMQ) b) 数据库,Sql2005以上版本 ...
- python3用pyqt5开发简易浏览器
http://python.jobbole.com/82715/ 在这篇教程中,我们会用 Python 的 PyQt 框架编写一个简单的 web 浏览器.关于 PyQt ,你可能已经有所耳闻了,它是 ...