我正在开发一个Spring 4.0 REST应用程序,它工作正常,但是当我尝试进行一些测试时,它无法从占位符中读取有效值(如果我正常运行应用程序,它们可以正常工作)
应用程序的某些属性来自文件和其他数据库,所以我已经配置了一个PropertiesPropertySource来从db中读取它们。我无法通过xml配置它,所以我在@Configuration类中完成了它:
public class AppConfig {
private static final Logger logger = LoggerFactory.getLogger(AppConfig.class);
@Inject
private org.springframework.core.env.Environment env;
@Autowired
private DataSource dataSource;
@PostConstruct
public void initializeDatabasePropertySourceUsage() {
MutablePropertySources propertySources = ((ConfigurableEnvironment) env).getPropertySources();
try {
DatabaseConfiguration databaseConfiguration = new DatabaseConfiguration(dataSource, "[TABLE]", "property", "value");
CommonsConfigurationFactoryBean commonsConfigurationFactoryBean = new CommonsConfigurationFactoryBean(databaseConfiguration);
Properties dbProps = (Properties) commonsConfigurationFactoryBean.getObject();
PropertiesPropertySource dbPropertySource = new PropertiesPropertySource("dbPropertySource", dbProps);
propertySources.addFirst(dbPropertySource);
} catch (Exception e) {
logger.error("Error during database properties setup:"+e.getMessage(), e);
throw new RuntimeException(e);
}
}
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
return new PropertySourcesPlaceholderConfigurer();
}
}
在测试类中,我使用此批注加载上下文配置: @ContextConfiguration(classes = {TestConfig1.class,AppConfig.class,TestConfig2.class})
由于我正在混合XML和java配置,我必须创建TestConfigX类,它使用此批注加载XML: @ImportResource([PATH TO config xml])
如果省略TestConfig2,我可以毫无问题地在测试中使用DB中的占位符。但显然我没有测试任何东西。
我做错了什么?