核心思想
实现InitializingBean接口,重写afterPropertiesSet方法
范例代码
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.stereotype.Component;
@Slf4j
@Component
public class Demo implements InitializingBean {
@Override
public void afterPropertiesSet() throws Exception {
log.info("开始休眠5min");
Thread.sleep(1000 * 60 * 5);
log.info("休眠5min结束");
}
}
运行证明
[INFO] 2024-05-22 14:40:24.962 com.xxx.jscaffold.handler.Demo:[12][]- 开始休眠5min
[INFO] 2024-05-22 14:45:24.959 com.xxx.jscaffold.handler.Demo:[14][]- 休眠5min结束
[INFO] 2024-05-22 14:45:26.381 p6spy:[60][]- Consume Time:60 ms 2024-05-22 14:45:26
Execute SQL:SELECT 1
[INFO] 2024-05-22 14:45:26.887 com.jdl.jscaffold.MainApplication:[61][]- Started MainApplication in 308.167 seconds (JVM running for 309.04)
使用场景
可以定义一个sercie来实现InitializingBean接口,用于程序启动时加载元数据到内存中,如果没加载完毕,不算springContext成功结束。
标签:applicationContext,springboot,22,05,2024,5min,加载,14 From: https://www.cnblogs.com/PythonOrg/p/18206232