java - springboot kafka onApplicationEvent
问题描述
我有一个 Springboot 应用程序,它具有KafkaConfig
生成和使用 Kafka 消息的组件。我还有一个数据库连接池管理器,用于初始化数据库池并创建数据库连接。这不是由 Spring 管理的,它是一个 Java 应用程序。我将此连接池代码捆绑在 jar 中。ApplicationListener<ApplicationReadyEvent>
此连接池由Springboot 应用程序内部实现的 CP 类初始化。现在,我正在尝试使用连接池初始化的代码从数据库中获取 Kafka 引导服务器信息。如何确保我的 bean 在KafkaConfig
CP 类之后初始化?
@Component
public class Startup implements ApplicationListener<ApplicationReadyEvent> {
@Override
public void onApplicationEvent(final ApplicationReadyEvent event) {
// connection pool getting initialized...
return;
}
}
这是从数据库中获取引导服务器信息的存储库类
@Repository
public class Repo {
public string getKafkaServer(){
//Uses the ConnectionPool initialized by Startup class to get the
//bootstrap sever information.
}
}
这是 Kafka 配置类
@Component
@EnableKafka
public class KafkaConfig {
@Autowired
Repo repo;
@Bean
public consumerFactory<String, String> consumerFactory(){
String bootstrapServer = repo.getKafkaServer();
//The bootstrapServer is coming as null and this code executes before the
//Startup class gets initialized.
}
}
关于如何KafkaConfig
在启动代码加载后执行的任何想法?
解决方案
推荐阅读
- javascript - 无法从另一台电脑查看局域网上的 React 应用程序
- selenium - Appium点击动态列表项
- powershell - 从 DistinguishedName 中提取 OU 和域
- java - 如何将 log4j 配置设置为仅记录信息消息
- module - 无法让 appcelerator 模块/小部件 SocialShare 工作
- azure - Application Insights:探查器跟踪中的“非托管异步”
- java - 如何要求 servlet 提供方法 java 的实现
- asp.net-core - 将 SignalR 服务添加为单例,然后将 redis 添加到其中
- vuetify.js - nuxt vuetify 谷歌字体
- codeigniter - 使用 Codeigniter 购物车库时购物车显示 0 件商品