zoukankan      html  css  js  c++  java
  • springboot(十五)-Runner启动器

    Runner启动器

    如果你想在Spring Boot启动的时候运行一些特定的代码,你可以实现接口ApplicationRunner或者CommandLineRunner,这两个接口实现方式一样,它们都只提供了一个run方法。

    CommandLineRunner:启动获取命令行参数。

    public interface CommandLineRunner {
    
        /**
         * Callback used to run the bean.
         * @param args incoming main method arguments
         * @throws Exception on error
         */
        void run(String... args) throws Exception;
    
    }

    ApplicationRunner:启动获取应用启动的时候参数。

    public interface ApplicationRunner {
    
        /**
         * Callback used to run the bean.
         * @param args incoming application arguments
         * @throws Exception on error
         */
        void run(ApplicationArguments args) throws Exception;
    
    }

    使用方式

    import org.springframework.boot.*;
    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.*;
    
    @Component
    public class MyBean implements CommandLineRunner  {
    
        public void run(String... args) {
           System.out.println("haha");
        }
        
        @Bean
        public CommandLineRunner init() {
    
            return (String... strings) -> {
            System.out.println("enen");
            };
    
        }
    
    
    }

    提供两个方法,一个实现了接口提供的方法,一个自己定义一个Bean。我们看下效果。

    05-09 18:10:07.920 INFO  [com.cetc.cks.Application] - Starting Application on LAPTOP-3HL6RUMK with PID 3012 (D:workspacespro_cks_manager	argetclasses started by 18811 in D:workspacespro_cks_manager)
    05-09 18:10:07.920 INFO  [com.cetc.cks.Application] - No active profile set, falling back to default profiles: default
    05-09 18:10:08.436 INFO  [org.apache.catalina.core.StandardService] - Starting service [Tomcat]
    05-09 18:10:08.436 INFO  [org.apache.catalina.core.StandardEngine] - Starting Servlet Engine: Apache Tomcat/8.5.23
    05-09 18:10:08.498 INFO  [org.apache.jasper.servlet.TldScanner] - At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    05-09 18:10:08.498 INFO  [o.a.c.core.ContainerBase.[Tomcat].[localhost].[/]] - Initializing Spring embedded WebApplicationContext
    05-09 18:10:08.764 INFO  [org.hibernate.jpa.internal.util.LogHelper] - HHH000204: Processing PersistenceUnitInfo [
        name: default
        ...]
    05-09 18:10:08.795 INFO  [org.hibernate.dialect.Dialect] - HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
    haha
    enen
    05-09 18:10:09.025 INFO  [com.cetc.cks.Application] - Started Application in 1.148 seconds (JVM running for 414.131)

    由控制台的log可知,都执行了。

    我们实现另一个:

    @Component
    public class MyBean implements ApplicationRunner   {
    
    //    public void run(String... args) {
    //       System.out.println("haha");
    //    }
        
        @Bean
        public CommandLineRunner init() {
    
            return (String... strings) -> {
            System.out.println("enen");
            };
    
        }
    
        @Override
        public void run(ApplicationArguments args) throws Exception {
            // TODO Auto-generated method stub
            System.out.println("666");
        }
    
    }

    同样,我们提供两个方法,一个实现接口的方法,还有一个自定义一个Bean。

    05-09 18:12:58.234 INFO  [com.cetc.cks.Application] - Starting Application on LAPTOP-3HL6RUMK with PID 3012 (D:workspacespro_cks_manager	argetclasses started by 18811 in D:workspacespro_cks_manager)
    05-09 18:12:58.234 INFO  [com.cetc.cks.Application] - No active profile set, falling back to default profiles: default
    05-09 18:12:58.703 INFO  [org.apache.catalina.core.StandardService] - Starting service [Tomcat]
    05-09 18:12:58.703 INFO  [org.apache.catalina.core.StandardEngine] - Starting Servlet Engine: Apache Tomcat/8.5.23
    05-09 18:12:58.766 INFO  [org.apache.jasper.servlet.TldScanner] - At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    05-09 18:12:58.781 INFO  [o.a.c.core.ContainerBase.[Tomcat].[localhost].[/]] - Initializing Spring embedded WebApplicationContext
    05-09 18:12:59.109 INFO  [org.hibernate.jpa.internal.util.LogHelper] - HHH000204: Processing PersistenceUnitInfo [
        name: default
        ...]
    05-09 18:12:59.125 INFO  [org.hibernate.dialect.Dialect] - HHH000400: Using dialect: org.hibernate.dialect.MySQL5Dialect
    666
    enen
    05-09 18:12:59.390 INFO  [com.cetc.cks.Application] - Started Application in 1.212 seconds (JVM running for 584.496)

    由控制台的log可知,都执行了。而且都是先执行重写接口的方法。

  • 相关阅读:
    mysql索引及优化
    mysql5.5 uuid做主键与int做主键的性能实测
    php生成UUID
    Android 图片的裁剪与相机调用
    Android GPS 临近触发
    Android中GPS类及方法简介
    永久删除 tadb.exe
    linux服务器调整参数支持高并发
    隐藏nginx 版本号信息
    nginx 重写 rewrite 基础及实例
  • 原文地址:https://www.cnblogs.com/fengyuduke/p/10840182.html
Copyright © 2011-2022 走看看