`
yangyongByJava
  • 浏览: 126523 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

spring框架加载完成后执行上下文刷新事件(ContextRefreshedEvent)

阅读更多

目前spring框架是j2ee比较常用的项目开发技术,只需在web.xml文件中进行少许配置即可,代码如下所示:
<!--spring的配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:config/applicationContext.xml</param-value>
</context-param>
<!-- 启动spring容器的监听器-->
<listener>
<listenerclass>
                org.springframework.web.context.ContextLoaderListener
        </listener-class>
</listener>
spring容器的许多初始化工作就是在ContextLoaderListener中完成,包括初始化applicationContext.xml文件中配置的bean对象、初始化国际化相关的对象(MessageSource)等,当这些步骤执行完后,最后一个就是执行刷新上下文事件,代码为
public void refresh() throws BeansException, IllegalStateException {
synchronized (this.startupShutdownMonitor) {
try {
                ...
               ...
               ...

// Initialize message source for this context.
initMessageSource();
                               
                ...
                ...
                ...

// Last step: publish corresponding event.
finishRefresh();
} catch (BeansException ex) {
// Destroy already created singletons to avoid dangling resources.
destroyBeans();

// Reset 'active' flag.
cancelRefresh(ex);

// Propagate exception to caller.
throw ex;
}
}
}

/**
* Finish the refresh of this context, invoking the LifecycleProcessor's
* onRefresh() method and publishing the
* {@link org.springframework.context.event.ContextRefreshedEvent}.
*/
protected void finishRefresh() {
// Initialize lifecycle processor for this context.
initLifecycleProcessor();

// Propagate refresh to lifecycle processor first.
getLifecycleProcessor().onRefresh();

// Publish the final event.
publishEvent(new ContextRefreshedEvent(this));
}
跟进finishRefresh方法可发现publishEvent(new ContextRefreshedEvent(this));这行代码,此处就是刷新上下文的事件。

public void publishEvent(ApplicationEvent event) {

Assert.notNull(event, "Event must not be null");

if (logger.isTraceEnabled()) {

logger.trace("Publishing event in " + getDisplayName() + ": " + event);

}

getApplicationEventMulticaster().multicastEvent(event);

if (this.parent != null) {

this.parent.publishEvent(event);

}

}

 

SimpleApplicationEventMulticaster.java

@SuppressWarnings("unchecked")

public void multicastEvent(final ApplicationEvent event) {

for (final ApplicationListener listener : getApplicationListeners(event)) {

Executor executor = getTaskExecutor();

if (executor != null) {

executor.execute(new Runnable() {

@SuppressWarnings("unchecked")

public void run() {

listener.onApplicationEvent(event);

}

});

}

else {

listener.onApplicationEvent(event);

}

}

}

开发者可自己定义一个监听器让其实现ApplicationListener接口,覆盖onApplicationEvent方法,在onApplicationEvent方法中完成开发所需的动作,代码如下所示:


@Component
public class SpringContextListener implements ApplicationListener {

public void onApplicationEvent(ApplicationEvent event) {
if(event instanceof ContextRefreshedEvent) { //spring容器启动完成
...
                ...
                ...
}
}
}

 

分享到:
评论
3 楼 1927105 2015-11-13  
yangyongByJava 写道
cfying 写道
请问,可是为什么会执行两边啊?
QQ:342913415

哥们,你那边是啥情况?
我测了一下,只会调一次,下面是我的测试代码:
@Component
public class TestContextLister implements ApplicationListener<ApplicationContextEvent> {

public void onApplicationEvent(ApplicationContextEvent event) {
System.out.println("========================");
}
}



是不是mvc的容器也触发了。。。呵呵
2 楼 yangyongByJava 2015-06-01  
cfying 写道
请问,可是为什么会执行两边啊?
QQ:342913415

哥们,你那边是啥情况?
我测了一下,只会调一次,下面是我的测试代码:
@Component
public class TestContextLister implements ApplicationListener<ApplicationContextEvent> {

public void onApplicationEvent(ApplicationContextEvent event) {
System.out.println("========================");
}
}
1 楼 cfying 2015-03-21  
请问,可是为什么会执行两边啊?
QQ:342913415

相关推荐

    详解spring boot容器加载完后执行特定操作

    主要介绍了详解spring boot容器加载完后执行特定操作,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

    Spring框架.ppt

    向Spring 框架提供上下文信息,包括企业服务,如 JNDI、EJB、电子邮件、国际化、校验和调度等。 Spring AOP。通过配置管理特性,可以很容易地使 Spring 框架管理的任何对象支持 AOP。Spring AOP 模块直接将面向方面...

    spring框架,技术详解及使用指导

    spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用指导spring框架,技术详解及使用...

    Spring框架的简单实现

    我们从一个简单的容器开始,一步步的重构,最后实现一个基本的Spring框架的雏形,为了帮助我们更加深入的理解Spring的IoC的原理和源码。 详细内容见博文: 【SSH进阶之路】一步步重构容器实现Spring框架——从一个...

    Spring框架生态流程框架图-执行运行路程图

    执行Spring框架的运行路程通常包括以下几个重要的步骤: 应用程序启动:应用程序启动时,Spring框架会加载并初始化所需的配置文件和类。 配置加载:Spring框架会读取并加载应用程序的配置文件,例如XML配置文件或...

    利用Spring Context上下文创建自定义对象

    利用Spring Context上下文创建自定义对象,学习Spring的IoC容器如何使用控制反转创建一个对象,了解Spring控制反转原理

    Spring动态加载配置文件

    本文档是很专业的 Spring动态加载配置文件,实现热部署方案。

    Spring框架文档 - 核心技术部分 中文版 - 第一部分.pdf

    Spring框架(Spring Framework)有自己的AOP框架,在概念上很容易理解,它成功地解决了Java企业编程中80%的AOP需求。 本文档也讲解Spring与AspectJ的集成,就特征而言,AspectJ是Java企业空间中目前最丰富的、最成熟...

    Spring框架高级编程完整源码

    而且揭示了Spring完成这些功能的原理,解释其功能和动机,以帮助读者使用该框架的所有部分来开发成功的应用程序。本书涵盖Spring的所有特性,并且演示了如何将其构成一个连贯的整体,帮助读者理解Spring方法的基本...

    Spring.Batch批处理框架

    Spring Batch 是一个批处理应用框架,不是调度框架,但需要和调度框架合作来构建完成的批处理任务。它只关注批处理任务相关的问题,如事务、并发、监控、执行等,并不提供相应的调度功能。如果需要使用调用框架,在商业...

    Spring框架开发参考中文手册

    一本很好的快速学习Spring框架开发的指导手册,中文、PDF排版格式。

    spring框架所需要加载库合集

    学习spring框架所需要加载的库合集,commons-logging,log4j,spring

    spring框架帮助文档

    spring框架帮助文档 能够帮助您解决spring框架里的一些难题

    Spring框架文档 - 核心技术部分 中英双语版 第二部分.pdf

    Spring框架(Spring Framework)有自己的AOP框架,在概念上很容易理解,它成功地解决了Java企业编程中80%的AOP需求。 本文档也讲解Spring与AspectJ的集成,就特征而言,AspectJ是Java企业空间中目前最丰富的、最成熟...

    spring框架外文文献.docx

    毕业论文外文文献+中文翻译,选取spring框架外文文献并进行相应翻译。 The Spring Framework evolved from the code written for Expert One-on-One J2EE Design and Development by Rod Johnson (Wrox, 2002). The ...

    android的spring框架

    android的spring框架

    传智博客Spring框架2016版笔记资料.zip

    传智博客Spring框架2016版笔记资料 传智博客Spring框架2016版笔记资料 传智博客Spring框架2016版笔记资料 传智博客Spring框架2016版笔记资料 传智博客Spring框架2016版笔记资料

    详细介绍Spring框架

    详细介绍了spring框架的核心内容以及怎么配置框架

    spring框架2016

    最新spring框架2016视频教程,包含源码和笔记。小白入手实用教程。

    spring框架教程 PPT

    spring框架教程 PPT ppt

Global site tag (gtag.js) - Google Analytics