2017-05-22 108 views

回答

1

您可以通過添加組件監聽ApplicationReadyEvent

@Component 
public class LoadedConfigFileListener implements ApplicationListener<ApplicationReadyEvent>, Ordered { 

    private static final Logger logger = LoggerFactory.getLogger(LoadedConfigFileListener.class); 

    @Override 
    public void onApplicationEvent(ApplicationReadyEvent event) { 
     MutablePropertySources propertySources = event.getApplicationContext().getEnvironment().getPropertySources(); 
     Iterator<PropertySource<?>> propertySourceIterator = propertySources.iterator(); 
     propertySourceIterator.forEachRemaining(propertySource -> logger.info("Successfully loaded: \"{}\" into application context", propertySource.getName())); 
    } 

    @Override 
    public int getOrder() { 
     return ConfigFileApplicationListener.DEFAULT_ORDER + 1; 
    } 
} 
+0

謝謝尼爾森,但我不需要記錄文件內容。我希望檢查文件application-profile.properties是否已成功加載。 – NAZEHA

+0

使用相同的方法,您可以列出或迭代所有加載的屬性源並檢查您喜歡的屬性。 –

+0

謝謝尼爾森。有用。 – NAZEHA

0

24.4配置文件特定性能日誌loaded屬性源

http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-profile-specific-properties

除了應用。屬性文件,配置文件特定屬性也可以使用命名約定applica tion- {}配置文件的.properties。

如果未設置活動配置文件(即,如果沒有顯式激活配置文件,則會加載來自application-default.properties的屬性),環境具有一組默認配置文件(默認情況下爲[缺省值])。

特定於配置文件的屬性從標準application.properties的相同位置加載,配置文件特定的文件始終覆蓋非特定的文件,而不管配置文件特定的文件是在打包的jar內部還是外部。

如果指定了多個配置文件,則應用最後一個贏取策略。例如,由spring.profiles.active屬性指定的配置文件將添加到通過SpringApplication API配置的配置文件之後,因此優先。

例如:你可以在appliation.properties

spring.profiles.active=dev 

所以開發的個人資料將被載入設置配置文件名稱。

1

如果你想調試負載特性,添加以下環境變量(命令行參數啓動Java代碼時):

logging.level.org.springframework.core.env=DEBUG 

您可以在日誌行看到,例如:

2017-05-23 08:37:00.773 DEBUG 26152 --- [   main] o.s.core.env.MutablePropertySources  : Adding [applicationConfig: [file:./application.properties]] PropertySource with lowest search precedence 
... 
2017-05-23 08:37:00.774 DEBUG 26152 --- [   main] o.s.c.e.PropertySourcesPropertyResolver : Found key 'server.port' in [applicationConfig: [file:./application.properties]] with type [String] 
... 
2017-05-23 08:37:02.087 DEBUG 26152 --- [   main] o.s.c.e.PropertySourcesPropertyResolver : Found key 'spring.datasource.url' in [applicationConfig: [file:./application.properties]] with type [String] 
0

什麼彈簧啓動應用程序的初始日誌輸出錯誤?一,當我重新開始彈簧啓動的應用程序開箱的前5行的是:

2017-05-23 23:09:59 INFO e.r.t.MyApplication - No active profile set, falling back to default profiles: default

該日誌輸出告訴我的默認(application.yml)屬性文件加載。對於所有活動的配置文件,將加載相應的屬性文件。

例如,如果這是我的日誌輸出:

2017-05-23 23:14:32 INFO e.r.t.MyApplication - The following profiles are active: cloud, dev, special

然後ALL這些屬性的文件會被加載(注,你可以交換的.properties和。陽明海運):

application.yml

application-cloud.yml

application-dev.yml

application-special.yml

此外,請記住,春天啓動允許屬性壓倒一切的屬性文件讀取的順序,因此最後加載的特性文件勝出。在這種情況下,如果我聲明瞭一個屬性,則在上述所有屬性文件的所有4個文件中將其稱爲my.property,因爲它是最後一個應用的配置文件,所以只會加載application-special.yml中的值。

相關問題