码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • springboot实现项目启动前的一些操作


    在服务启动时,做一些操作,比如加载配置,初始化数据,请求其他服务的接口等。
    有三种方法:

    • 第一种是实现CommandLineRunner接口
    • 第二种是实现ApplicationRunner接口
    • 第三种是使用注解:@PostConstruct

    三者使用方法

    先看下三种方法分别怎么实现我们的目的

    CommandLineRunner

    CommandLineRunner执行的时间节点是在Application完成初始化工作之后。

    CommandLineRunner在有多个实现的时候,可以使用@order注解指定执行先后顺序。、

    使用方法:实现CommandLineRunner接口,并重写run方法,run方法里写我们的初始化操作。

    示例:

    import org.springframework.boot.CommandLineRunner;
    import org.springframework.stereotype.Component;
    
    
    @Component
    public class InitTest02 implements CommandLineRunner {
        @Override
        public void run(String... args) throws Exception {
            System.out.println("========CommandLineRunner");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    ApplicationRunner

    ApplicationRunner跟CommandLineRunner是区别是在run方法里接收的参数不同,CommandLineRuner接收的参数是String… args,而ApplicationRunner的run方法的参数是ApplicationArguments 。

    实现ApplicationRunner接口,并重写run方法,run方法里写我们的初始化操作。

    示例:

    import org.springframework.boot.ApplicationArguments;
    import org.springframework.boot.ApplicationRunner;
    import org.springframework.stereotype.Component;
    
    
    @Component
    public class InitTest01 implements ApplicationRunner {
        @Override
        public void run(ApplicationArguments args) throws Exception {
            System.out.println("=======ApplicationRunner");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    @PostConstruct

    @PostConstruct是在对象加载完之后执行。

    使用@PostConstruct注解再自己写的方法上,方法内写初始化逻辑

    示例:

    import org.springframework.stereotype.Component;
    
    import javax.annotation.PostConstruct;
    
    
    @Component
    public class InitTest03 {
    
        @PostConstruct
        public void start() {
            System.out.println("========PostConstruct");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    三者区别

    三者都可以实现项目启动前的一些初始化操作,唯一不同的是初始化的时间不同。

    Spring应用启动过程中,肯定是要自动扫描有@Component注解的类,加载类并初始化对象进行自动注入。加载类时首先要执行static静态代码块中的代码,之后再初始化对象时会执行构造方法。

    在对象注入完成后,调用带有@PostConstruct注解的方法。当容器启动成功后,再根据@Order注解的顺序调用CommandLineRunner和ApplicationRunner接口类中的run方法。

    因此,加载顺序为static>constructer>@PostConstruct>CommandLineRunner和ApplicationRunner。

    参考

    https://blog.csdn.net/qinshengfei/article/details/114856010

  • 相关阅读:
    美团资深技术专家亲笔的400页的高并发系统设计,秒杀一众面试官
    安卓手机磁盘空间不足怎样导出数据?
    【云原生 | Kubernetes 系列】--Gitops持续交付 实现从代码克隆到应用部署
    基于JAVA的在线考试平台的设计与实现,正在做的同学可以参考一下【数据库设计、源码、开题报告】
    AI:业余时间打比赛—挣它个小小目标—【阿里安全×ICDM 2022】大规模电商图上的风险商品检测比赛
    《数字化与碳中和(园区篇)》报告正式发布,助力加快推进国家“双碳”战略实施
    【编码译码】基于matlab QC-LDPC码编码和译码【含Matlab译码 2194期】
    Flask-SQLAlchemy 快速上手
    Redisson集群管理工具、对Redis节点的操作
    携手ChainGPT 人工智能基础设施 波场TRON革新 Web3 版图
  • 原文地址:https://blog.csdn.net/qq_43745578/article/details/126918750
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | Kerberos协议及其部分攻击手法
    0day的产生 | 不懂代码的"代码审计"
    安装scrcpy-client模块av模块异常,环境问题解决方案
    leetcode hot100【LeetCode 279. 完全平方数】java实现
    OpenWrt下安装Mosquitto
    AnatoMask论文汇总
    【AI日记】24.11.01 LangChain、openai api和github copilot
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1
正则表达式工具 cron表达式工具 密码生成工具

京公网安备 11010502049817号