码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • SpringBoot初始化数据的一些方法


    方式有很多,这里就说四种常用的

    注解方式

    方式一: @PostConstruct注解

    在项目启动时需要加载某个方法的时候,可以使用@Component+@PostConstruct方法将一个方法完成初始化操作

    spring中Constructor、@Autowired、@PostConstruct的顺序
    Constructor >> @Autowired >> @PostConstruct

    如果一个类A中有个成员变量p被@Autowired注解,那么@Autowired注入是发生在A的构造方法执行完之后的。

    @PostConstruct应用场景:

    • 如果想在生成对象时候完成某些初始化操作,而偏偏这些初始化操作又依赖于依赖注入,那么就无法在构造函数中实现。为此,可以使用@PostConstruct注解一个方法来完成初始化,@PostConstruct注解的方法将会在依赖注入完成后被自动调用。
    @Component
    public class PostConstantAnnotationTest {
    
        @PostConstruct
        public void init(){
            System.out.println("初始化init方法...");
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    方式二:@Bean注解的initMethod

    @Bean(initMethod = "init")
    public InitMethodDemo initMethodTest(){
    	return new InitMethodDemo();
    }
     
    public class InitMethodDemo {
     
        public void init(){
            // 执行相关业务
            System.out.println("InitMethodDemo初始化数据");
        }
     
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    实现接口

    方式一:InitializingBean接口

    @Component
    public class InitializingBeanDemo implements InitializingBean {
        @Override
        public void afterPropertiesSet() throws Exception {
            // 执行相关业务
            System.out.println("InitializingBeanDemo初始化数据");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    方式二:ApplicationListener接口

    Spring的事件机制。

    首先定义一个自己使用的监听器类并实现ApplicationListener接口

    @Component
    public class ApplicationListenerDemo implements ApplicationListener<ApplicationStartedEvent>, Ordered {
     
        @Override
        public void onApplicationEvent(ApplicationStartedEvent applicationStartedEvent) {
            // 执行相关业务
            System.out.println("ApplicationListenerDemo初始化数据");
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    然后通过SpringApplication类中的addListeners方法将自定义的监听器注册进去

    public class Application {
        public static void main(String[] args) {
            SpringApplication application = new SpringApplication(Application.class);
            application.addListeners(new MessageReceiver());
            application.run(args);
        
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
  • 相关阅读:
    MySQL数据库入门到精通8--进阶篇( MySQL管理)
    深度学习——特征点检测和目标检测
    深度学习必备Python基础知识充电2
    面向对象技术--设 计 模 式
    鸢尾花数据种类预测、分析与处理、scikit-learn数据集使用、seaborn作图及数据集的划分
    【PostgreSql基础语法 】1、增删改查、where、limit、like模糊查询
    【Redis实战】有MQ为啥不用?用Redis作消息队列!?Redis作消息队列使用方法及底层原理高级进阶
    java计算机毕业设计音乐网站MyBatis+系统+LW文档+源码+调试部署
    Qwt QwtLegend和QwtPlotLegendItem图例类详解
    数据结构和算法八股与手撕
  • 原文地址:https://blog.csdn.net/yyuggjggg/article/details/126018770
  • 最新文章
  • 攻防演习之三天拿下官网站群
    数据安全治理学习——前期安全规划和安全管理体系建设
    企业安全 | 企业内一次钓鱼演练准备过程
    内网渗透测试 | 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号