private void createWebServer {WebServer webServer = this.webServer;ServletContext servletContext = getServletContext;if (webServer == && servletContext == ) {ServletWebServerFactory factory = getWebServerFactory;this.webServer = factory.getWebServer(getSelfInitializer);}else if (servletContext != ) {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 != ) ? 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的一个流程 。如果不明白的话 , 我们在用另一种方式来理解下 , 大家要应该都知道stater举点例子 。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency>首先自定义一个stater 。<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-freemarker</artifactId></dependency>我们先来看 maven 配置写入版本号 , 如果自定义一个 stater 的话必须依赖 spring-boot-autoconfigure 这个包,我们先看下项目目录 。
文章插图
publicclassGwServiceImplimplementsGwService{@AutowiredGwProperties properties;@OverridepublicvoidHello{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@ConditionalOnMissingBeanpublic GwService gwService{return new GwServiceImpl;}}这个为配置类 , 为什么这么写因为 , spring-boot的stater都是这么写的 , 我们可以参照他仿写stater , 以达到自动配置的目的 , 然后我们在通过spring.factories也来进行配置 。org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gw.GwAutoConfiguration然后这样一个简单的stater就完成了 , 然后可以进行maven的打包 , 在其他项目引入就可以使用 。来源:
cnblogs.com/jing99/p/11504113.html
【15000 字详解 Spring Boot 注解】
推荐阅读
- Spring IOC容器对Bean实例化的过程详解源码分析
- Attention 详解深度学习中的注意力机制
- 雪莲花的做法大全家常,简单易学槐花的四种家常做法详解
- 朱元璋:我能活多久,朱元璋八字分析
- 战国时期两个字的名人,战国时期两个水利工程
- 副清风散,详解鸡冠花的药用功效与作用
- 杭白菊老字号品牌,杭白菊的生长习性
- 适合水果商标名字,0万买回商标
- 亲自啃了一周,终于把Mybatis源码理清,以后简历请写精通二字
- Spring MVC 框架搭建配置方法及详解
