zoukankan      html  css  js  c++  java
  • Java项目启动时执行指定方法的几种方式

    1.使用 @PostConstruct,作用于方法上面。

    @Component
    public class PostConstruct {
    
        @PostConstruct
        public void test() {
            System.out.println("PostConstruct:开始运行...");
        }
    }
    

    2.使用 ApplicationRunner。

    @Component
    public class Start implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            System.out.println("CommandLineRunner:开始运行...");
        }
    }
    

    3.使用 CommandLineRunner 接口

    @Component
    public class Start1 implements ApplicationRunner {
        @Override
        public void run(ApplicationArguments args) throws Exception {
            System.out.println("ApplicationRunner:开始运行...");
        }
    }
    

    以上三种方式都是在项目启动的时候加载指定的方法,第一种使用的是 注解的方式,第二种、第三种使用的是实现接口的方式。

    它们的执行顺讯为 @PostConstruct--->ApplicationRunner--->CommandLineRunner

    1.使用 @PostConstruct,作用于方法上面。
    
    如果,您希望更容易地发现我的博客,不妨收藏关注一下
    原创公众号:Java学习之道
    个人博客 : www.mmzsblog.cn


    喜欢就推荐一下,因为你的参与是我在写作道路上的最强动力。
    本文版权归作者淼淼之森和博客园共有,欢迎转载。
    但须在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    定时备份脚本
    NFS+inotify实时同步
    pxe+kickstart自动化安装
    LVS负载均衡DR模式
    Rsync文件同步服务
    NFS文件共享服务
    MySQL-5.5.49安装、多实例、主从复制
    PHP-5.3.27源码安装及nginx-fastcgi配置
    一文解读5G (转)
    一文解读VR/AR/MR (转)
  • 原文地址:https://www.cnblogs.com/mmzs/p/14567264.html
Copyright © 2011-2022 走看看