2016-12-06 120 views
1

我嘗試設置有效的個人資料如下:springboot應用程序:如何設置配置文件propery與spel?

Application.properties(內部類路徑)

spring.profile.active = ${app.profile} 

屬性 「app.profile」 來自Application.properties外JAR:

Application.properties文件(外部)

app.profile = dev 

Spring拒絕加載「dev」配置文件。當我在classpath中設置「spring.profile.active」屬性時,它按預期工作。

還有一個選擇嗎?

感謝

回答

0

可以在web.xml文件Web應用程序的設置活動的配置文件。請參考下面的代碼片段:

<context-param> 
    <param-name>spring.profiles.active</param-name> 
    <param-value>dev, testdb</param-value> 
</context-param> 

如果他們沒有web.xml文件和你configuriong使用Java Web應用程序,那麼你可以使用WebApplicationInitializer。請參考下面的代碼片段:

class WebAppInitializer extends WebApplicationInitializer { 

    void onStartup(ServletContext container) { 
     AnnotationConfigWebApplicationContext rootContext = new AnnotationConfigWebApplicationContext(); 
     rootContext.getEnvironment().setActiveProfiles("profileName"); 
     rootContext.register(SpringConfiguration.class); 
     container.addListener(new ContextLoaderListener(rootContext)); 
    } 
} 

WebAppInitializer需要與@Configuration來註釋這個工作。

請讓我知道它是否有幫助,或者您需要更多幫助。

+0

我正在與REST的應用程序只工作。 「WebApplicationInitializer」是正確的方法嗎? – Idan

+0

我的目標是用戶將更改「app.profile」屬性,然後spring.profile.active = $ {app.profile}將設置正確的配置文件。 所有其他外部屬性均按預期工作,除配置文件外。 – Idan

3

您可以在啓動Spring啓動應用程序時指定配置文件名稱。

從CMD運行

java -jar my-first-spring-boot-app.jar -Dspring.profiles.active=dev 

從Eclipse的
運行添加spring.profiles.active=dev作爲VM參數,如果你想親語法設置輪廓

請參閱以下網址。
Spring Boot Programmatically setting profiles

+0

是否可以更改屬性名稱?導致用戶將編輯這些外部屬性 – Idan

+0

沒有屬性不能改變(spring.profiles.active)只有值(dev)是變化的 – Jobin

+0

我的目標是,用戶將更改「app.profile」屬性,然後spring.profile.active = $ {app.profile}將設置正確的配置文件。 – Idan

相關問題