• 【Spring】Springboot监听器,启动之后初始化工作


    package com.laplace.laplace.common.starter.config;

    import java.io.IOException;

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.event.ApplicationReadyEvent;
    import org.springframework.context.ApplicationListener;
    import org.springframework.stereotype.Component;

    /*
    * http://blog.netgloo.com/2014/11/13/run-code-at-spring-boot-startup/
    * http://www.baeldung.com/running-setup-logic-on-startup-in-spring
    * https://springframework.guru/running-code-on-spring-boot-startup/
    */
    @Component
    public class ApplicationStartup implements ApplicationListener<ApplicationReadyEvent>{
    private static final Logger LOG = LoggerFactory.getLogger(ApplicationStartup.class);

    @Autowired
    private GrpcLocalProxyService grpcLocalProxyService;

    @Override
    public void onApplicationEvent(ApplicationReadyEvent event) {
    if (grpcLocalProxyService == null) {
    LOG.info("grpcLocalProxyService is null/disabled, no need to loadProtoResources");
    }
    try {
    grpcLocalProxyService.loadProtoResources();
    } catch (IOException | ClassNotFoundException e) {
    LOG.error("grpcLocalProxyService loadProtoResources exception, {}", e);
    }
    }

    }

    package com.laplace.laplace.common.starter.config;
    
    import java.io.IOException;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.context.event.ApplicationReadyEvent;
    import org.springframework.context.ApplicationListener;
    import org.springframework.stereotype.Component;
    
    /*
     * http://blog.netgloo.com/2014/11/13/run-code-at-spring-boot-startup/
     * http://www.baeldung.com/running-setup-logic-on-startup-in-spring
     * https://springframework.guru/running-code-on-spring-boot-startup/
    */    
    @Component
    public class ApplicationStartup implements ApplicationListener<ApplicationReadyEvent>{
        private static final Logger LOG = LoggerFactory.getLogger(ApplicationStartup.class);
        
        @Autowired
        private GrpcLocalProxyService grpcLocalProxyService;
        
    
        @Override
        public void onApplicationEvent(ApplicationReadyEvent event) {
            if (grpcLocalProxyService == null) {
                LOG.info("grpcLocalProxyService is null/disabled, no need to loadProtoResources");
            } 
            try {
                grpcLocalProxyService.loadProtoResources();
            } catch (IOException | ClassNotFoundException e) {
                LOG.error("grpcLocalProxyService loadProtoResources exception, {}", e);
            }
        }
        
    }
  • 相关阅读:
    MarkDown学习
    做网站的或者博客的网站
    Calendar日历简单用法
    Cron表达式范例
    CSVFileUtil 读取写入CSV文件简单工具类
    直接读取ZIP包数据 线上、线下方法
    Spring @Async/@Transactional 失效的原因及解决方案
    多线程学习 读写锁
    多线程学习 公平锁和非公平锁
    多线程学习 ThreadLocal的使用。
  • 原文地址:https://www.cnblogs.com/junneyang/p/9132779.html
Copyright © 2020-2023  润新知