spring boot 内置tomcat源码学习(一)

主要是总结一下最近看spring源码的学习到的经验,这一部分是springboot中对server配置内容是怎么初始化到代码中。

springboot使用的版本是2.0.4.RELEASE

1.第一部分是将配置文件中的配置信息加载到TomcatServletWebServerFactory 工厂类中。整个流程图如下:

1.1WebServerFactoryCustomizerBeanPostProcessor 类

implement BeanPostProcessor ,实现 postProcessBeforeInitialization();

BeanPostProcessor 这个接口是spring提供的接口,只要类实现了这个接口,并实现其中的两个方法就可以再spring 容器初始化所有的bean对象之前和之后进行一些操作。WebServerFactoryCustomizerBeanPostProcessor 的源码如下:

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		if (bean instanceof WebServerFactory) {
			postProcessBeforeInitialization((WebServerFactory) bean);
		}
		return bean;
	}
其中的postProcessBeforeInitialization(WebServerFactory webServerFactory) 会从spring容器中获取到所有的WebServerFactoryCustomizer对象,并调用他们的customize(webServerFactory);

1.2ServletWebServerFactoryCustomizer

customize(ConfigurableServletWebServerFactory factory)主要是将ServerProperties类中获取到的配置信息转换到factory类中。
public void customize(ConfigurableServletWebServerFactory factory) {
		PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
		map.from(this.serverProperties::getPort).to(factory::setPort);
		...
	}

2.通过TomcatServletWebServerFactory 获取到WebContext

2.1 ServletWebServerApplicationContext:就是应用的applicationContext对象 ,保存一个WebServer对象。
	@Override
	protected void onRefresh() {
		super.onRefresh();
		try {
			createWebServer();
		}
		catch (Throwable ex) {
			throw new ApplicationContextException("Unable to start web server", ex);
		}
	}

3.其中用到设计思想。

3.1模板方法

模板方法主要用于一些有通用的流程,但是有不同的实现方的场合。接口声明通用的流程或者规范。抽象类中有通用的实现,实现类中定制的一些内容。

3.2processor设计

WebServerFactoryCustomizerBeanPostProcessor

这个类就体现了这种模式设计,从spring容器中获取所有的WebServerFactoryCustomizer的实现,并排序,然后根据集合中的顺序,依次调用,跟责任链模式的功能是一样的,但是语义上没有链式调用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值