自动装配不适用于Spring

时间:2017-09-19 17:12:37

标签: spring spring-boot autowired spring-bean spring-config

我有一个application.yml文件,其中包含以下对象列表:

outlook:
  mailboxes:
    - id: m1
      name: mailbox1

    - id: m2
      name: mailbox2

我创建了一个名为 MailBoxProperties 的spring配置类,以便在bean中包含这些属性,如下所示:

MailBoxProperties.java

@ConfigurationProperties(prefix = "outlook")
@Configuration
public class MailBoxProperties {

    private List<MailBox> mailboxes;

    public MailBoxProperties() {

    }

    public MailBoxProperties(List<MailBox> mailboxes) {
        this.mailboxes = mailboxes;
    }

    public void setMailBoxes(List<MailBox> mailboxes) {
        this.mailboxes = mailboxes;
    }

    public List<MailBox> getMailBoxes() {
        return mailboxes;
    }

    public static class MailBox {
        public String getName() {
            return this.name;
        }

        public String getId() {
            return this.id;
        }


        private String id, name;

        public MailBox() {

        }

        public MailBox(String id, String name) {
            this.id = id;
            this.name = name;
        }
    }
}

我想将上面的配置bean注入另一个配置类,如下所示:

OutlookConnectionManager.java

@Configuration
@EnableConfigurationProperties
public class OutlookConnectionManager{
  @Autowired
    private MailBoxProperties mailBoxProperties;

    private List<String> names;

    @Bean
    public OutlookConnectionManager getOutlookConnectionManager() {
        OutlookConnectionManager outlookConnectionManager = new OutlookConnectionManager();
        outlookConnectionManager.getMailBoxProperties();
        return outlookConnectionManager;
    }

    public void getMailBoxProperties() {
        names = new ArrayList<String> ();
        for(MailBox mail: mailBoxProperties.getMailBoxes()) {
              this.names.add(mail.getName());
        }
    }
}

但是,MailBoxProperties始终为null,并在从getMailBoxProperties调用mailBoxProperties.getMailBoxes()时抛出空指针异常。

(我试过给@Configuration     @EnableConfigurationProperties(MailBoxProperties.class)。它抛出一个异常,说明创建了2个bean。一个是实际路径,另一个是null)。我试过@Import(){MailBoxProperties.class}。

但是,注入完全适用于我的主要应用程序类,如下所示: 它获取bean并正确打印邮箱名称。

MailApplication.java

@SpringBootApplication
public class MailApplication {

    public static void main(String[] args) {
        ApplicationContext context = new SpringApplicationBuilder(MailApplication.class).run(args);
MailBoxProperties props = context.getBean(MailBoxProperties.class);
props. getMailBoxes()
                .forEach(cc -> System.out.println(cc.getName()));

}
}

我是否遗漏了OutlookConnectionManager.java中的任何配置?请帮忙。

注意: 我正在使用Spring boot 1.5.7.RELEASE

谢谢。

3 个答案:

答案 0 :(得分:2)

基本上你的MailBoxProperties应该像@varren建议的那样。并且不需要使用@Configuration此属性类进行注释。

所以:

@ConfigurationProperties(prefix = "outlook")
public class MailBoxProperties {

    private List<MailBox> mailboxes;

    public List<MailBox> getMailboxes() {
        return mailboxes;
    }

    public void setMailboxes(List<MailBox> mailboxes) {
        this.mailboxes = mailboxes;
    }

    public static class MailBox {
        private String id, name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

然后你可以保持OutlookConnectionManager注释@Configuration但是你想在其中做什么是一点点正统(你用new重新实例化相同的配置类关键字)。

但是,您可以通过将@Bean方法更改为:

来使其正常工作

<强> OutlookConnectionManager

@Bean
public OutlookConnectionManager getOutlookConnectionManager() {
    OutlookConnectionManager outlookConnectionManager = new OutlookConnectionManager();
    //this call will set this.names
    this.getMailBoxProperties();
    //set this.names to the names variable of outlookConnectionManager instance
    outlookConnectionManager.names = this.names;
    return outlookConnectionManager;
}

答案 1 :(得分:0)

正如@m-deinum所指出的那样,属性仍为空。尝试在使用@PostConstruct注释的方法中进行初始化。应该填充所有自动装配的字段。

此处有关@PostConstruct的更多详细信息:How to call a method after bean initialization is complete?

答案 2 :(得分:0)

我认为你可以解决两件事:

首先,您在MailBoxProperties中有一些奇怪的getter和setter名称(如setMailBoxes字段mailboxes应为setMailboxes),我不认为MailBoxProperties将使用自定义setter名称从yaml文件中获取值。使用默认的getter和setter

@ConfigurationProperties("outlook")
public class MailBoxProperties {

    private List<MailBox> mailboxes;

    public List<MailBox> getMailboxes() {
        return mailboxes;
    }

    public void setMailboxes(List<MailBox> mailboxes) {
        this.mailboxes = mailboxes;
    }

    public static class MailBox {
        private String id, name;

        public String getId() {
            return id;
        }

        public void setId(String id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }
    }
}

其次,我不知道OutlookConnectionManager中你的目标是什么,而是作为@M。 Deinum the manual您试图在春天更改为自动连接字段之前调用getMailBoxProperties

我认为你可以用

实现类似的行为
@Component
public class OutlookConnectionManager{

    private final MailBoxProperties mailBoxProperties;

    private List<String> names;

    @Autowired
    public OutlookConnectionManager(MailBoxProperties mailBoxProperties) {
        this.mailBoxProperties = mailBoxProperties;
        this.names = getNames();
    }


    public List<String> getNames() {
        List<String> names = new ArrayList<>();
        for(MailBoxProperties.MailBox mail: mailBoxProperties.getMailboxes()) {
            names.add(mail.getName());
        }
        return names;
    }
}