Actuator /env サービスはプロパティを表示しますが、実際にアクティブなプロパティ値は表示しません。非常に頻繁に、アプリケーションのプロパティをオーバーライドしたい場合があります。
- プロファイル固有のアプリケーション プロパティ
- コマンドライン引数
- OS 環境変数
したがって、複数のソースで同じプロパティと異なる値を持つことになります。
以下のスニペットは、起動時にアクティブなアプリケーション プロパティの値を出力します。
@Configuration
public class PropertiesLogger {
private static final Logger log = LoggerFactory.getLogger(PropertiesLogger.class);
@Autowired
private AbstractEnvironment environment;
@PostConstruct
public void printProperties() {
log.info("**** APPLICATION PROPERTIES SOURCES ****");
Set<String> properties = new TreeSet<>();
for (PropertiesPropertySource p : findPropertiesPropertySources()) {
log.info(p.toString());
properties.addAll(Arrays.asList(p.getPropertyNames()));
}
log.info("**** APPLICATION PROPERTIES VALUES ****");
print(properties);
}
private List<PropertiesPropertySource> findPropertiesPropertySources() {
List<PropertiesPropertySource> propertiesPropertySources = new LinkedList<>();
for (PropertySource<?> propertySource : environment.getPropertySources()) {
if (propertySource instanceof PropertiesPropertySource) {
propertiesPropertySources.add((PropertiesPropertySource) propertySource);
}
}
return propertiesPropertySources;
}
private void print(Set<String> properties) {
for (String propertyName : properties) {
log.info("{}={}", propertyName, environment.getProperty(propertyName));
}
}
}