Springboot內建ApplicationListener--AnsiOutputApplicationListener
阿新 • • 發佈:2018-11-19
原始碼分析
本文原始碼基於 Springboot 2.1.0
package org.springframework.boot.context.config;
import org.springframework.boot.ansi.AnsiOutput;
import org.springframework.boot.ansi.AnsiOutput.Enabled;
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
import org.springframework. boot.context.properties.bind.Binder;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.env.ConfigurableEnvironment;
/**
* An ApplicationListener that configures AnsiOutput depending on the
* value of the property spring.output.ansi.enabled. See Enabled for valid
* values.
* 應用啟動過程中當ApplicationEnvironmentPreparedEvent發生時配置AnsiOutput,
* 具體的來說是從環境屬性中按型別AnsiOutput.Enabled取出spring.output.ansi.enabled的值,
* 如果存在的話則是用設定值呼叫類AnsiOutput靜態方法setEnabled(Enabled enabled)。
*
* @author Raphael von der Grün
* @author Madhura Bhave
* @since 1.2.0
*/
public class AnsiOutputApplicationListener
implements ApplicationListener<ApplicationEnvironmentPreparedEvent>, Ordered {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ConfigurableEnvironment environment = event.getEnvironment();
Binder.get(environment)
.bind("spring.output.ansi.enabled", AnsiOutput.Enabled.class)
.ifBound(AnsiOutput::setEnabled);
AnsiOutput.setConsoleAvailable(environment
.getProperty("spring.output.ansi.console-available", Boolean.class));
}
@Override
public int getOrder() {
// Apply after ConfigFileApplicationListener has called EnvironmentPostProcessors
return ConfigFileApplicationListener.DEFAULT_ORDER + 1;
}
}