zoukankan      html  css  js  c++  java
  • 利用spring的ApplicationListener实现springmvc容器的初始化加载--转

    1、我们在使用springmvc进行配置的时候一般初始化都是在web.xml里面进行的,但是自己在使用的时候经常会测试一些数据,这样就只有加载spring-mvc.xml的配置文件来实现。为了更方便的使用注解,而不影响具体的实现效果,我今天看到了一个初始化的方式,就是实现ApplicationListener接口

    2.spring容器加载bean完成后进行初始化

    package com.cy.init;
    
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.event.ContextRefreshedEvent;
    
    public class SpringListener implements ApplicationListener<ContextRefreshedEvent>{
        
        //spring容器加载bean完成后进行初始化
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            if (event.getApplicationContext().getParent() == null) {    //root application context 没有parent
                System.out.println("容器加载完毕执行的方法test()");
            }
        }
    
    }

    3、spring配置文件中配置:

    <bean class="com.cy.init.SpringListener"/>

    4、这种方式在数据库初始化,获取配置文件等初始化,都很不错不用再通过具体的servlet来手动实现
    5、另外想要对bean进行操作,可以参考:http://www.cnblogs.com/ll409546297/p/6433420.html

    ===========================<补充> ================================

    上面的例子,如果要使用到spring容器中的其他bean呢?比如service,controller等,这时候需要获取到spring的容器applicationContext,怎么做?

    下面是个例子,使用的是【spring框架】spring获取webapplicationcontext,applicationcontext几种方法详解--(转)这篇文章里面的方法三:继承自抽象类ApplicationObjectSupport

    代码:

    package com.cy.init;
    
    import org.springframework.beans.BeansException;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.ApplicationListener;
    import org.springframework.context.event.ContextRefreshedEvent;
    import org.springframework.context.support.ApplicationObjectSupport;
    
    public class SpringListener extends ApplicationObjectSupport implements ApplicationListener<ContextRefreshedEvent>{
        private static ApplicationContext applicationContext = null;
        
        //spring容器加载bean完成后进行初始化
        @Override
        public void onApplicationEvent(ContextRefreshedEvent event) {
            if (event.getApplicationContext().getParent() == null) {
                System.out.println("容器加载完毕执行的方法test()");
                
                //do  someting
                // Object o = getBean(beanId);
                // ...
            }
        }
        
        @Override
        protected void initApplicationContext(ApplicationContext context) throws BeansException {
            super.initApplicationContext(context);
            if(null == applicationContext){
                applicationContext = context;
            }
        }
        
        public static ApplicationContext getAppContext(){
            return applicationContext;
        }
        
        public static Object getBean(String beanId){
            return getAppContext().getBean(beanId);
        }
    
    }

    小结:

    使用场景

    在一些业务场景中,当容器初始化完成之后,需要处理一些操作,比如一些数据的加载、初始化缓存、特定任务的注册等等。这个时候我们就可以使用Spring提供的ApplicationListener来进行操作。

    二次调用问题

    在使用传统的application.xml和project-servlet.xml配置中会出现二次调用的问题。主要原因是初始化root容器之后,会初始化project-servlet.xml对应的子容器。我们需要的是只执行一遍即可。那么上面打印父容器的代码用来进行判断排除子容器即可。在业务处理之前添加如下判断:

    if(contextRefreshedEvent.getApplicationContext().getParent() != null){
                return;
    }

    这样其他容器的初始化就会直接返回,而父容器(Parent为null的容器)启动时将会执行相应的业务操作。

    -------

  • 相关阅读:
    Nim or not Nim? HDU
    邂逅明下 HDU
    4.1.8 巴什博弈
    4.1.7 Cutting Game(POJ 2311)
    0.1.2 max_element和min_element的用法
    bzoj 2152 聪聪可可 树形dp
    hdu 5976 Detachment 脑洞题 猜结论
    hdu 5974 A Simple Math Problem gcd(x,y)=gcd((x+y),lcm(x,y))
    hdu 5971 Wrestling Match 二分图染色
    Codeforces 842C Ilya And The Tree 树上gcd
  • 原文地址:https://www.cnblogs.com/tenWood/p/8597492.html
Copyright © 2011-2022 走看看