我在" dev"下面有一个豆子。配置文件:
@Bean(name="internalDL")
public Map<Client, List<String>> internalDistributionLists() {
Map<Client, List<String>> distributionLists = new HashMap<Client, List<String>>();
distributionLists.put(Client.DL_NAME, Arrays.asList("example@blah.com"));
return distributionLists;
}
我需要在另一个bean中使用这个bean:
@Autowired
@Qualifier("internalDL")
private Map<String, List<String>> internalMailingList;
@Bean(name="internalMailingDetails")
public Map<Client, MailingDetails> internalMailingDetails() {
Map<Client, MailingDetails> internalMailingDetails = new HashMap<Client, MailingDetails>();
MailingDetails details = new MailingDetails();
details.setMailingList(internalMailingList.get(Client.DL_NAME));
details.setTemplateSubject("someTemplate");
details.setTemplateBody("someTemplate");
internalMailingDetails.put(Client.DL_NAME, details);
return internalMailingDetails;
}
现在所有环境的邮件模板都是相同的,所以我没有把它放在一个标有特定配置文件的配置中。
我尝试通过初始化ApplicationContextInitializer<ConfigurableApplicationContext>
类来初始化配置文件:
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.getEnvironment().setActiveProfiles("dev");
}
并将其作为上下文参数:
<context-param>
<param-name>spring.profiles.active</param-name>
<param-value>dev</param-value>
</context-param>
它们似乎都不起作用。它不断抛出一个未找到的bean异常。
org.springframework.beans.factory.BeanCreationException:无法自动装配字段:private java.util.Map com.fmr.bpo.asyncprocessingframework.invocator.wiring.configuration.pojo.common.RootConfig.internalMailingList
感谢您的帮助。
答案 0 :(得分:2)
@Autowired不适用于集合bean。使用@Resource
代替@Autowire
@Resource
private Map<String, List<String>> internalMailingList;
并参考beans-property-is-not-setting-from-utillist-object
如果您以后使用Spring 4.x,请查看spring-framework-4-0-and-java-generics