一、背景说明
二、实现方式
@SpringBootApplication
@EnableCaching
@MapperScan("mapper")
@AllArgsConstructor
@Slf4j
public class JoolunTransferApplication {
public static void main(String[] args) {
SpringApplication.run(JoolunTransferApplication.class, args);
ApplicationContext context = SpringContextUtil.getContext();
log.info("初始化开始");
long beginTime = SystemClock.now();
UserInfoService userInfoService = context.getBean(UserInfoService.class);
userInfoService.initGoUserInfo();
long totalConsumedTime = SystemClock.now() - beginTime;
log.info("共使用时间为 totalConsumedTime:[{}]", totalConsumedTime);
log.info("初始化结束");
}
}
@Component
public class SpringContextUtil implements ApplicationContextAware {
private static ApplicationContext context;
@SuppressWarnings("static-access")
@Override
public void setApplicationContext(ApplicationContext applicationContext)
throws BeansException {
this.context = applicationContext;
}
public static ApplicationContext getContext() {
return context;
}
@SuppressWarnings("unchecked")
public static <T> T getBean(String name) {
return (T) context.getBean(name);
}
public static <T> T getBean(Class<T> clazz) throws BeansException {
return context.getBean(clazz);
}
}