• Spring Cloud微服务实战 打造企业级优惠券系统 3-9 SpringBoot开机启动


    0    课程地址

    http://coding.imooc.com/lesson/380.html#mid=28360

    1    浓缩精华

    2.1,4.1,4.2

    2    个人关注
    2.1  initializingbean开启启动和本节两种开机启动有什么优劣

    这个问题提的挺好的,我来做一些解释。首先,我们可以看一看 InitializingBean 的定义:

    public interface InitializingBean {
        void afterPropertiesSet() throws Exception;
    }

        InitializingBean 接口为 bean 提供了属性初始化后的处理方法,它只包括 afterPropertiesSet 方法,凡是继承该接口的类,在 bean 的属性初始化后都会执行该方法。

        但是,需要注意的是,InitializingBean 是在当前的 Bean 初始化完成之后就去执行的。而我在课程中讲到的 ApplicationRunner 和 CommandLineRunner 则是在所有的 Bean 都初始化完成之后才会去执行。所以,我们可以在 ApplicationRunner 和 CommandLineRunner 中直接注入系统中的 bean 做一些操作。

        另外,InitializingBean 是 Spring 框架的接口,而 ApplicationRunner 和 CommandLineRunner 则是 SpringBoot 提供的接口

        不过,通常来说,如果开机启动的任务没有依赖,使用任何一种方式都是可行的,区别不大。

    3    课程内容
    3.1  课程内容

    两种开机启动方式以及@Order注解的应用

    4    代码演练
    4.1  两种开启启动方式(ApplicationRunner和CommandRunner)(ApplicationRunner先启动)

    BootApplicationRunner

    package com.imooc.springboot.application.study;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.stereotype.Component;
    
    /**
     * BootApplicationRunner
     *
     * @author 魏豆豆
     * @date 2021/4/4
     */
    @Component
    @Slf4j
    public class BootApplicationRunner implements ApplicationRunner {
        @Override
        public void run(ApplicationArguments args) throws Exception {
          log.info("BootApplicationRunner===========启动");
        }
    }

    BootCommandRunner

    package com.imooc.springboot.application.study;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.stereotype.Component;
    
    /**
     * BootCommandRunner
     *
     * @author 魏豆豆
     * @date 2021/4/4
     */
    @Component
    @Slf4j
    public class BootCommandRunner implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            log.info("============================BootCommandRunner");
        }
    }

    打印日志

      .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )\___ | '_ | '_| | '_ / _` |    
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v2.1.4.RELEASE)
    
    2021-04-04 08:39:37.971  INFO 32 --- [           main] c.i.s.a.SpringBootStudyApplication       : Starting SpringBootStudyApplication on DESKTOP-A08HONB with PID 32 (F:kewaiSpringCloudlaoqinchapter_3imooc_springboot_study	argetclasses started by weijingli in F:kewaiSpringCloudlaoqinchapter_3imooc_springboot_study)
    2021-04-04 08:39:37.982  INFO 32 --- [           main] c.i.s.a.SpringBootStudyApplication       : No active profile set, falling back to default profiles: default
    2021-04-04 08:39:43.845 ERROR 32 --- [           main] o.a.catalina.core.AprLifecycleListener   : An incompatible version [1.1.32] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]
    2021-04-04 08:39:45.470 ERROR 32 --- [           main] o.a.catalina.core.AprLifecycleListener   : An incompatible version [1.1.32] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]
    2021-04-04 08:39:45.828  INFO 32 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8081 (http)
    2021-04-04 08:39:45.855 ERROR 32 --- [           main] o.a.catalina.core.AprLifecycleListener   : An incompatible version [1.1.32] of the APR based Apache Tomcat Native library is installed, while Tomcat requires version [1.2.14]
    2021-04-04 08:39:45.871  INFO 32 --- [           main] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
    2021-04-04 08:39:45.871  INFO 32 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.17]
    2021-04-04 08:39:46.403  INFO 32 --- [           main] o.a.c.c.C.[Tomcat].[localhost].[/imooc]  : Initializing Spring embedded WebApplicationContext
    2021-04-04 08:39:46.403  INFO 32 --- [           main] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 8187 ms
    2021-04-04 08:39:47.698  INFO 32 --- [           main] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
    2021-04-04 08:39:48.347  INFO 32 --- [           main] o.s.s.c.ThreadPoolTaskScheduler          : Initializing ExecutorService 'taskScheduler'
    2021-04-04 08:39:48.385  INFO 32 --- [           main] o.s.b.a.e.web.EndpointLinksResolver      : Exposing 2 endpoint(s) beneath base path '/actuator'
    2021-04-04 08:39:48.649  INFO 32 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8081 (http) with context path '/imooc'
    2021-04-04 08:39:48.656  INFO 32 --- [           main] c.i.s.a.SpringBootStudyApplication       : Started SpringBootStudyApplication in 13.154 seconds (JVM running for 15.812)
    2021-04-04 08:39:48.692  INFO 32 --- [           main] c.i.s.a.study.BootApplicationRunner      : BootApplicationRunner===========启动
    2021-04-04 08:39:48.692  INFO 32 --- [           main] c.i.s.a.study.BootCommandRunner          : ============================BootCommandRunner
    4.2  @Order调整先后顺序

    BootApplicationRunner

    package com.imooc.springboot.application.study;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.stereotype.Component;
    
    /**
     * BootApplicationRunner
     *
     * @author 魏豆豆
     * @date 2021/4/4
     */
    @Order(2)
    @Component @Slf4j public class BootApplicationRunner implements ApplicationRunner { @Override public void run(ApplicationArguments args) throws Exception { log.info("BootApplicationRunner===========启动"); } }

    BootCommandRunner

    package com.imooc.springboot.application.study;
    
    import lombok.extern.slf4j.Slf4j;
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.stereotype.Component;
    
    /**
     * BootCommandRunner
     *
     * @author 魏豆豆
     * @date 2021/4/4
     */
    @Order(1)
    @Component @Slf4j public class BootCommandRunner implements CommandLineRunner { @Override public void run(String... args) throws Exception { log.info("============================BootCommandRunner"); } }


    打印日志:

    2021-04-04 08:46:11.492  INFO 18544 --- [           main] c.i.s.a.study.BootCommandRunner          : ============================BootCommandRunner
    2021-04-04 08:46:11.492  INFO 18544 --- [           main] c.i.s.a.study.BootApplicationRunner      : BootApplicationRunner===========启动
    诸葛
  • 相关阅读:
    vue项目按钮权限配置
    页面框架搭建模板可复用
    vue循环渲染复选框列表
    Vue项目网页版在浏览器中实现扫码识别功能(项目应用篇)
    基于51单片机的智能小车
    卡特兰数:翻折思想 翻折前后一一对应
    Redis之Info指令
    Redis的安全使用
    Redis中的近似LRU超出内存限制后的处理
    Redis主从同步
  • 原文地址:https://www.cnblogs.com/1446358788-qq/p/14296101.html
Copyright © 2020-2023  润新知