springboot 启动流程(二)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/baofengyu90/article/details/85698938

8、接着上一节,继续

在步骤7里面提到,父类AbstractEnvironment里面的构造函数

	public AbstractEnvironment() {
		customizePropertySources(this.propertySources);
		if (logger.isDebugEnabled()) {
			logger.debug("Initialized " + getClass().getSimpleName() + " with PropertySources " + this.propertySources);
		}
	}

会打印"Initialized " + getClass().getSimpleName() + " with PropertySources " + this.propertySources

也就是在文章开头里面,打印的那一堆日志里面:

DEBUG- o.s.web.context.support.StandardServletEnvironment - Initialized StandardServletEnvironment with PropertySources [StubPropertySource@2129144075 {name='servletConfigInitParams', properties=java.lang.Object@1f59a598}, StubPropertySource@504858437 {name='servletContextInitParams', properties=java.lang.Object@192c3f1e}, MapPropertySource@649329985 {name='systemProperties', properties={java.runtime.name=Java(TM) SE Runtime Environment, sun.boot.library.path=D:\Program Files\Java\jre8\bin, java.vm.version=25.77-b03, java.vm.vendor=Oracle Corporation, java.vendor.url=http://java.oracle.com/, path.separator=;, java.vm.name=Java HotSpot(TM) 64-Bit Server VM, file.encoding.pkg=sun.io, user.country=CN, user.script=, sun.java.launcher=SUN_STANDARD, sun.os.patch.level=Service Pack 1, PID=16868, java.vm.specification.name=Java Virtual Machine Specification, java.awt.graphicsenv=sun.awt.Win32GraphicsEnvironment, org.jboss.logging.provider=slf4j, java.endorsed.dirs=D:\Program Files\Java\jre8\lib\endorsed, os.arch=amd64, java.io.tmpdir=C:\Users\AppData\Local\Temp\, line.separator=
, java.vm.specification.vendor=Oracle Corporation, user.variant=, os.name=Windows 7, sun.jnu.encoding=GBK, spring.beaninfo.ignore=true, java.library.path

接着,步骤6里面,跟进了21行代码,printBanner(environment)方法,

现在,再看步骤6里面第22行代码,context = createApplicationContext();

跟进createApplicationContext()方法,

	protected ConfigurableApplicationContext createApplicationContext() {
		Class<?> contextClass = this.applicationContextClass;
		if (contextClass == null) {
			try {
				contextClass = Class.forName(this.webEnvironment
						? DEFAULT_WEB_CONTEXT_CLASS : DEFAULT_CONTEXT_CLASS);
			}
			catch (ClassNotFoundException ex) {
				throw new IllegalStateException(
						"Unable create a default ApplicationContext, "
								+ "please specify an ApplicationContextClass",
						ex);
			}
		}
		return (ConfigurableApplicationContext) BeanUtils.instantiate(contextClass);
	}

看这行代码,contextClass = Class.forName(this.webEnvironment? DEFAULT_WEB_CONTEXT_CLASS : DEFAULT_CONTEXT_CLASS);

如果是webEnvironment,则加载DEFAULT_WEB_CONTEXT_CLASS,否则加载DEFAULT_CONTEXT_CLASS

再看DEFAULT_WEB_CONTEXT_CLASS = "org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext"

再跟进AnnotationConfigEmbeddedWebApplicationContext类

public class AnnotationConfigEmbeddedWebApplicationContext
		extends EmbeddedWebApplicationContext {
    //重要属性和方法,省略其他...
	private final AnnotatedBeanDefinitionReader reader;

	private final ClassPathBeanDefinitionScanner scanner;

	public AnnotationConfigEmbeddedWebApplicationContext() {
		this.reader = new AnnotatedBeanDefinitionReader(this);
		this.scanner = new ClassPathBeanDefinitionScanner(this);
	}
}

看构造函数里面的this.reader = new AnnotatedBeanDefinitionReader(this);
和this.scanner = new ClassPathBeanDefinitionScanner(this);

看跟进ClassPathBeanDefinitionScanner类

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry) {
		this(registry, true);
	}

继续

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters) {
		this(registry, useDefaultFilters, getOrCreateEnvironment(registry));
	}

继续

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
			Environment environment) {

		this(registry, useDefaultFilters, environment,
				(registry instanceof ResourceLoader ? (ResourceLoader) registry : null));
	}

继续

public ClassPathBeanDefinitionScanner(BeanDefinitionRegistry registry, boolean useDefaultFilters,
			Environment environment, ResourceLoader resourceLoader) {

		Assert.notNull(registry, "BeanDefinitionRegistry must not be null");
		this.registry = registry;

		if (useDefaultFilters) {
			registerDefaultFilters();
		}
		setEnvironment(environment);
		setResourceLoader(resourceLoader);
	}

跟进registerDefaultFilters方法(创建AnnotationTypeFilter(Component.class)过滤器对象,并放入到List<TypeFilter> includeFilters)

@SuppressWarnings("unchecked")
	protected void registerDefaultFilters() {
		this.includeFilters.add(new AnnotationTypeFilter(Component.class));
		ClassLoader cl = ClassPathScanningCandidateComponentProvider.class.getClassLoader();
		try {
			this.includeFilters.add(new AnnotationTypeFilter(
					((Class<? extends Annotation>) ClassUtils.forName("javax.annotation.ManagedBean", cl)), false));
			logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");
		}
		catch (ClassNotFoundException ex) {
			// JSR-250 1.1 API (as included in Java EE 6) not available - simply skip.
		}
		try {
			this.includeFilters.add(new AnnotationTypeFilter(
					((Class<? extends Annotation>) ClassUtils.forName("javax.inject.Named", cl)), false));
			logger.debug("JSR-330 'javax.inject.Named' annotation found and supported for component scanning");
		}
		catch (ClassNotFoundException ex) {
			// JSR-330 API not available - simply skip.
		}
	}

在这段代码里,看到logger.debug("JSR-250 'javax.annotation.ManagedBean' found and supported for component scanning");

待更...

猜你喜欢

转载自blog.csdn.net/baofengyu90/article/details/85698938