i有一个应用程序.yml文件,其对象列表如下:
outlook:
mailboxes:
- id: m1
name: mailbox1
- id: m2
name: mailbox2
我创建了一个称为 mailboxproperties 的春季配置类,将这些属性在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());
}
}
}
但是,邮箱杂物始终是null的,并从getMailBoxProperties拨打邮件Boxproperties.getMailboxes()。
(我尝试给予@configuration @enableconfigurationProperties(MailboxProperties.Class)。它引发了一个例外,指出创建了2个豆。一个带有实际路径,另一个带有空路)。我尝试过@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
谢谢。
基本上您的 MailBoxProperties
应该如@varren所建议的。并且无需用@Configuration
注释此属性类。
so:
@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;
}
}
}
然后,您可以用@Configuration
注释OutlookConnectionManager
,但是您想在其中做什么是一个Anorthodox(您正在使用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;
}
正如 @m-deinum指出的那样,属性仍然为空。尝试以@PostConstruct
注释的方法进行初始化。所有自动的字段都应由该点填充。
有关@PostConstruct
的更多详细信息:bean初始化完成后如何调用方法?
我认为有两件事可以解决:
首先,您在 MailBoxProperties
中有一些奇怪的getter和setter名称(例如field mailboxes
的 CC_11应该是 setMailboxes
),而且我不认为邮箱boxproperties会从yaml文件中获取带有自定义setter名称的值。使用默认的Geters和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提到您正在尝试在Spring之前打电话给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;
}
}