zoukankan      html  css  js  c++  java
  • 转载:如何让spring mvc web应用启动时就执行

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

    http://www.cnblogs.com/yjmyzz/p/4747251.html#

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

  • 相关阅读:
    assembly 基础
    自定义编写0号内中断除法错误的中断处理程序
    Codeforces Round #573 (Div. 2) D. Tokitsukaze, CSL and Stone Game (博弈,思维)
    Codeforces Round #573 (Div. 2) E. Tokitsukaze and Duel (博弈)
    Schedule HDU
    牛客假日团队赛5 F 随机数 BZOJ 1662: [Usaco2006 Nov]Round Numbers 圆环数 (dfs记忆化搜索的数位DP)
    洛谷 P2866 [USACO06NOV]糟糕的一天Bad Hair Day 牛客假日团队赛5 A (单调栈)
    「BZOJ1669」D 饥饿的牛 [Usaco2006 Oct] Hungry Cows 牛客假日团队赛5 (LIS,离散化树状数组)
    树状数组求LIS模板
    牛客OI周赛11-普及组 B Game with numbers (数学,预处理真因子)
  • 原文地址:https://www.cnblogs.com/kzfy/p/6077906.html
Copyright © 2011-2022 走看看