Spring-MVC的应用中,要实现类似的功能,主要是通过实现下面这些接口(任选一,至少一个即可)

一、ApplicationContextAware接口

1
2
3
4
5
6
7
8
9
package org.springframework.context;
 
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.Aware;
import org.springframework.context.ApplicationContext;
 
public interface ApplicationContextAware extends Aware {
    void setApplicationContext(ApplicationContext var1) throws BeansException;
}

二、ServletContextAware 接口

1
2
3
4
5
6
7
8
package org.springframework.web.context;
 
import javax.servlet.ServletContext;
import org.springframework.beans.factory.Aware;
 
public interface ServletContextAware extends Aware {
    void setServletContext(ServletContext var1);
}

三、InitializingBean 接口

1
2
3
4
5
package org.springframework.beans.factory;
 
public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}

四、ApplicationListener<ApplicationEvent> 接口

1
2
3
4
5
6
7
8
package org.springframework.context;
 
import java.util.EventListener;
import org.springframework.context.ApplicationEvent;
 
public interface ApplicationListener<E extends ApplicationEvent> extends EventListener {
    void onApplicationEvent(E var1);
}

示例程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package test.web.listener;
 
import org.apache.logging.log4j.*;
import org.springframework.beans.*;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.*;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;
import org.springframework.web.context.ServletContextAware;
import javax.servlet.ServletContext;
 
@Component
public class StartupListener implements ApplicationContextAware, ServletContextAware,
        InitializingBean, ApplicationListener<ContextRefreshedEvent> {
 
    protected Logger logger = LogManager.getLogger();
 
    @Override
    public void setApplicationContext(ApplicationContext ctx) throws BeansException {
        logger.info("1 => StartupListener.setApplicationContext");
    }
 
    @Override
    public void setServletContext(ServletContext context) {
        logger.info("2 => StartupListener.setServletContext");
    }
 
    @Override
    public void afterPropertiesSet() throws Exception {
        logger.info("3 => StartupListener.afterPropertiesSet");
    }
 
    @Override
    public void onApplicationEvent(ContextRefreshedEvent evt) {
        logger.info("4.1 => MyApplicationListener.onApplicationEvent");
        if (evt.getApplicationContext().getParent() == null) {
            logger.info("4.2 => MyApplicationListener.onApplicationEvent");
        }
    }
 
}

运行时,输出的顺序如下:

1 => StartupListener.setApplicationContext
2 => StartupListener.setServletContext
3 => StartupListener.afterPropertiesSet
4.1 => MyApplicationListener.onApplicationEvent
4.2 => MyApplicationListener.onApplicationEvent
4.1 => MyApplicationListener.onApplicationEvent

注意:onApplicationEvent方法会触发多次,初始化这种事情,越早越好,建议在setApplicationContext方法中处理。

最后一步别忘了,在spring-config.xml中加入自定义的bean,如下面的例子:

  1. <!--初始化操作的bean 在spring初始化完成的时候执行这个操作 -->
  2. <bean class="com.**.firm.fclient.web.listener.ApplicationContextListener"/>

此外还有一种方法:

写个BEAN,将要执行的操作写在构造函数里,在将这个类配置到XML里,比如配置到SPRING-MVC.XML里  <bean         class="com.test.xx"> 自己的类

不过不推荐使用这种方法

