SpringBoot @PostConstruct原理用法解析
作者:lwb444 时间:2022-02-02 18:05:46
前言
本节我们将学习一下@PostConstruct的用法。
概述
@PostContruct是spring框架的注解,在方法上加该注解会在项目启动的时候执行该方法,也可以理解为在spring容器初始化的时候执行该方法。
/**
* 项目启动时,初始化定时器
*/
@PostConstruct
public void init()
{
List<Job> jobList = jobDao.selectJobAll();
for (Job job : jobList)
{
CronTrigger cronTrigger = ScheduleUtils.getCronTrigger(scheduler, job.getJobId());
// 如果不存在,则创建
if (cronTrigger == null)
{
ScheduleUtils.createScheduleJob(scheduler, job);
}
else
{
ScheduleUtils.updateScheduleJob(scheduler, job);
}
}
}
上述代码表示在项目启动,Spring IOC容器初始化创建之后,Bean初始化之前和销毁之前,执行@PostConstruct注解的方法。
一般用于一些项目初始化的设定。比如Spring IOC Container 初始化之后,用@PostConstruct注解Quartz的 CronTrigger 用于初始化定时器(向定时器中添加定时启动的JOB)。那么项目运行时就能自动的运行CronTrigger 中的job了。
来源:https://www.cnblogs.com/linwenbin/p/13403076.html
标签:Spring,Boot,Post,Construct
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Mybatis返回int或者Integer类型报错的解决办法
2023-08-09 02:41:14
C#判断数据类型的简单示例代码
2023-09-22 22:22:48
Android使用PowerImageView实现播放强大的ImageView动画效果
2022-06-16 19:55:54
![](https://img.aspxhome.com/file/2023/0/88720_0s.gif)
Android实现界面内嵌多种卡片视图(ViewPager、RadioGroup)
2023-03-06 15:48:31
![](https://img.aspxhome.com/file/2023/5/108235_0s.png)
java实现Object转String的4种方法小结
2023-07-18 08:54:53
![](https://img.aspxhome.com/file/2023/8/121948_0s.jpg)
Android开发使用Drawable绘制圆角与圆形图案功能示例
2023-08-26 21:07:09
C#如何Task执行任务,等待任务完成
2022-03-06 11:31:31
![](https://img.aspxhome.com/file/2023/2/119452_0s.jpg)
flutter实现底部导航栏
2023-08-23 01:06:13
![](https://img.aspxhome.com/file/2023/0/109150_0s.jpg)
SpringBoot整合Redis正确的实现分布式锁的示例代码
2023-05-05 14:59:58
![](https://img.aspxhome.com/file/2023/7/81297_0s.png)
解析C# 程序结构
2021-11-15 05:22:59
springboot之端口设置和contextpath的配置方式
2023-10-05 14:16:20
C# Partial:分部方法和分部类代码实例
2022-04-07 11:28:26
spring aop之链式调用的实现
2022-10-30 05:56:50
![](https://img.aspxhome.com/file/2023/4/84654_0s.png)
浅谈Spring与SpringMVC父子容器的关系与初始化
2023-02-08 12:09:05
![](https://img.aspxhome.com/file/2023/3/79273_0s.jpg)
Android Intent传递大量数据出现问题解决
2021-11-07 10:16:13
C#操作XML方法详解
2022-11-10 21:21:14
Android 调试工具用法详细介绍
2022-07-18 21:34:05
你知道jdk竟有4个random吗
2022-06-14 23:37:08
Java swing读取txt文件实现学生考试系统
2021-06-13 17:41:02
![](https://img.aspxhome.com/file/2023/6/79906_0s.jpg)
C#中List集合使用Max()方法查找到最大值的实例
2023-12-01 08:03:32