通过配置获取Spring上下文会导致null属性

问题描述

我有一个discordConfiguration类来使用API​​进行操作。要授权我在配置属性中使用令牌。我在这里声明:

@Configuration
@ConfigurationProperties(prefix = "discord")
public class discordConfiguration {

    private long clientId;
    private String clientSecret;
    private String bottoken;

    public long getClientId() {
        return clientId;
    }

    public void setClientId(long clientId) {
        this.clientId = clientId;
    }

    public String getClientSecret() {
        return clientSecret;
    }

    public void setClientSecret(String clientSecret) {
        this.clientSecret = clientSecret;
    }

    public String getBottoken() {
        return bottoken;
    }

    public void setBottoken(String bottoken) {
        this.bottoken = bottoken;
    }

    @Bean
    @Scope("singleton")
    public JDA discordJDA() {
        try {
            return JDABuilder.createDefault(bottoken)
                    .addEventListeners(new MessageListener())
                    .build(); <-- Here's an exception with null token
        } catch (LoginException e) {
            e.printstacktrace();
            return null;
        }
    }

启动应用程序时,我的bean进行了初始化,因此discord身份验证成功(调试器令牌中的值不为null)。我只需要在其他类中获取已初始化的JDA对象,因此将其标记为单例。 这就是我得到豆子的方式:

ApplicationContext discordContext = new AnnotationConfigApplicationContext(discordConfiguration.class);
JDA jda = discordContext.getBean(JDA.class);

当我的discordContext初始化时,我得到一个“令牌空异常”。在调试器中,令牌实际上为空。为什么我的bean第二次初始化并且具有非静态的行为?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)