简体   繁体   English

Spring Autowired组件在bean方法中是null

[英]Spring Autowired Component is null in bean method

I am trying to use an @Autowired variable in an @Configuration class, where a bean is created using @Bean in a method.我正在尝试在@Configuration class 中使用@Autowired变量,其中在方法中使用@Bean创建了一个 bean。 However the component I need to create the bean with is null .但是,我需要用来创建 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();
}

The Exception I get is as follows:我得到的异常如下:

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

I assume the code snippet is a part of some kind of configuration (Something annotated with @Configuration or maybe even @SpringBootApplication ).我假设代码片段是某种配置的一部分(用@Configuration或什@SpringBootApplication注释的东西)。

In this case:在这种情况下:

  1. Make JDAListener be managed by Spring container as well.使JDAListener也由 Spring 容器管理。
  2. Inject the instance of JDAListener bean into the shard manager by passing the parameter to the shardManager method通过将参数传递给shardManager方法,将JDAListener bean 的实例注入到分片管理器中

You will end up with the code that looks like this:您将得到如下所示的代码:

@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();
 }
}

I think that you're facing this issue because Spring attempted to dependency inject the bean before it injected the listener.我认为您正面临此问题,因为 Spring 试图在注入侦听器之前依赖注入 bean。 How about you try declare JdaListener as a bean as well?您也尝试将 JdaListener 声明为 bean 怎么样?

Is JDAListener is marked as @Component/ @Service or something which tells the framework that it is suppose to be treated as a bean? JDAListener 是否被标记为@Component/@Service 或告诉框架它应该被视为 bean 的东西? Also, quickly check your component scan configuration.另外,快速检查您的组件扫描配置。

I am trying to use an @Autowired variable in an @Configuration class, where a bean is created using @Bean in a method.我正在尝试在@Configuration class 中使用@Autowired变量,其中在方法中使用@Bean创建了一个bean。 However the component I need to create the bean with is null .但是,我需要创建 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();
}

The Exception I get is as follows:我得到的异常如下:

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