他会调用 ((abstractapplicationcontext) applicationcontext).refresh();方法,我们点进来看
public void refresh() throws beansexception, illegalstateexception { synchronized (this.startupshutdownmonitor) { // prepare this context for refreshing. preparerefresh(); // tell the subclass to refresh the internal bean factory. configurablelistablebeanfactory beanfactory = obtainfreshbeanfactory(); // prepare the bean factory for use in this context. preparebeanfactory(beanfactory); try { // allows post-processing of the bean factory in context subclasses. postprocessbeanfactory(beanfactory); // invoke factory processors registered as beans in the context. invokebeanfactorypostprocessors(beanfactory); // register bean processors that intercept bean creation. registerbeanpostprocessors(beanfactory); // initialize message source for this context. initmessagesource(); // initialize event multicaster for this context. initapplicationeventmulticaster(); // initialize other special beans in specific context subclasses. onrefresh(); // check for listener beans and register them. registerlisteners(); // instantiate all remaining (non-lazy-init) singletons. finishbeanfactoryinitialization(beanfactory); // last step: publish corresponding event. finishrefresh(); } catch (beansexception ex) { if (logger.iswarnenabled()) { logger.warn(exception encountered during context initialization - + cancelling refresh attempt: + ex); } // destroy already created singletons to avoid dangling resources. destroybeans(); // reset 'active' flag. cancelrefresh(ex); // propagate exception to caller. throw ex; } finally { // reset common introspection caches in spring's core, since we // might not ever need metadata for singleton beans anymore... resetcommoncaches(); } }}这点代码似曾相识啊 没错,就是一个spring的bean的加载过程我在,解析springioc加载过程的时候介绍过这里面的方法,如果你看过spring源码的话 ,应该知道这些方法都是做什么的。现在我们不关心其他的,我们来看一个方法叫做 onrefresh();方法
protected void onrefresh() throws beansexception { // for subclasses: do nothing by default.}他在这里并没有实现,但是我们找他的其他实现,我们来找
我们既然要找tomcat那就肯定跟web有关,我们可以看到有个servletwebserverapplicationcontext
@overrideprotected void onrefresh() { super.onrefresh(); try { createwebserver(); } catch (throwable ex) { throw new applicationcontextexception(unable to start web server, ex); }}我们可以看到有一个createwebserver();方法他是创建web容器的,而tomcat不就是web容器,那他是怎么创建的呢,我们继续看
private void createwebserver() { webserver webserver = this.webserver; servletcontext servletcontext = getservletcontext(); if (webserver == null && servletcontext == null) { servletwebserverfactory factory = getwebserverfactory(); this.webserver = factory.getwebserver(getselfinitializer()); } else if (servletcontext != null) { try { getselfinitializer().onstartup(servletcontext); } catch (servletexception ex) { throw new applicationcontextexception(cannot initialize servlet context, ex); } } initpropertysources();}factory.getwebserver(getselfinitializer());他是通过工厂的方式创建的
public interface servletwebserverfactory { webserver getwebserver(servletcontextinitializer... initializers);}可以看到 它是一个接口,为什么会是接口。因为我们不止是tomcat一种web容器。
我们看到还有jetty,那我们来看tomcatservletwebserverfactory
@overridepublic webserver getwebserver(servletcontextinitializer... initializers) { tomcat tomcat = new tomcat(); file basedir = (this.basedirectory != null) ? this.basedirectory : createtempdir(tomcat); tomcat.setbasedir(basedir.getabsolutepath()); connector connector = new connector(this.protocol); tomcat.getservice().addconnector(connector); customizeconnector(connector); tomcat.setconnector(connector); tomcat.gethost().setautodeploy(false); configureengine(tomcat.getengine()); for (connector additionalconnector : this.additionaltomcatconnectors) { tomcat.getservice().addconnector(additionalconnector); } preparecontext(tomcat.gethost(), initializers); return gettomcatwebserver(tomcat);}那这块代码,就是我们要寻找的内置tomcat,在这个过程当中,我们可以看到创建tomcat的一个流程。因为run方法里面加载的东西很多,所以今天就浅谈到这里。如果不明白的话, 我们在用另一种方式来理解下,
大家要应该都知道stater举点例子
org.springframework.boot spring-boot-starter-data-redis org.springframework.boot spring-boot-starter-freemarker所以我们不防不定义一个stater来理解下,我们做一个需求,就是定制化不同的人跟大家说你们好,我们来看
org.springframework.boot spring-boot-starter-parent 2.1.4.release com.zgwgw-spring-boot-srater1.0-snapshot org.springframework.boot spring-boot-autoconfigure 我们先来看maven配置写入版本号,如果自定义一个stater的话必须依赖spring-boot-autoconfigure这个包,我们先看下项目目录
public class gwserviceimpl implements gwservice{ @autowired gwproperties properties; @override public void hello() { string name=properties.getname(); system.out.println(name+说:你们好啊); }}我们做的就是通过配置文件来定制name这个是具体实现
@component@configurationproperties(prefix = spring.gwname)public class gwproperties { string name=zgw; public string getname() { return name; } public void setname(string name) { this.name = name; }}这个类可以通过@configurationproperties读取配置文件
@configuration@conditionalonclass(gwservice.class) //扫描类@enableconfigurationproperties(gwproperties.class) //让配置类生效public class gwautoconfiguration { /** * 功能描述 托管给spring * @author zgw * @return */ @bean @conditionalonmissingbean public gwservice gwservice() { return new gwserviceimpl(); }}这个为配置类,为什么这么写因为,spring-boot的stater都是这么写的,我们可以参照他仿写stater,以达到自动配置的目的,然后我们在通过spring.factories也来进行配置
org.springframework.boot.autoconfigure.enableautoconfiguration=com.gw.gwautoconfiguration然后这样一个简单的stater就完成了,然后可以进行maven的打包,在其他项目引入就可以使用,在这里列出代码地址
MySQL数据库如何安装和使用说明
立体仓库是什么,有哪些特点
余压监控系统将会成为“智慧消防”领域中的一把利器
清越科技:硅基OLED产品已向客户批量出货
功率因数偏低如何解决 功率因数和有功功率无功功率的关系
SpringBoot的核心注解2
多功能肉制品安全检测仪的产品性能是怎样的
PCB企业财报数据综合展现
esd保护措施有哪些
消磁电阻在电视机消磁中的应用
AMD或发布优化改良版锐龙3000系列 升级12nm Zen+架构且频率有所提升
iPhone8即将来临 苹果这次准备干一票大的?
加速碳中和研究进展已成为电力能源领域的主要趋势
OLED需求放缓 三星并未受益
小米米家电水壶1A即将开卖,1800W大功率聚能加热环发热
车载充电器快充怎么选
手机壳泄露iPhone XI外观
一加第一款智能手表通过印度BIS认证
LabVIEW引入特别的虚拟仪表的概念
“无线”功率开关为节能汽车提供先进电源管理解决方案