繁体   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