如何让spring mvc web应用启动时就执行特定处理

Asp.Net的应用中通过根目录下的Global.asax,在Application_Start方法中做一些初始化操作,比如:预先加载缓存项对网站热点数据进行预热,获取一些远程的配置信息等等。

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

一、ApplicationContextAware接口

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 接口

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 接口

package org.springframework.beans.factory;

public interface InitializingBean {
    void afterPropertiesSet() throws Exception;
}

四、ApplicationListener<ApplicationEvent> 接口

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);
}

示例程序:

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方法中处理。

时间: 2024-08-24 18:21:00

如何让spring mvc web应用启动时就执行特定处理的相关文章

spring mvc web应用启动时就执行特定处理(线程启动)

package com.sdt.platform.index.controller; import java.net.URL; import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; import javax.annotation.Resource; import org.springframework.beans.BeansException; import org.sprin

web项目启动时,执行某个方法

1.监听(Listener) web文件添加 <listener> <listener-class>cn.ro.common.InitListener</listener-class> </listener> 添加InitListener类,如下 package cn.ro.common; import javax.servlet.ServletContextEvent; import javax.servlet.ServletContextListener

在web项目启动时,执行某个方法

在web项目中有很多时候需要在项目启动时就执行一些方法,而且只需要执行一次,比如:加载解析自定义的配置文件.初始化数据库信息等等,在项目启动时就直接执行一些方法,可以减少很多繁琐的操作. 在工作中遇到了项目初始数据需要跟其他项目同步的问题,也就是说在项目部署后,启动的时候就要同步另外一个项目的数据,这里写了个简单的实例,用的是监听器机制,创建一个类实现ServletContextListener 接口,实现里面的contextInitialized和contextDestroyed方法. pac

基于spring的web项目启动时预加载数据到ServletContext

1.要在web启动时预加载数据到ServletContext,实现方法有很多,一种比较简单的方案就是: 1)新建一个bean,定义其初始化方法: <bean id="beanId" init-method="初始化方法" />或者使用@PostConstruct注解到初始化方法上面 2)获取ServletContext实例对象,如何获取呢? 方法1: @Autowired private ServletContext application; 方法2:

spring在web容器启动时执行初始化方法

需求:在tomcat启动时开启一个定时任务. 想法:容器启动时执行方法,最容易想到的就是servlet中可以配置load-on-startup,设置一个正整数也就可以随容器一起启动. 问题:上面的方法很好,但是由于定时任务需要去操作数据库,而项目采用了spring的依赖注入来管理对象,而servlet并不受Spring的管理.若此时在servlet中注入Spring管理的对象,则会报错:javax.naming.NameNotFoundException: Name com.test.InitS

web项目启动时,自动执行代码

tomcat启动时自动执行,以下两种方法的执行时长,会计算在tomcat的启动时长里. 1.ServletContextListener web.xml配置<listener> <listener-class>com.yuan.framework.GreyClientInitListener</listener-class> </listener> 1 public class GreyClientInitListener implements Servle

Spring学习(二)——使用用Gradle构建一个简单的Spring MVC Web应用程序

1.新建一个Gradle工程(Project) 在新建工程窗口的左侧中选择 [Gradle],右侧保持默认选择,点击next,模块命名为VelocityDemo. 2.在该工程下新建一个 module,在弹出的窗口的左侧中选择 [Gradle],右侧勾选[Spring MVC],如下图所示: 并勾选[Application server],下方选择框中选择Tomcat7.0,如无该选项,则选中右边的 [ New... ] -- [ Tomcat Server ], 配置 Tomcat .配置好后

采用Spring实现在容器启动时把用ConcurrentHashMap实现的并发缓存加载到ServletContext中

1.ConstantDataDTO.java,一定要重写hashcode和equals方法 import java.io.Serializable; import java.util.Date; /** * ConstantDataDTO.java * * @author steveguoshao * @created 2014年07月10日 下午6:15:55 * @version 1.0 */ public class ConstantDataDTO implements Serializa

Spring MVC返回json视图时,如何将对象直接序列化成不带变量名做为根节点

Spring MVC返回json视图时,如何将对象直接序列化成不带变量名做为根节点的 json 报文 问题 问题描述起来比较拗口,其实就是用Spring MVC时,如何将对象映射成 json 报文时不把对象作为json的根节点.即使用@ResponseBody的效果. 比如,默认情况下,使用ModelAndView的addObject(key,object)或者ModelMap的addAttribute(key,object)保存完Java对象,然后交给Srping的视图解析器解析成json时,