zoukankan      html  css  js  c++  java
  • Spring MVC启动时初始化的几个常用方法

    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"> 自己的类 

    不过不推荐使用这种方法

  • 相关阅读:
    025、MySQL字符串大小写转化函数,文本转化大写,文本转化小写
    024、MySQL字符串替换函数,文本替换函数
    023、MySQL取文本长度取字符串长度
    022、MySQL字符串的拼接
    021、MySQL变量的使用,在MySQL中创建存储过程,并添加变量
    020、MySQL创建一个存储过程,显示存储过程,调用存储过程,删除存储过程
    019、MySQL取本季度开始时间和本季度结束时间
    018、MySQL取满足日期在两个日期之间的所有数据
    017、MySQL取第4本季度开始和结束日期
    016、MySQL取本年第一季度开始日期
  • 原文地址:https://www.cnblogs.com/xiohao/p/5279581.html
Copyright © 2011-2022 走看看