第1ばねブートバージョンは1.5.9であるもののいくつかの中で最もシンプルな春のブート環境を見てください
@RequestMapping(value = "/hello")
@RestController
public class HelloWorld {
@Autowired
private AbstractEnvironment environment;
@RequestMapping(value = "/world", method = RequestMethod.GET)
public String world() {
System.out.println(environment);
System.out.println(environment.getSystemEnvironment());
System.out.println(environment.getSystemProperties());
for (String s : environment.getActiveProfiles()){
System.out.println(s);
}
System.out.println("qwe");
for (String s : environment.getDefaultProfiles()){
System.out.println(s);
}
return "qwe";
}
}
テストコード
StandardServletEnvironment {activeProfiles = []、defaultProfiles = [デフォルト]、propertySources = [MapPropertySource {NAME = 'server.ports'}、StubPropertySource {NAME = 'servletConfigInitParams'}、ServletContextPropertySource {NAME = 'servletContextInitParams'}、MapPropertySource {NAME =」 systemProperties '}、SystemEnvironmentPropertySource {NAME =' systemEnvironment '}、RandomValuePropertySource {NAME =' ランダム '}、PropertiesPropertySource {NAME =' applicationConfig:[クラスパス:/application.properties] '}]}
これは、すべての変数内の環境では、これらの環境変数が生成されているところを見てみましょう。まず、この方法は、実行から入力することができる環境を生成するために使用されるprepareEnvironment方法を見ることができます。
private ConfigurableEnvironment prepareEnvironment(SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments) {
ConfigurableEnvironment environment = this.getOrCreateEnvironment();
this.configureEnvironment((ConfigurableEnvironment)environment, applicationArguments.getSourceArgs());
listeners.environmentPrepared((ConfigurableEnvironment)environment);
if (!this.webEnvironment) {
environment = (new EnvironmentConverter(this.getClassLoader())).convertToStandardEnvironmentIfNecessary((ConfigurableEnvironment)environment);
}
return (ConfigurableEnvironment)environment;
}
3つのメソッドだけを見てgetOrCreateEnvironment、configureEnvironment、environmentPreparedは、getOrCreateEnvironmentを見てみることができます
private ConfigurableEnvironment getOrCreateEnvironment() {
if (this.environment != null) {
return this.environment;
} else {
return (ConfigurableEnvironment)(this.webEnvironment ? new StandardServletEnvironment() : new StandardEnvironment());
}
}
ここで唯一のステップ、新しいStandardServletEnvironment()。そして、初期化されるもののいくつかを見てみましょう
StandardServletEnvironment-> StandardEnvironment-> AbstractEnvironment。これは継承され、コンストラクタAbstractEnvironmentで初期化を行って
public AbstractEnvironment() {
this.propertySources = new MutablePropertySources(this.logger);
this.propertyResolver = new PropertySourcesPropertyResolver(this.propertySources);
this.customizePropertySources(this.propertySources);
if (this.logger.isDebugEnabled()) {
this.logger.debug("Initialized " + this.getClass().getSimpleName() + " with PropertySources " + this.propertySources);
}
}
そして、初期化するcustomizePropertySources方法はStandardServletEnvironmentに方法を見つけました
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(new StubPropertySource("servletConfigInitParams"));
propertySources.addLast(new StubPropertySource("servletContextInitParams"));
if (JndiLocatorDelegate.isDefaultJndiEnvironmentAvailable()) {
propertySources.addLast(new JndiPropertySource("jndiProperties"));
}
super.customizePropertySources(propertySources);
}
ここでは、サーブレットのいくつかの初期化パラメータ。のは、親クラスのメソッドを見てみましょう、親クラスを呼び出す方法もあります
protected void customizePropertySources(MutablePropertySources propertySources) {
propertySources.addLast(new MapPropertySource("systemProperties", this.getSystemProperties()));
propertySources.addLast(new SystemEnvironmentPropertySource("systemEnvironment", this.getSystemEnvironment()));
}
ここでの発見は、システム環境変数を初期化します。
これまでのところ、初期の大部分が完了しました。
そして、ルックconfigureEnvironmentを取ります
protected void configureEnvironment(ConfigurableEnvironment environment, String[] args) {
this.configurePropertySources(environment, args);
this.configureProfiles(environment, args);
}
第一の方法に
protected void configurePropertySources(ConfigurableEnvironment environment, String[] args) {
MutablePropertySources sources = environment.getPropertySources();
if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
sources.addLast(new MapPropertySource("defaultProperties", this.defaultProperties));
}
if (this.addCommandLineProperties && args.length > 0) {
String name = "commandLineArgs";
if (sources.contains(name)) {
PropertySource<?> source = sources.get(name);
CompositePropertySource composite = new CompositePropertySource(name);
composite.addPropertySource(new SimpleCommandLinePropertySource(name + "-" + args.hashCode(), args));
composite.addPropertySource(source);
sources.replace(name, composite);
} else {
sources.addFirst(new SimpleCommandLinePropertySource(args));
}
}
}
ここで初期化するためのコマンドラインパラメータであります
最後に、environmentPrepared、本質的ConfigFileApplicationListenerクラスを見て
放送、およびonApplicationEnvironmentPreparedEventを呼び出すとき
private void onApplicationEnvironmentPreparedEvent(ApplicationEnvironmentPreparedEvent event) {
List<EnvironmentPostProcessor> postProcessors = this.loadPostProcessors();
postProcessors.add(this);
AnnotationAwareOrderComparator.sort(postProcessors);
Iterator var3 = postProcessors.iterator();
while(var3.hasNext()) {
EnvironmentPostProcessor postProcessor = (EnvironmentPostProcessor)var3.next();
postProcessor.postProcessEnvironment(event.getEnvironment(), event.getSpringApplication());
}
}
この方法は、postProcessEnvironmentに入りました
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
this.addPropertySources(environment, application.getResourceLoader());
this.configureIgnoreBeanInfo(environment);
this.bindToSpringApplication(environment, application);
}
メソッドの入ったaddPropertySources
protected void addPropertySources(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
RandomValuePropertySource.addToEnvironment(environment);
(new ConfigFileApplicationListener.Loader(environment, resourceLoader)).load();
}
ここRandomValuePropertySourceは、環境変数を追加しました
それから彼は最後のロード.ymlと.propertiesファイルを送りました。
負荷()メソッドに
public void load() {
this.propertiesLoader = new PropertySourcesLoader();
this.activatedProfiles = false;
this.profiles = Collections.asLifoQueue(new LinkedList());
this.processedProfiles = new LinkedList();
Set<ConfigFileApplicationListener.Profile> initialActiveProfiles = this.initializeActiveProfiles();
this.profiles.addAll(this.getUnprocessedActiveProfiles(initialActiveProfiles));
if (this.profiles.isEmpty()) {
String[] var2 = this.environment.getDefaultProfiles();
int var3 = var2.length;
for(int var4 = 0; var4 < var3; ++var4) {
String defaultProfileName = var2[var4];
ConfigFileApplicationListener.Profile defaultProfile = new ConfigFileApplicationListener.Profile(defaultProfileName, true);
if (!this.profiles.contains(defaultProfile)) {
this.profiles.add(defaultProfile);
}
}
}
this.profiles.add((Object)null);
ConfigFileApplicationListener.Profile profile;
label41:
for(; !this.profiles.isEmpty(); this.processedProfiles.add(profile)) {
profile = (ConfigFileApplicationListener.Profile)this.profiles.poll();
Iterator var8 = this.getSearchLocations().iterator();
while(true) {
while(true) {
if (!var8.hasNext()) {
continue label41;
}
String location = (String)var8.next();
if (!location.endsWith("/")) {
this.load(location, (String)null, profile);
} else {
Iterator var10 = this.getSearchNames().iterator();
while(var10.hasNext()) {
String name = (String)var10.next();
this.load(location, name, profile);
}
}
}
}
}
this.addConfigurationProperties(this.propertiesLoader.getPropertySources());
}
最下位置に見えることサイクルに必要なファイルは、順序がその行ダウンようになっている場所です。ファイル:./設定/ファイル:./、クラスパス:/設定/、クラスパス:/。名前はアプリケーションであり、プロファイルは、2つのアプリケーション・default.ymlに対応する「デフォルト」であってもよいです。
loadメソッドの内部を見てください
private void load(String location, String name, ConfigFileApplicationListener.Profile profile) {
String group = "profile=" + (profile == null ? "" : profile);
String ext;
if (!StringUtils.hasText(name)) {
this.loadIntoGroup(group, location, profile);
} else {
for(Iterator var5 = this.propertiesLoader.getAllFileExtensions().iterator(); var5.hasNext(); this.loadIntoGroup(group, location + name + "." + ext, profile)) {
ext = (String)var5.next();
if (profile != null) {
this.loadIntoGroup(group, location + name + "-" + profile + "." + ext, (ConfigFileApplicationListener.Profile)null);
Iterator var7 = this.processedProfiles.iterator();
while(var7.hasNext()) {
ConfigFileApplicationListener.Profile processedProfile = (ConfigFileApplicationListener.Profile)var7.next();
if (processedProfile != null) {
this.loadIntoGroup(group, location + name + "-" + processedProfile + "." + ext, profile);
}
}
this.loadIntoGroup(group, location + name + "-" + profile + "." + ext, profile);
}
}
}
}
this.loadIntoGroup(群、位置+名+「」+内線、プロファイル)の内部を見ることができるforループ
application.propertiesがここに初期化されます