通过ApplicationListener<event>实现服务启动时加载热key

22 阅读1分钟
@Component
public class HotKeyLoader implements ApplicationListener<ContextRefreshedEvent> {

    @Autowired
    private HotKeyService hotKeyService;

    @Override
    public void onApplicationEvent(ContextRefreshedEvent event) {
        // 确保只在根应用上下文初始化时加载热键
        if (event.getApplicationContext().getParent() == null) {
            hotKeyService.loadHotKeys();
        }
    }
}