java spring 03 启动细节

spring启动类ClassPathXmlApplicationContext,读取xml文件并且创建bean

public ClassPathXmlApplicationContext(
			String[] configLocations, boolean refresh, @Nullable ApplicationContext parent)
			throws BeansException {

		super(parent);
		setConfigLocations(configLocations);//configLocations是xml文件
		if (refresh) {
			refresh();
		}
	}

第一步.
01.其中的父类构造方法:加载设置一些资源

super(parent);

02
ClassPathXmlApplicationContext 继承 AbstractXmlApplicationContext:

public class ClassPathXmlApplicationContext extends AbstractXmlApplicationContext

AbstractXmlApplicationContext的构造方法:

	public AbstractXmlApplicationContext(@Nullable ApplicationContext parent) {
		super(parent);
	}

03.AbstractXmlApplicationContext 继承 AbstractRefreshableConfigApplicationContext

AbstractRefreshableConfigApplicationContext的构造方法:

public AbstractRefreshableConfigApplicationContext(@Nullable ApplicationContext parent) {
		super(parent);
	}

04.AbstractRefreshableConfigApplicationContext 继承 AbstractRefreshableApplicationContext

AbstractRefreshableApplicationContext的构造方法:

	public AbstractRefreshableApplicationContext(@Nullable ApplicationContext parent) {
		super(parent);
	}

05.AbstractRefreshableApplicationContext 继承 AbstractApplicationContext

AbstractApplicationContext的构造方法:

public AbstractApplicationContext(@Nullable ApplicationContext parent) {
		this();
		setParent(parent);
	}

public AbstractApplicationContext() {
		this.resourcePatternResolver = getResourcePatternResolver();
	}

getResourcePatternResolver() 解析资源的函数,比如xml文件

protected ResourcePatternResolver getResourcePatternResolver() {
		return new PathMatchingResourcePatternResolver(this);
	}

06.AbstractApplicationContext的相关属性(这个时候加载)

   /** Logger used by this class. Available to subclasses. */
   //日志
	protected final Log logger = LogFactory.getLog(getClass());

	/** Unique id for this context, if any. */
	//唯一标识符
	private String id = ObjectUtils.identityToString(this);

	/** Display name. */
	private String displayName = ObjectUtils.identityToString(this);

    /** Parent context. */
	@Nullable
	private ApplicationContext parent;

	/** Environment used by this context. */
	@Nullable
	private ConfigurableEnvironment environment;

	/** BeanFactoryPostProcessors to apply on refresh. */
	//BeanFactoryPostProcessor的集合
	private final List<BeanFactoryPostProcessor> beanFactoryPostProcessors = new ArrayList<>();

	/** System time in milliseconds when this context started. */
	private long startupDate;

	/** Flag that indicates whether this context is currently active. */
	private final AtomicBoolean active = new AtomicBoolean();

	/** Flag that indicates whether this context has been closed already. */
	private final AtomicBoolean closed = new AtomicBoolean();

	/** Synchronization monitor for the "refresh" and "destroy". */
	private final Object startupShutdownMonitor = new Object();


	@Nullable
	private Thread shutdownHook;

	/** ResourcePatternResolver used by this context. */
	private final ResourcePatternResolver resourcePatternResolver;

	/** LifecycleProcessor for managing the lifecycle of beans within this context. */
	@Nullable
	private LifecycleProcessor lifecycleProcessor;

	/** MessageSource we delegate our implementation of this interface to. */
	@Nullable
	private MessageSource messageSource;

在这里插入图片描述
第二步.setConfigLocations(configLocations) 设置配置文件的路径

setConfigLocations(configLocations);//configLocations是xml文件
	public void setConfigLocations(@Nullable String... locations) {
		if (locations != null) {
			Assert.noNullElements(locations, "Config locations must not be null");
			this.configLocations = new String[locations.length];
			for (int i = 0; i < locations.length; i++) {
			//解析路径
				this.configLocations[i] = resolvePath(locations[i]).trim();
			}
		}
		else {
			this.configLocations = null;
		}
	}

在这里插入图片描述

	protected String resolvePath(String path) {
		return getEnvironment().resolveRequiredPlaceholders(path);
	}

@Override
	public ConfigurableEnvironment getEnvironment() {
		if (this.environment == null) {
			this.environment = createEnvironment();
		}
		return this.environment;
	}
protected ConfigurableEnvironment createEnvironment() {
		return new StandardEnvironment();
	}

public class StandardEnvironment extends AbstractEnvironment {
	public StandardEnvironment() {
	}
}
public abstract class AbstractEnvironment implements ConfigurableEnvironment {
	protected AbstractEnvironment(MutablePropertySources propertySources) {
		this.propertySources = propertySources;
		this.propertyResolver = createPropertyResolver(propertySources);
		customizePropertySources(propertySources);
	}

}

这些属性customizePropertySources设置的属性

public static final String IGNORE_GETENV_PROPERTY_NAME = "spring.getenv.ignore";
public static final String ACTIVE_PROFILES_PROPERTY_NAME = "spring.profiles.active";
public static final String DEFAULT_PROFILES_PROPERTY_NAME = "spring.profiles.default";
protected static final String RESERVED_DEFAULT_PROFILE_NAME = "default";

customizePropertySources在StandardEnvironment

	@Override
	protected void customizePropertySources(MutablePropertySources propertySources) {
		propertySources.addLast(
				new PropertiesPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
		propertySources.addLast(
				new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
	}

进入getSystemProperties方法的(Map) System.getProperties(),获取到了 当前JVM 设置的属性值, Java进程变量

	@Override
	@SuppressWarnings({"rawtypes", "unchecked"})
	public Map<String, Object> getSystemProperties() {
		return (Map) System.getProperties();
	}

在这里插入图片描述

    public static Properties getProperties() {
        SecurityManager sm = getSecurityManager();
        if (sm != null) {
            sm.checkPropertiesAccess();
        }

        return props;
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值