1. 程式人生 > >Springboot內建ApplicationContextInitializer--ServerPortInfoApplicationContextInitializer

Springboot內建ApplicationContextInitializer--ServerPortInfoApplicationContextInitializer

原始碼分析

本文程式碼基於 Springboot 2.1.0

package org.springframework.boot.web.context;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.server.WebServer;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import
org.springframework.core.env.MapPropertySource; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.util.StringUtils; /** * ApplicationContextInitializer that sets Environment properties for the * ports that WebServer servers are actually listening on. The property * "local.server.port" can be injected directly into tests using * Value @Value or obtained via the Environment. * * 即是一個ApplicationContextInitializer也是一個ApplicationListener,應用上下文 * 初始化時將自己作為一個ApplicationListener註冊到應用上下文,關注事件WebServerInitializedEvent, * 在該事件發生時向環境屬性物件中新增一個屬性物件"local.server.port",值是 * WebServer當前使用的監聽埠。這樣的話,"local.server.port"就可以通過註解@Value * 的方式直接注入到測試中,或者通過Environment物件獲得: * environment.getProperty("local.server.port") * * If the WebServerInitializedEvent has a * WebServerApplicationContext#getServerNamespace() server namespace , it will be * used to construct the property name. For example, the "management" actuator context * will have the property name "local.management.port". * * Properties are automatically propagated up to any parent context. * * @author Dave Syer * @author Phillip Webb * @since 2.0.0 */
public class ServerPortInfoApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>, ApplicationListener<WebServerInitializedEvent> { @Override public void initialize(ConfigurableApplicationContext applicationContext) { // 應用上下文初始化過程中將自己作為一個ApplicationListener註冊到應用上下文, // 關注的事件是WebServerInitializedEvent applicationContext.addApplicationListener(this); } @Override public void onApplicationEvent(WebServerInitializedEvent event) { // 作為ApplicationListener時的任務:WebServerInitializedEvent 事件發生時, // 將當前Web伺服器監聽埠設定到環境屬性local.server.port(通常是這個名字,從下面 的程式碼來看,也有可能是其他名字) String propertyName = "local." + getName(event.getApplicationContext()) + ".port"; setPortProperty(event.getApplicationContext(), propertyName, event.getWebServer().getPort()); } private String getName(WebServerApplicationContext context) { // 如果制定了名字使用指定的名字否則使用預設名稱 server String name = context.getServerNamespace(); return StringUtils.hasText(name) ? name : "server"; } private void setPortProperty(ApplicationContext context, String propertyName, int port) { if (context instanceof ConfigurableApplicationContext) { // 將埠資訊設定到應用上下文的環境屬性中去 setPortProperty(((ConfigurableApplicationContext) context).getEnvironment(), propertyName, port); } if (context.getParent() != null) { // 如果當前應用上下文的雙親應用上下文存在,則把該屬性設定向上傳播 setPortProperty(context.getParent(), propertyName, port); } } @SuppressWarnings("unchecked") private void setPortProperty(ConfigurableEnvironment environment, String propertyName, int port) { // 獲取環境物件environment中的屬性源server.ports,如果不存在則先建立它 MutablePropertySources sources = environment.getPropertySources(); PropertySource<?> source = sources.get("server.ports"); if (source == null) { source = new MapPropertySource("server.ports", new HashMap<>()); sources.addFirst(source); } // 將埠名稱/值設定到環境物件environment的屬性源server.ports中去 ((Map<String, Object>) source.getSource()).put(propertyName, port); } }

相關文章

Springboot 內建ApplicationContextInitializer