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,如下面的例子:

   <!--初始化操作的bean 在spring初始化完成的时候执行这个操作 -->
<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. Java中如何指定跳出多重嵌套循环

    今天做项目优化涉及到一个跳出指定多重嵌套循环的问题,觉得还是记录一下那么在Java中如何跳出当前的多重嵌套循环? 方法一:可以在需要的循环语句前定义一个标号,然后在里层循环体的代码中使用带有标号的br ...

  2. pandas 数据处理

    1. 查看数值数据的整体分布情况 datafram.describe() 输出: agecount 1463.000000mean 22.948052std 8.385384min 13.000000 ...

  3. Load事件

    Load事件 在 窗体完全呈现之后 被 触发 如下伪代码: void  ShowWindows { .....//显示父容器 .....//显示子容器 .....//显示控件 //至此,窗体完全呈现 ...

  4. Ubuntu 配置静态ip的方法

    1. 配置静态ip地址 $sudo vi /etc/network/interfaces 原有内容只有如下两行: auto lo iface lo inet loopback 向末尾追加以下内容: a ...

  5. 分享知识-快乐自己:FastDFS详解

    在使用fdfs之前,需要对其有一定的了解,这篇文章作为准备篇,将针对fdfs的简介,功能性,使用场景等方面进行介绍 一):起源 淘宝网开放平台技术部资深架构师余庆先生首先回顾了自己在Yahoo工作时的 ...

  6. react 总结

    1.React 里直接修改 this.state 和调用 setState() 修改 state 的值有什么区别? 使用对this.state赋值并没有什么作用,官方提醒,应该把this.state当 ...

  7. nt":false,"tarball":"http://registry.npm.taobao.org/babel-preset-stag

    npm ERR! Unexpected end of input at 1:12777 npm ERR! nt":false,"tarball":"http:/ ...

  8. 数据库oracle(PGA+SGA分配机制)

    一.名词解释 (1)SGA:System Global Area是Oracle Instance的基本组成部分,在实例启动时分配;系统全局域SGA主要由三部分构成:共享池.数据缓冲区.日志缓冲区. ( ...

  9. SVN 的搭建及使用(二)VisualSVN Server建立版本库,以及VisualSVN和TortoiseSVN的使用

    上一篇介绍了VisualSVN Server和TortoiseSVN的下载,安装,汉化.这篇介绍一下如何使用VisualSVN Server建立版本库,以及VisualSVN和TortoiseSVN的 ...

  10. C#当中利用Attribute实现简易AOP

    首先看一段简单的代码: public partial class Form1 : Form { public Form1() { InitializeComponent(); } //来自UI层的调用 ...