2017-05-22 105 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中的值。

相关问题