簡體   English   中英

Spring Autowired組件在bean方法中是null

[英]Spring Autowired Component is null in bean method

我正在嘗試在@Configuration class 中使用@Autowired變量,其中在方法中使用@Bean創建了一個 bean。 但是,我需要用來創建 bean 的組件是null

@Autowired
private JDAListener listener;

@Bean
public ShardManager shardManager() throws LoginException, IllegalArgumentException {
    DefaultShardManagerBuilder builder = DefaultShardManagerBuilder.createDefault(this.botToken)
            .enableIntents(GatewayIntent.GUILD_MEMBERS)
            .setStatus(OnlineStatus.IDLE)
            .setShardsTotal(this.totalShards)
            .addEventListeners(Arrays.asList(this.listener)); //throws Exception

    return builder.build();
}

我得到的異常如下:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'config': 
Unsatisfied dependency expressed through field 'shardManager'; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'shardManager' defined in class path resource [dev/teamnight/nightbot/Config.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [net.dv8tion.jda.api.sharding.ShardManager]: Circular reference involving containing bean 'config' - consider declaring the factory method as static for independence from its containing instance. Factory method 'shardManager' threw exception; 
nested exception is java.lang.IllegalArgumentException: listeners may not be null

我假設代碼片段是某種配置的一部分(用@Configuration或什@SpringBootApplication注釋的東西)。

在這種情況下:

  1. 使JDAListener也由 Spring 容器管理。
  2. 通過將參數傳遞給shardManager方法,將JDAListener bean 的實例注入到分片管理器中

您將得到如下所示的代碼:

@Configuration
public class MyConfiguration {


  @Bean // now jda listener is managed by spring!
  public JDAListener jdaListener() {
     return new JDAListener();
  }

  @Bean // note the parameter to the method
  public ShardManager shardManager(JDAListener jdaListener) throws LoginException, IllegalArgumentException {
        DefaultShardManagerBuilder builder = 
              DefaultShardManagerBuilder.createDefault(this.botToken)
                 .enableIntents(GatewayIntent.GUILD_MEMBERS)
                 .setStatus(OnlineStatus.IDLE)
                 .setShardsTotal(this.totalShards)
                 .addEventListeners(Arrays.asList(jdaListener)); //throws Exception

    return builder.build();
 }
}

我認為您正面臨此問題,因為 Spring 試圖在注入偵聽器之前依賴注入 bean。 您也嘗試將 JdaListener 聲明為 bean 怎么樣?

JDAListener 是否被標記為@Component/@Service 或告訴框架它應該被視為 bean 的東西? 另外,快速檢查您的組件掃描配置。

我正在嘗試在@Configuration class 中使用@Autowired變量,其中在方法中使用@Bean創建了一個bean。 但是,我需要創建 bean 的組件是null

@Autowired
private JDAListener listener;

@Bean
public ShardManager shardManager() throws LoginException, IllegalArgumentException {
    DefaultShardManagerBuilder builder = DefaultShardManagerBuilder.createDefault(this.botToken)
            .enableIntents(GatewayIntent.GUILD_MEMBERS)
            .setStatus(OnlineStatus.IDLE)
            .setShardsTotal(this.totalShards)
            .addEventListeners(Arrays.asList(this.listener)); //throws Exception

    return builder.build();
}

我得到的異常如下:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'config': 
Unsatisfied dependency expressed through field 'shardManager'; nested exception is org.springframework.beans.factory.BeanCreationException: 
Error creating bean with name 'shardManager' defined in class path resource [dev/teamnight/nightbot/Config.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: 
Failed to instantiate [net.dv8tion.jda.api.sharding.ShardManager]: Circular reference involving containing bean 'config' - consider declaring the factory method as static for independence from its containing instance. Factory method 'shardManager' threw exception; 
nested exception is java.lang.IllegalArgumentException: listeners may not be null

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM