canna-cloud【四】spring bootのbean遅延初期化
2893 ワード
論理:
1、初期化抽象インターフェースを定義するInitService
2、モニターを通して、起動完了後、すべてのbeanを巡回して、インターフェースInitServiceが実現されたかどうかを判断する.
3、インターフェースが実現されると、インターフェースの初期化方法を呼び出し、インターフェース定義の初期化順序に従って初期化を行う.
1、初期化抽象インターフェースを定義するInitService
2、モニターを通して、起動完了後、すべてのbeanを巡回して、インターフェースInitServiceが実現されたかどうかを判断する.
3、インターフェースが実現されると、インターフェースの初期化方法を呼び出し、インターフェース定義の初期化順序に従って初期化を行う.
public interface InitService {
/*
* order
*/
/**
*
*/
static final Integer INTERCEPT_ORDER = 1000; // >= 1000
/**
*
*/
static final Integer QUEUE_ORDER = 2000; // >= 2000
/**
*
*/
static final Integer CONTROLLER_ORDER = 3000; // >= 3000
/**
*
*/
static final Integer SCHEDULE_ORDER = 4000; // >= 4000
/**
* service
*/
static final Integer SERVICE_ORDER = 5000; // >= 5000
/**
*
*/
static final Integer CONFIGURE_ORDER = 6000; // >= 6000
int getOrder();
void init() throws CannaException;
}
public class EndStartedListener implements ApplicationListener {
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if (((ApplicationContextEvent) event).getApplicationContext().getParent() == null) {
try {
this.initSystem(SpringContextHolder.getApplicationContext());
} catch (CannaException e) {
log.error("", e);
}
}
}
public void initSystem(ApplicationContext context) throws CannaException {
String beans[] = context.getBeanDefinitionNames();
log.info("bean length:{}", beans.length);
Map> beanMap = new HashMap>();
for (String bean : beans) {
Object obj = context.getBean(bean);
if (obj instanceof InitService) {
InitService initService = (InitService) obj;
Integer key = initService.getOrder();
List