2012-05-10 33 views
3

如果我有一個PropertyPlaceholderConfigurer通過XML文件傳入,是否可以讓我的Spring @Configuration使用它作爲它處理的所有bean的屬性來源?屬性與Spring @Configuration

@Configuration 
@ComponentScan(value = { "x.y.z }) 
@ImportResource({ "classpath:remote-properties/applicationContext.xml",}) 
public class CoreConfiguration implements TransactionManagementConfigurer { 

    @Resource(name = "com.c.h.c.PropertyPlaceholderConfigurer") 
    public PropertyPlaceholderConfigurer pp; 

    @Bean 
    public PropertyPlaceholderConfigurer propertiesFactoryBean() { 
     return pp; 
    } 
} 

通過上述,它從來沒有擊中我的pp斷點。如果我刪除@Bean和該方法,我可以驗證pp已填充。那麼我怎麼才能使用配置註冊呢?

+0

您是否瀏覽過http://forum.springsource.org/showthread.php?107194-PropertyPlaceholderConfigurer-and-javconfig –

+0

@Rob我無法直接使用屬性文件。我必須從該應用內容中使用PropertyPlaceholderConfigurer bean,或者加載具有第二個屬性文件的url的屬性文件,然後加載該屬性文件。我知道這聽起來很荒謬,但這是我解決多個環境問題的團隊。 – AHungerArtist

+0

@Rob更重要的是,我不僅需要'@Configuration'類本身的屬性,還可以在'@ComponentScan'對'@Service'註釋類執行魔術時使用它們(所以'@Value'註釋將會得到正確填充)。 – AHungerArtist

回答

3

我覺得很愚蠢。我錯過了我的@Value註釋中的一個右大括號。我無法想象我曾看過多少次而錯過了它。

因此,如果在應用上下文中有PropertyPlaceHolderConfigurer,那麼您的@ImportResource將無任何問題。你甚至不需要把它作爲@Resource