zoukankan      html  css  js  c++  java
  • springboot 学习之路 9 (项目启动后就执行特定方法)


    概述:

        spring boot 整理之路继续前行,这次我说一下怎么在spring boot项目启动后立即执行某一个特定的方法,已经要执行特定方法不止一个的时候的顺序问题【顺序问题我只说注解咋实现@Order】


    分析:

        这次我拿项目中某一个功能为案例,进行演示:

          如何在项目启动时启动ftp服务器(java内嵌ftp服务器):如何在spring boot项目启动时让ftp服务器也随之启动呢?


    方案:

        针对上面的问题,我在spring boot中找到了解决方案。那就是spring boot给我们提供了两个接口 ApplicationRunnerCommandLineRunner。这两个接口是Springboot给我们提供了两种“开机启动”的方式

        ApplicationRunner  :

        源码如下:

          public interface ApplicationRunner {

              void run(ApplicationArguments var1) throws Exception;
          }

        CommandLineRunner  :

        源码如下:  

          public interface CommandLineRunner {

              void run(String... var1) throws Exception;
          }

        对比:
        相同点:这两种方法提供的目的是为了满足,在项目启动的时候立刻执行某些方法。我们可以通过实现ApplicationRunner和CommandLineRunner,来实现,他们都是在SpringApplication 执行之后开始执行的。
        不同点:CommandLineRunner接口可以用来接收字符串数组的命令行参数,ApplicationRunner 是使用ApplicationArguments 用来接收参数的 【根据业务场景灵活运用】

     代码:

     注意:在这我用了注解,如果选择接口实现方式只需要实现Order接口即可,里面有个getOrder方法    返回个int值,值越小,越优先执行
    /**
     * @Author:huhy
     * @DATE:Created on 2018/1/3 12:47
     * @Modified By:
     * @Class 项目启动后运行此方法:CommandLineRunner实现
     */
    @Component
    @Order(value=2)
    public class FtpInitRunner implements CommandLineRunner {
        @Override
        /**
         *@Author: huhy
         *@Package_name:com.huhy.web.common.runner
         *@Date:13:20 2018/1/3
         *@Description:项目启动后运行此方法 CommandLineRunner
         * //项目路径 path =  E:IDEworkspaceideaWorkspacespring bootspring-boot
        String path = System.getProperty("user.dir");
        CommandLine.main(new String[]{path+"\src\main\resources\ftpserver\ftpd-typical.xml"});
        System.out.println("----------------------"+path);
         */
        public  void run(String... var1) throws Exception{
            System.out.println("2222222222222222222222");
        }


    /**
     * @Author:huhy
     * @DATE:Created on 2018/1/3 13:20
     * @Modified By:
     * @Class Description:ApplicationRunner 实现
     */
    @Component
    @Order(value = 1)   //执行顺序控制
    public class FtpInitRunner2 implements ApplicationRunner{
        @Override
        /**
         *@Author: huhy
         *@Package_name:com.huhy.web.common.runner
         *@Date:13:29 2018/1/3
         *@Description:   ApplicationRunner方式实现
         */
        public void run(ApplicationArguments applicationArguments) throws Exception {
            //项目路径 path =  E:IDEworkspaceideaWorkspacespring bootspring-boot
            String path = System.getProperty("user.dir");
            CommandLine.main(new String[]{path+"\src\main\resources\ftpserver\ftpd-typical.xml"});
            System.out.println("----------------------"+path);
            System.out.println("111111111111111111111111");
    
    
        }

      启动图如下:


    总结:

        spring boot的项目启动注意分为两步:

        实现相应接口(ApplicationRunner和CommandLineRunner)     -----------》   加入注解   (@Component    |   @Order  )

  • 相关阅读:
    騎士宣言
    [洛谷P1631] 序列合并
    [HNOI2006]公路修建问题
    [洛谷2068] 统计和
    [洛谷P1168] 中位数
    【模板】可持久化数组(可持久化线段树/平衡树)
    【模板】可持久化线段树 1(主席树)
    [JSOI2008]最大数maxnumber
    NOI导刊2010提高(06) 黑匣子
    [洛谷1533] 可怜的狗狗
  • 原文地址:https://www.cnblogs.com/huhongy/p/8183390.html
Copyright © 2011-2022 走看看