Spring MVC启动时初始化的几个常用方法的更多相关文章

  1. Spring MVC启动过程(1):ContextLoaderListener初始化

    此文来自https://my.oschina.net/pkpk1234/blog/61971 (写的特别好)故引来借鉴 Spring MVC启动过程 以Tomcat为例,想在Web容器中使用Spirn ...

  2. Spring Boot 2 (七):Spring Boot 如何解决项目启动时初始化资源

    Spring Boot 2 (七):Spring Boot 如何解决项目启动时初始化资源 在项目启动的时候需要做一些初始化的操作,比如初始化线程池,提前加载好加密证书等.今天就给大家介绍一个 Spri ...

  3. SpringBoot 源码解析 (三)----- Spring Boot 精髓:启动时初始化数据

    在我们用 springboot 搭建项目的时候,有时候会碰到在项目启动时初始化一些操作的需求 ,针对这种需求 spring boot为我们提供了以下几种方案供我们选择: ApplicationRunn ...

  4. 1.Spring项目启动时,加载相关初始化配置

    Spring项目启动时,会加载一些常用的配置: 1.加载spring上下文 SpringApplicationContextUtils.initApplicationContext(event.get ...

  5. Docker容器启动时初始化Mysql数据库

    1. 前言 Docker在开发中使用的越来越多了,最近搞了一个Spring Boot应用,为了方便部署将Mysql也放在Docker中运行.那么怎么初始化 SQL脚本以及数据呢? 我这里有两个传统方案 ...

  6. Spring mvc 启动 和 请求分发

    Spring mvc 启动 和 请求分发 启动加载: abstract class HttpServletBean extends HttpServlet void init() initServle ...

  7. Spring MVC之DispatcherServlet初始化详解

    Spring作为一个优秀的web框架,其运行是基于Tomcat的.在我们前面的讲解中,Spring的驱动都是使用的ClassPathXmlApplicationContext,并且都是直接在main方 ...

  8. Spring MVC启动流程分析

    本文是Spring MVC系列博客的第一篇,后续会汇总成贴子. Spring MVC是Spring系列框架中使用频率最高的部分.不管是Spring Boot还是传统的Spring项目,只要是Web项目 ...

  9. spring mvc 启动过程及源码分析

    由于公司开源框架选用的spring+spring mvc + mybatis.使用这些框架,网上都有现成的案例:需要那些配置文件.每种类型的配置文件的节点该如何书写等等.如果只是需要项目能够跑起来,只 ...

随机推荐

  1. JavaScript面向对象之Prototypes和继承

    本文翻译自微软的牛人Scott Allen Prototypes and Inheritance in JavaScript ,本文对到底什么是Prototype和为什么通过Prototype能实现继 ...

  2. Java中异常的捕获顺序(多个catch)

    import java.io.IOException; public class ExceptionTryCatchTest { public void doSomething() throws IO ...

  3. 在Windows Server 2012上安装SharePoint 2010 SP1

    现在很多企业的系统都开始用上了Windows 2012,最近公司需要建立一个门户系统,一开始就想到了微软的SharePoint2010,这玩意确实非常强悍,2008已经褪去,当然直接在Windows ...

  4. BCCoventUtils全角与半角互相转换

    public class BCConvert { /** * ASCII表中可见字符从!开始,偏移位值为33(Decimal) */ static final char DBC_CHAR_START ...

  5. MVC,MVP 和 MVVM 的图示,区别

    作者: 阮一峰 日期: 2015年2月 1日 复杂的软件必须有清晰合理的架构,否则无法开发和维护. MVC(Model-View-Controller)是最常见的软件架构之一,业界有着广泛应用.它本身 ...

  6. language model ——tensorflow 之RNN

    代码结构 tf的代码看多了之后就知道其实官方代码的这个结构并不好: graph的构建和训练部分放在了一个文件中,至少也应该分开成model.py和train.py两个文件,model.py中只有一个P ...

  7. 用工具快速建立hibernate框架

    ,一.建好项目后先导入两类jar包,一类是hibernate的jar包,一类是jdbc的jar包 二.点击“窗口”--“显示视图”--“其它”-“Hibernate configurations” 三 ...

  8. LeetCode OJ:Binary Tree Postorder Traversal(后序遍历二叉树)

    Given a binary tree, return the postorder traversal of its nodes' values. For example:Given binary t ...

  9. mcake活动维护常见问题记录【pc端】 ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★

    ★ ★ ★ ★ ★ ★ ★ ★ ★ ★pc端问题及解决方法 ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ ★ 问题一.pc.弹窗,背景兼容ie8的写法 ;;; -moz-opacity:.7;f ...

  10. dataGridView的使用经验

    1.dataGridView是dataGrid的替代品,包含了dataGrid的全部功能. 2.为dataGridView赋值,一般将其数据设置为一个DataTabel.例子如下: DataTable ...