zoukankan      html  css  js  c++  java
  • SpringBoot项目启动时执行初始化操作

    SpringBooot中的CommandLineRunner接口会在所有Spring Beans初始化之后,SpringApplication.run()之前执行。

    1.添加pom引用

    <?xml version="1.0" encoding="UTF-8"?>
    
    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
        <modelVersion>4.0.0</modelVersion>
    
        <groupId>com.itstudy</groupId>
        <artifactId>demo</artifactId>
        <version>1.0-SNAPSHOT</version>
    
        <name>demo</name>
        <url>http://www.example.com</url>
        <properties>
            <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
            <maven.compiler.source>1.8</maven.compiler.source>
            <maven.compiler.target>1.8</maven.compiler.target>
        </properties>
        <dependencies>
            <dependency>
                <groupId>junit</groupId>
                <artifactId>junit</artifactId>
                <version>4.12</version>
                <scope>test</scope>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-test</artifactId>
                <scope>test</scope>
            </dependency>
    
        </dependencies>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>1.5.9.RELEASE</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
        <build>
            <plugins>
                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                </plugin>
            </plugins>
        </build>
    </project>

    2.添加两个CommandLineRunner

    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    @Component
    @Order(2) //指定顺序
    public class Runner1 implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            System.out.println("Runner1 ===========");
        }
    }
    import org.springframework.boot.CommandLineRunner;
    import org.springframework.core.annotation.Order;
    import org.springframework.stereotype.Component;
    
    @Component
    @Order(1) //指定顺序
    public class Runner2 implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            System.out.println("Runner2 ===========");
        }
    }

    3.启动项目

    import org.springframework.boot.SpringApplication;
    import org.springframework.boot.autoconfigure.SpringBootApplication;
    
    @SpringBootApplication
    public class App {
    
        public static void main(String[] args) {
    
            System.out.println("启动中...");
    
            SpringApplication.run(App.class, args);
    
            System.out.println("启动成功");
        }
    }

    4.运行结果

    启动中...
    
      .   ____          _            __ _ _
     /\ / ___'_ __ _ _(_)_ __  __ _    
    ( ( )\___ | '_ | '_| | '_ / _` |    
     \/  ___)| |_)| | | | | || (_| |  ) ) ) )
      '  |____| .__|_| |_|_| |_\__, | / / / /
     =========|_|==============|___/=/_/_/_/
     :: Spring Boot ::        (v1.5.9.RELEASE)
    
    2019-06-11 09:49:12.152  INFO 4536 --- [           main] com.itstudy.App                          : Starting App on liuwma with PID 4536 (D:javaworkideawork
    unnerdemo	argetclasses started by Administrator in D:javaworkideawork)
    2019-06-11 09:49:12.156  INFO 4536 --- [           main] com.itstudy.App                          : No active profile set, falling back to default profiles: default
    2019-06-11 09:49:12.247  INFO 4536 --- [           main] s.c.a.AnnotationConfigApplicationContext : Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@5123a213: startup date [Tue Jun 11 09:49:12 CST 2019]; root of context hierarchy
    2019-06-11 09:49:13.277  INFO 4536 --- [           main] o.s.j.e.a.AnnotationMBeanExporter        : Registering beans for JMX exposure on startup
    Runner2 ===========
    Runner1 ===========
    2019-06-11 09:49:13.300  INFO 4536 --- [           main] com.itstudy.App                          : Started App in 1.604 seconds (JVM running for 2.483)
    启动成功
  • 相关阅读:
    548
    zookeeper的异常处理(Disconnected, SyncConnected, Expired)
    ZOJ 2588 Burning Bridges(求含重边的无向连通图的割边)
    SQL Server 中VARCHAR(MAX)变量赋值引起的性能问题。
    查看mysql数据库版本方法总结
    Linux mysql 5.6: ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: NO)
    Ignite用户配置管理介绍
    ORACLE动态采样分析
    SQL SERVER 2012链接到SQL SERVER 2000的问题解决案例
    SQL SERVER 2005删除维护作业报错:The DELETE statement conflicted with the REFERENCE constraint "FK_subplan_job_id"
  • 原文地址:https://www.cnblogs.com/liuxm2017/p/11002004.html
Copyright © 2011-2022 走看看