spring容器加载完成执行方法

onApplicationEvent就是完成执行某个方法,我这里是从数据库查到省市编码code跟名字放到customCodeAndName里面使用。。。

import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.stereotype.Component;


@Component
public class ApplicationListenerPostProcessor implements ApplicationListener<ContextRefreshedEvent> {


    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {

    }

}

因为使用springmvc有两个容器,这个方法会执行两次,要想只执行一次通过这个判断即可:

  if (event.getApplicationContext().getParent() != null) {
                return;
            }
posted @ 2019-06-21 16:02  你就像甜甜的益达  阅读(131)  评论(0编辑  收藏  举报