在某些情况下,有可能你会有这种需求:在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在启动完成后执行方法的更多相关文章

  1. 当springMVC 容器初始化完成后执行某个方法

    分类: spring java2013-06-19 16:40 8289人阅读 评论(4) 收藏 举报 在某些应用中,我们希望,当spring 容器将所有的bean都初始化完成后,做一个操作(例如:将 ...

  2. springboot项目启动成功后执行一段代码的两种方式

    springboot项目启动成功后执行一段代码的两种方式 实现ApplicationRunner接口 package com.lnjecit.lifecycle; import org.springf ...

  3. 如何让springmvc在启动的时候执行特定的业务处理

    如何让springmvc在启动的时候执行特定的业务处理 java 的 web服务器启动时,经常会做一些特定的业务逻辑处理,比如数据库初始化, 初始化系统参数,读取配置文库等. 很多web服务的中间件, ...

  4. (一)在Spring Boot应用启动之后立刻执行一段逻辑

    在Spring Boot应用启动之后立刻执行一段逻辑 1.CommandLineRunner 2.ApplicationRunner 3.传递参数 码农小胖哥:如何在Spring Boot应用启动之后 ...

  5. spring boot启动后执行方法

    @Componentpublic class InitProject implements ApplicationRunner { private static final Logger logger ...

  6. Springboot - 在启动完成后执行特定方法

    1.实现方式 实现ApplicationRunner接口 实现CommandLineRunner接口 @Component @Slf4j public class AfterServiceStarte ...

  7. 利用spring实现服务启动就自动执行某些操作的2种方式

    第一种方式,用bean的init-method属性 <bean class="com.emax.paycenter.log.LogBridge" init-method=&q ...

  8. 如何在Spring Boot应用启动之后立刻执行一段逻辑

    1. 前言 不知道你有没有接到这种需求,项目启动后立马执行一些逻辑.比如简单的缓存预热,或者上线后的广播之类等等.如果你使用 Spring Boot 框架的话就可以借助其提供的接口CommandLin ...

  9. spring 容器加载完成后执行某个方法

    理论 刚好再开发过程中遇到了要在项目启动后自动开启某个服务,由于使用了spring,我在使用了spring的listener,它有onApplicationEvent()方法,在Spring容器将所有 ...

随机推荐

  1. 【模拟】bzoj2295 【POJ Challenge】我爱你啊

    #include<cstdio> #include<cstring> using namespace std; int n; char s[100001],table[]=&q ...

  2. 【权值分块】bzoj1861 [Zjoi2006]Book 书架

    权值分块……rank3……没什么好说的. #include<cstdio> #include<cmath> #include<algorithm> using na ...

  3. [POI2010]Frog

    题目大意: 一个数轴上有n个点,现在你要在这些点上跳. 每次跳的时候你只能跳到离这个点第k近的点上,而且要连续跳m次. 问从每一个点出发,最后分别会在哪一个点结束. 思路: 首先可以维护一个大小为k+ ...

  4. PHP计算字符串长度,PHP如何计算短信的长度/字数?

    PHP计算字符串长度,包括计算英文.GBK.UTF-8多种字符集下PHP如何计算字符串长度. 英文字符串长度,strlen()是PHP自带的计算英文字符串的函数. GBK字符串长度 中文字符计算为2个 ...

  5. touch: cannot touch ‘/var/jenkins_home/copy_reference_file.log’: Permission denied Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions?

    问题:在从 https://c.163.com/hub#/m/repository/?repoId=3093 下载镜像 docker pull hub.c.163.com/library/jenkin ...

  6. iOS:地图笔记

    地图笔记 01. CLLocation -------------------------------------------------------- CLLocationManager 定位管理者 ...

  7. http://www.360doc.com/content/12/0516/14/1671317_211422841.shtml

    http://www.360doc.com/content/12/0516/14/1671317_211422841.shtml

  8. js 正则只允许小写字母、数字、点、中短划线

    正则表达式如下: /^[a-z0-9\.-]*$/g 可用如下语句验证: alert(/^[a-z0-9\.-]*$/g.test('abc123.45a-b')); //true alert(/^[ ...

  9. Solr6.6.0 用 SimplePostTool索引文件 中文乱码

    在用SimplePostTool工具导入CSV文件,文件内容如下: 启动solr ,利用命令导入:java -Dtype=text/csv -Dc=solr_test -jar post.jar .. ...

  10. RHEL7.1 安装openstack juno 一个BUG

    错误提示: -- :: ERROR nova.compute.manager [-] [instance: 887e5e40-ebd8--b2f7-afa2a37bdef8] Instance fai ...