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


    喜欢就推荐一下,因为你的参与是我在写作道路上的最强动力。
    本文版权归作者淼淼之森和博客园共有,欢迎转载。
    但须在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
  • 相关阅读:
    IDEA 现有项目连接SVN
    图片预加载,懒加载
    element-ui table中排序 取消表格默认排序问题
    Linux vim编辑命令
    linux mount 硬盘挂载和卸载
    Linux mke2fs 硬盘格式化
    Linux rm 删除文件
    linux 创建和删除目录
    linux 配置IP
    kickstart自动化安装--tftp+nfs+dhcp
  • 原文地址:https://www.cnblogs.com/mmzs/p/14567264.html
Copyright © 2011-2022 走看看