2015-05-11 125 views
2

我目前正在嘗試將現有的彈簧應用程序移動到彈簧引導,因此重新創建沒有引導的工作。如何將彈簧引導的數據源配置外部化?

我想從外部來源配置一些屬性(如spring.datasource。*)。具體來說就是具有多個屬性文件的文件夾

我成立了創建propertyPlaceholder configurers這樣的配置類:

@Configuration 
public class PropertySourceConfiguration { 

@Bean 
public static PropertySourcesPlaceholderConfigurer defaultsPlaceHolderConfigurer() throws IOException { 
    PropertySourcesPlaceholderConfigurer propertyConfigurer = new PropertySourcesPlaceholderConfigurer(); 
    propertyConfigurer.setLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:/*-defaults.properties")); 
    propertyConfigurer.setIgnoreUnresolvablePlaceholders(true); 
    return propertyConfigurer; 
} 

@Bean 
public static PropertySourcesPlaceholderConfigurer externalPlaceHolderConfigurer() throws IOException { 
    PropertySourcesPlaceholderConfigurer propertyConfigurer = new PropertySourcesPlaceholderConfigurer(); 
    propertyConfigurer.setLocations(new 
      PathMatchingResourcePatternResolver().getResources("file:/my-config-path/*.properties")); 
    propertyConfigurer.setOrder(1); 
    propertyConfigurer.setIgnoreUnresolvablePlaceholders(true); 
    return propertyConfigurer; 
} 

這似乎對大多數事情的工作(如AMQP還是我自己配置​​屬性),但是當我嘗試使用彈簧數據 - 他們被忽略了。基本上在這些文件中設置spring.datasource.url(和其他用於自動配置的東西)不起作用。

通過查看PropertySourcesPropertyResolver的日誌,我發現這些配置器屬於localProperties組,在尋找spring.datasource.*時未使用該組。

有沒有辦法解決這個問題或更好的方式來添加外部屬性文件到我的上下文?

我知道我可以設置spring.config.location做類似的事情,但我無法將命令行屬性傳遞給我的應用程序,並且需要在我的應用程序中執行此配置。 afaik這是不可能的這個屬性。

編輯:設置spring.config.location

嘗試1:

public class ServletInitializer extends SpringBootServletInitializer { 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(CampaignServiceStarter.class); 
    } 
    @Override 
    public void onStartup(ServletContext servletContext) throws ServletException { 
     super.onStartup(servletContext); 
     servletContext.setInitParameter("spring.config.location", "file:/my-config-path/*.properties"); 
    } 
} 

嘗試2:

public class ServletInitializer extends SpringBootServletInitializer { 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(CampaignServiceStarter.class).properties("spring.config.location=file:/my-config-path/*.properties"); 
    } 
} 
在兩種情況下,外部性質沒有在所有拾取

(甚至在之前工作過的地方,比如amqp config)

+0

只需將「@ PropertySource」添加到您的配置中,請勿添加額外的「PropertySourcesPlaceholderConfigurer」。或者添加一個'ApplicationContextInitializer',添加其他屬性來源。但是你目前的解決方案不會起作用。另外,爲什麼不把所有東西放在'application.properties'中,並讓spring引導管理一切? –

+0

爲什麼不是所有'applicaton.properties'文件:因爲不同的環境(如us/eu/staging,...)需要不同的配置。有沒有辦法與佔位符做到這一點? '@ PropertySources'不支持這個。 – Laures

+0

然後添加'application-us.properties'。 Spring配置文件和彈簧引導利用它來加載配置文件特定的屬性文件。此外,您的application.properties可能很容易因環境而異。我強烈建議閱讀Spring部分參考指南的[本節](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config)。 [第23.3節](http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-application-property-files)解釋了加載順序。 –

回答

2

Spring Boot參考指南的this section中介紹瞭如何使用外部配置。

spring.config.location是包含您的application.properties文件的目錄的路徑。它使用逗號分隔的值列表,以便您可以指定多個路徑。它不需要通配符。這是一個路徑,所以不是一個匹配多個屬性文件的表達式。如果你想改變默認的application然後使用spring.config.name使其他東西。

Spring Boot的默認設置被視爲Spring Boot的其餘部分(使用默認配置等)。

如果要進行更廣泛的(預)配置,則應使用ApplicationContextInitializer手動將PropertySource添加到Environment。這在Spring Boot參考指南中提到here

初始值設定項的外觀示例。

然後當建立你的應用程序對象時,按如下方式添加它。

public class ServletInitializer extends SpringBootServletInitializer { 

    @Override 
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) { 
     return application.sources(CampaignServiceStarter.class) 
      .initializers(new ConfigurationInitializer()); 
    } 
} 

現在應該將configs添加到屬性源列表中。

+0

此解決方案的問題是,日誌記錄屬性將被忽略,並且日誌記錄系統(如logback)將不起作用。我正在處理這個問題,並找到了一個也加載日誌記錄屬性的解決方案,基本上'spring.config.location'必須在配置Spring Boot時設置,你可以在這裏檢查它[http:// stackoverflow .COM /問題/ 31017064 /如何對外部化 - 彈簧 - 啓動應用程序性能到Tomcat的LIB文件夾) –