正在阅读:
Springboot项目使用@PostConstruct注解启动时加载数据库数据到内存
背景:
项目框架使用的是 jeecgboot,当然,底层是整合了 springboot,在这个项目中,开发了job定时任务,定时同步数据,于是,将两个数据库的配置信息存放到数据库表中(其实也可以直接下载yml配置文件中)
目的是在项目启动后,加载job时,job所用到的数据源配置文件能从内存中加载,于是,用到了 @PostConstruct注解
代码:
@Component
@Order(value=1)
public class SynchronizationRunner {
public static List<SysSynDataSource> dataSourceList =new ArrayList<>();
@Autowired
ISysSynDataSourceService sysSynDataSourceService;
@PostConstruct
private void init() throws IOException {// 同步数据启动入口
System.out.println("同步用数据源加载开始");
dataSourceList= sysSynDataSourceService.list();
}
}这样,在项目启动时,即将数据库中配置信息加载到内存中
注:这里使用了个@Order注解,保证该配置信息为最优先加载;
使用:
在job中,加载以上获取的配置信息即可,如:
List<SysSynDataSource> dataSourcesConfiglist = SynchronizationRunner.dataSourceList;
log.info(dataSourcesConfiglist.toString());
log.info(String.format("--InventoryClassSynJob定时任务 start ! 时间:" + DateUtils.getTimestamp()));
SynchronizationUtilValue.start(SynchronizationUtilValue.getValue9(),dataSourcesConfiglist.get(0));
log.info(String.format("--InventoryClassSynJob定时任务 end ! 时间:" + DateUtils.getTimestamp()));该日志由 bemender 于 2021年01月08日 发表
转载请注明文本地址:http://www.bemhome.com/post/93.html
