我有点迷失在 Spring 的属性替换机制中。假设我有这个 Java Config
@Configuration
@ComponentScan(basePackageClasses = Application.class)
@PropertySources({
@PropertySource("classpath:/config/default.properties")
})
public class ApplicationConfig {
@Bean
public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer() {
PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
return pspc;
}
现在我想添加一个 Spring-Data Annotation@EnableMongoRepositories
并定义一个用于扫描的自定义基本包,使用自定义占位符,如下所示
@EnableMongoRepositories("${my.custom.repobasepackage}")
。占位符在我的 default.properties 中定义。
但是,这里无法解析此属性。当深入研究 Spring 的属性替换时,我可以看到它试图解析属性,因此可以这样做。
但是,用于替换占位符的底层Environment
类不知道我的 PropertyPlaceholderConfigurer,而只知道我的 SystemProperties 和我的 VM-Props。:-(
我可以看到在 org.springframework.context.annotation.ClassPathBeanDefinitionScanner#getOrCreateEnvironment.java#339 (我正在使用 Spring 4.0.1)我的“PropertyPlaceholder”已经到位,所以它不是初始化排序的问题,而是没有使用,因为使用的 BeanDefinitionRegistry 没有实现 Interface EnvironmentCapable
。到这里,我对 Spring App-Context Bootstrapping 的理解到此结束。
有人可以帮我吗?是否有一个 BeanDefinitionRegistry 能够提供Environment
使用我的属性占位符的实例?
任何帮助都非常感谢!我给你准备了饼干!:-))
干杯,斯特凡