2012-01-27 74 views
2

我希望自定義對屬性源的處理,同時使用基於初始化彈簧web應用程序的java註釋。爲基於java註釋的配置自定義@PropertyResource處理行爲

@Configuration 
@PropertySource("ldap.properties") 
@Repository 
public class LdapDao { 
... 
    @Autowired 
    public void setEnv(Environment env) throws NamingException { 
    this.url = env.getProperty("url").trim(); 
    this.user = env.getProperty("user").trim(); 
    this.password = env.getProperty("password).trim(); 

    this.initializeLdapContext(); 
    } 
... 
} 

在這種情況下,spring將在classpath中查找屬性源。

@PropertySource("file:/${conf.dir}/ldap.properties") 

ldap.properties由系統屬性「conf.dir」指定的目錄下搜索:如果作爲財產來源聲明。

我需要在系統屬性「conf.dir」指定的目錄下首先搜索屬性資源的行爲。如果沒有找到它,它的位置默認爲classpath。

有關如何實現此行爲的任何建議?

回答

3

使用此

@PropertySource({"ldap.properties", "file:/${conf.dir}/ldap.properties"}) 

,你會得到最後一個,只要添加在你propConfig代碼

PropertySourcesPlaceholderConfigurer propConfig = new PropertySourcesPlaceholderConfigurer(); 
//..... 
propConfig.setIgnoreResourceNotFound(true); 
propConfig.setIgnoreUnresolvablePlaceholders(false); 
propConfig.setLocalOverride(true); 
//....