首页 > 解决方案 > springboot kafka onApplicationEvent

问题描述

我有一个 Springboot 应用程序,它具有KafkaConfig生成和使用 Kafka 消息的组件。我还有一个数据库连接池管理器,用于初始化数据库池并创建数据库连接。这不是由 Spring 管理的,它是一个 Java 应用程序。我将此连接池代码捆绑在 jar 中。ApplicationListener<ApplicationReadyEvent>此连接池由Springboot 应用程序内部实现的 CP 类初始化。现在,我正在尝试使用连接池初始化的代码从数据库中获取 Kafka 引导服务器信息。如何确保我的 bean 在KafkaConfigCP 类之后初始化?

@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在启动代码加载后执行的任何想法?

标签: javaspring-bootapache-kafka

解决方案


推荐阅读