簡體   English   中英

在Spring Java配置的注釋中使用占位符

[英]Using placeholders in Annotations on Spring Java Configurations

我對Spring的Property Replacement機制有些迷茫。 可以說我有這個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批注@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實例?

任何幫助都非常感謝! 我有餅干給你! :-))

干杯,斯特凡

我想象@PropertySources (您只有一個,所以您不需要包裝器) 在處理了config類之后添加到Environment中,因此現在解決這些注釋之一將為時已晚。自己上課。 您可以通過將my.custom.repobasepackage設置為System屬性來驗證這一點。

另外,我鼓勵您嘗試使用Spring Boot(在處理任何配置之前,將application.properties添加到Environment )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM