我尝试逐步迁移旧的应用程序,从从Spring 4.0迁移到4.1开始。 虽然文档说没有重大变化,但似乎不然。
我的集成测试看起来有点像这样:
public class CsvResourceSplitter extends AbstractMessageSplitter {
private final static Logger LOGGER = LoggerFactory.getLogger(CsvResourceSplitter.class);
@Autowired
private ApplicationContext applicationContext;
private Map<Partner, CsvMappingStrategy> csvMappingMap;
...
private void updateCsvMappings() {
final Map<String, CsvMappingStrategy> mappingBeanMap = applicationContext.getBeansOfType(CsvMappingStrategy.class,
false, true);
csvMappingMap = Maps.newEnumMap(Partner.class);
for (final CsvMappingStrategy csvMappingStrategy : mappingBeanMap.values()) {
final CsvMappingStrategy previous = csvMappingMap.put(csvMappingStrategy.getPartner(),
csvMappingStrategy);
Preconditions.checkArgument(previous == null, "More than one CsvMapping bean found for partner: '%s",
csvMappingStrategy.getPartner());
}
}
}
csvResourceProcessingChannel Bean将帖子访问到拆分器bean CsvResourceSplitter,其定义如下:
@ContextConfiguration
public class CsvResourceSplitterTest extends AbstractJUnit4SpringContextTests {
@Configuration
static class Config {
@Bean
public CsvResourceSplitter createCsvResourceSplitter() {
return new CsvResourceSplitter();
}
@Bean
public CsvMappingStrategy createTestMappingStrategy() {
return new AbstractCsvMappingStrategy() {
...
};
}
...
}
@Autowired
private CsvResourceSplitter splitter;
...
}
使用updateCsvMappings中的最终前置条件检查,问题变得明显: 虽然测试只定义了一个CsvMappingStrategy类,但自从从Spring 4.0升级到4.1以来,还发现了几个包括重复的类。 由于在运行时期间mappingBeanMap的内容,我非常确定这里使用的上下文包含CsvResourceSplitterTest中使用的上下文元素,看起来有点像这样:
{{1}}
对于出了什么问题的任何想法都表示赞赏。
答案 0 :(得分:0)
一个有用的解决方法是将CsvResourceSplitterTest上下文移动到另一个配置文件:
@ContextConfiguration
@ActiveProfiles("anonymous_profile")
public class CsvResourceSplitterTest extends AbstractJUnit4SpringContextTests {
@Profile("anonymous_profile")
@Configuration
static class Config {
...
}