当前位置: 首页 > news >正文

视频网站 做综艺 电视台免费咨询宠物医生在线

视频网站 做综艺 电视台,免费咨询宠物医生在线,ww事业怎么推广,dyndns免费域名文章目录 分析SpringBoot 底层机制【Tomcat 启动分析Spring 容器初始化Tomcat 如何关联Spring 容器】[上]搭建SpringBoot 底层机制开发环境Configuration Bean 会发生什么#xff0c;并分析机制提出问题#xff1a;SpringBoot 是怎么启动Tomcat #xff0c;并可以支持访问C… 文章目录 分析SpringBoot 底层机制【Tomcat 启动分析Spring 容器初始化Tomcat 如何关联Spring 容器】[上]搭建SpringBoot 底层机制开发环境Configuration Bean 会发生什么并分析机制提出问题SpringBoot 是怎么启动Tomcat 并可以支持访问Controller源码分析: SpringApplication.run() 分析SpringBoot 底层机制【Tomcat 启动分析Spring 容器初始化Tomcat 如何关联Spring 容器】[上] 搭建SpringBoot 底层机制开发环境 1、创建Maven 项目nlc-springboot 2、修改nlc-springboot\pom.xml , 导入相关依赖 groupIdcom.nlc/groupId artifactIdnlc-springboot/artifactId version1.0-SNAPSHOT/version !-- 导入springboot 父工程规定的写法-- parentgroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-parent/artifactIdversion2.5.3/version /parent !-- 导入web 项目场景启动器,会自动导入和web 开发相关依赖,非常方便-- dependenciesdependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId/dependency /dependencies3 、创建nlc-springboot\src\main\java\com\nlc\springboot\MainApp.java SpringBootApplication public class MainApp {public static void main(String[] args) {//启动SpringBoot 应用程序ConfigurableApplicationContext run SpringApplication.run(MainApp.class, args);} }4、启动项目ok, 大家注意Tomcat 也启动了 Configuration Bean 会发生什么并分析机制 1 、创建nlc-springboot\src\main\java\com\nlc\springboot\bean\Dog.java public class Dog { }2 、创建nlc-springboot\src\main\java\com\nlc\springboot\config\Config.java Configuration public class Config {/*** 1. 通过Bean 的方式, 将new 出来的Bean 对象, 放入到Spring 容器* 2. 该bean 在Spring 容器的name 就是方法名* 3. 通过方法名, 可以得到new Dog()* return*/Beanpublic Dog dog() {return new Dog();} }3 、Debugnlc-springboot\src\main\java\com\nlc\springboot\MainApp.java, 看看容器中是否已经注入了dog 实例 4、底层机制分析: 仍然是我们实现Spring 容器那一套机制IO/文件扫描注解反射集合映射 提出问题SpringBoot 是怎么启动Tomcat 并可以支持访问Controller 1 、创建nlc-springboot\src\main\java\com\nlc\springboot\controller\HiController.java RestController public class HiController {RequestMapping(/hi)public String hi() {System.out.println(hi i am HiController);return hi i am HiController;} }2、完成测试, 浏览器输入http://localhost:8080/hi 3、问题: SpringBoot 是怎么内嵌Tomcat, 并启动Tomcat 的? 追踪源码 源码分析: SpringApplication.run() 1、Debug SpringApplication.run(MainApp.class, args) 看看SpringBoot 是如何启动Tomcat 的. 2、我们的Debug 目标: 紧抓一条线, 就是看到tomcat 被启动的代码. 比如tomcat.start() public class MainApp {public static void main(String[] args) {//启动springboot应用程序/项目//当我们执行run方法时怎么就启动我们的内置的tomcat?//在分析run方法的底层机制的基础上我们自己尝试实现ConfigurableApplicationContext ioc SpringApplication.run(MainApp.class, args);/** 开始debug SpringApplication.run()* 1.SpringApplication.java* public static ConfigurableApplicationContext run(Class? primarySource, String... args) {return run(new Class[]{primarySource}, args);}** 2.SpringApplication.java:创建返回 ConfigurableApplicationContext 对象* public static ConfigurableApplicationContext run(Class?[] primarySources, String[] args) {* return (new SpringApplication(primarySources)).run(args);* }** 3.SpringApplication.java* public ConfigurableApplicationContext run(String... args) {StopWatch stopWatch new StopWatch();stopWatch.start();DefaultBootstrapContext bootstrapContext this.createBootstrapContext();ConfigurableApplicationContext context null;this.configureHeadlessProperty();SpringApplicationRunListeners listeners this.getRunListeners(args);listeners.starting(bootstrapContext, this.mainApplicationClass);try {ApplicationArguments applicationArguments new DefaultApplicationArguments(args);ConfigurableEnvironment environment this.prepareEnvironment(listeners, bootstrapContext, applicationArguments);this.configureIgnoreBeanInfo(environment);Banner printedBanner this.printBanner(environment);context this.createApplicationContext();//创建容器严重分析context.setApplicationStartup(this.applicationStartup);this.prepareContext(bootstrapContext, context, environment, listeners, applicationArguments, printedBanner);this.refreshContext(context);//严重分析刷新应用程序上下文比如初始化默认配置/注入相关Bean/启动tomcatthis.afterRefresh(context, applicationArguments);stopWatch.stop();if (this.logStartupInfo) {(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);}listeners.started(context);this.callRunners(context, applicationArguments);} catch (Throwable var10) {this.handleRunFailure(context, var10, listeners);throw new IllegalStateException(var10);}try {listeners.running(context);return context;} catch (Throwable var9) {this.handleRunFailure(context, var9, (SpringApplicationRunListeners)null);throw new IllegalStateException(var9);}}* 4.SpringApplication.java容器类型很多会根据你的this.webApplicationType创建对应的容器* 默认this.webApplicationType 是SERVLET也就是web容器/可以处理servlet* protected ConfigurableApplicationContext createApplicationContext() {return this.applicationContextFactory.create(this.webApplicationType);}** 5.ApplicationContextFactory.java* ApplicationContextFactory DEFAULT (webApplicationType) - {try {switch (webApplicationType) {//如果想要更改可以通过配置文件更改想要创建的容器种类但是目前本身就是web开发没必要去改case SERVLET://默认进入这个分支返回AnnotationConfigServletWebServerApplicationContext容器return new AnnotationConfigServletWebServerApplicationContext();case REACTIVE:return new AnnotationConfigReactiveWebServerApplicationContext();default:return new AnnotationConfigApplicationContext();}} catch (Exception var2) {throw new IllegalStateException(Unable create a default ApplicationContext instance, you may need a custom ApplicationContextFactory, var2);}};** 6.SpringApplication.java* private void refreshContext(ConfigurableApplicationContext context) {if (this.registerShutdownHook) {shutdownHook.registerApplicationContext(context);}this.refresh(context);//严重分析真正执行相关任务}** 7.SpringApplication.java* protected void refresh(ConfigurableApplicationContext applicationContext) {applicationContext.refresh();}**8.ServletWebServerApplicationContext.java* public final void refresh() throws BeansException, IllegalStateException {try {super.refresh();//容器类型很多走一下父类} catch (RuntimeException var3) {WebServer webServer this.webServer;if (webServer ! null) {webServer.stop();//如果出现异常就关闭服务所以 super.refresh()里面肯定存在tomcat启动代码}throw var3;}}** 9.AbstractApplicationContext.java* public void refresh() throws BeansException, IllegalStateException {synchronized(this.startupShutdownMonitor) {StartupStep contextRefresh this.applicationStartup.start(spring.context.refresh);this.prepareRefresh();ConfigurableListableBeanFactory beanFactory this.obtainFreshBeanFactory();this.prepareBeanFactory(beanFactory);try {this.postProcessBeanFactory(beanFactory);StartupStep beanPostProcess this.applicationStartup.start(spring.context.beans.post-process);this.invokeBeanFactoryPostProcessors(beanFactory);this.registerBeanPostProcessors(beanFactory);beanPostProcess.end();this.initMessageSource();this.initApplicationEventMulticaster();this.onRefresh();//有点像模板模式先回到父类做一些共同的工作因为下面有很多容器都会做共同的工作到真正要刷新的时候又通过动态绑定机制回到子类再开始进行具体的刷新任务this.registerListeners();this.finishBeanFactoryInitialization(beanFactory);this.finishRefresh();} catch (BeansException var10) {if (this.logger.isWarnEnabled()) {this.logger.warn(Exception encountered during context initialization - cancelling refresh attempt: var10);}this.destroyBeans();this.cancelRefresh(var10);throw var10;} finally {this.resetCommonCaches();contextRefresh.end();}}}**10.ServletWebServerApplicationContext.Java* protected void onRefresh() {super.onRefresh();try {this.createWebServer();//创建webservlet 可以理解成创建指定 web服务-Tomcat} catch (Throwable var2) {throw new ApplicationContextException(Unable to start web server, var2);}}**11.ServletWebServerApplicationContext.Java* private void createWebServer() {WebServer webServer this.webServer;ServletContext servletContext this.getServletContext();if (webServer null servletContext null) {StartupStep createWebServer this.getApplicationStartup().start(spring.boot.webserver.create);ServletWebServerFactory factory this.getWebServerFactory();createWebServer.tag(factory, factory.getClass().toString());this.webServer factory.getWebServer(new ServletContextInitializer[]{this.getSelfInitializer()});//严重分析使用TomcatServletWebServerFactory创建一个TomcatWebServletcreateWebServer.end();this.getBeanFactory().registerSingleton(webServerGracefulShutdown, new WebServerGracefulShutdownLifecycle(this.webServer));this.getBeanFactory().registerSingleton(webServerStartStop, new WebServerStartStopLifecycle(this, this.webServer));} else if (servletContext ! null) {try {this.getSelfInitializer().onStartup(servletContext);} catch (ServletException var5) {throw new ApplicationContextException(Cannot initialize servlet context, var5);}}this.initPropertySources();}* 12.TomcatServletWebServerFactory.java* public WebServer getWebServer(ServletContextInitializer... initializers) {if (this.disableMBeanRegistry) {Registry.disableRegistry();}Tomcat tomcat new Tomcat();//创建Tomcat对象File baseDir this.baseDirectory ! null ? this.baseDirectory : this.createTempDir(tomcat);//做了一系列的设置tomcat.setBaseDir(baseDir.getAbsolutePath());Connector connector new Connector(this.protocol);connector.setThrowOnFailure(true);tomcat.getService().addConnector(connector);this.customizeConnector(connector);tomcat.setConnector(connector);tomcat.getHost().setAutoDeploy(false);this.configureEngine(tomcat.getEngine());Iterator var5 this.additionalTomcatConnectors.iterator();while(var5.hasNext()) {Connector additionalConnector (Connector)var5.next();tomcat.getService().addConnector(additionalConnector);}this.prepareContext(tomcat.getHost(), initializers);return this.getTomcatWebServer(tomcat);//严重分析}* 13.TomcatServletWebServerFactory.java//做了一个校验创建TomcatWebServer* protected TomcatWebServer getTomcatWebServer(Tomcat tomcat) {return new TomcatWebServer(tomcat, this.getPort() 0, this.getShutdown());}** 14.TomcatServletWebServerFactory.java* public TomcatWebServer(Tomcat tomcat, boolean autoStart, Shutdown shutdown) {this.monitor new Object();this.serviceConnectors new HashMap();Assert.notNull(tomcat, Tomcat Server must not be null);this.tomcat tomcat;this.autoStart autoStart;this.gracefulShutdown shutdown Shutdown.GRACEFUL ? new GracefulShutdown(tomcat) : null;this.initialize();//分析方法进行初始化和相应的启动}** 15.TomcatServletWebServerFactory.java* private void initialize() throws WebServerException {logger.info(Tomcat initialized with port(s): this.getPortsDescription(false));synchronized(this.monitor) {try {this.addInstanceIdToEngineName();Context context this.findContext();context.addLifecycleListener((event) - {if (context.equals(event.getSource()) start.equals(event.getType())) {this.removeServiceConnectors();}});this.tomcat.start();//启动Tomcat监听this.rethrowDeferredStartupExceptions();try {ContextBindings.bindClassLoader(context, context.getNamingToken(), this.getClass().getClassLoader());} catch (NamingException var5) {}this.startDaemonAwaitThread();} catch (Exception var6) {this.stopSilently();this.destroySilently();throw new WebServerException(Unable to start embedded Tomcat, var6);}}}*执行到this.tomcat.start();我们可以返回如下图位置查看context容器值*/System.out.println(hello ioc); 当我们刷新整个上下文时像config的配置类进去了包括我们的Bean也进去了
http://www.dnsts.com.cn/news/163944.html

相关文章:

  • 做英文网站的心得如何获得企业邮箱
  • 学科网站建设网站开发及技术路线
  • 高效网站推广设计wordpress迁移后插件消失
  • 选择网站做友情链接的标准一般是比较好的网站建设哪家好
  • 河南中原建设公司网站wordpress 插件打不开
  • wordpress 内置tagseo建站系统
  • 常州便宜的做网站服务电商网站开发平台哪个好
  • 邢台公司网站建设域名和服务器多少钱
  • 访问网站错误代码为137html教程网站
  • 专做商品折扣的网站网站建设费用预算表、
  • 手机网站可以做商城吗凡客诚品是干嘛的
  • 商城网站定制建设价位wordpress忘记密码怎么办
  • 网站制作设计方案网站策划
  • 单位申请免费网站旅游网站制作旅游网
  • 动力无限做网站优化设计七年级下册语文答案
  • 前端做网站难吗彩页印刷
  • 网站备案号的区别广州企业网站建设报价
  • 北京公司可以在上海建网站吗网站开发者id
  • 企业网站推广服务如何修改wordpress权限
  • 外贸建站模板价格企业网站推广最有效的方法
  • 乌海建设局网站wordpress远程访问
  • 怎么做网站用于推广wordpress管理后台没有登陆
  • 整形网站 源码网站前端模板下载
  • 网站打开速度慢是否需要升级带宽网站快速收录方法
  • 品牌网站建设帮你大蝌蚪自己搭建网站的步骤
  • 登建设厅锁子的是哪个网站ytwzjs烟台网站建设
  • 徐州市建设局网站软件工程考研要考哪些科目
  • 教育类网站开发模板营销软文范例大全
  • 怎样建设一个购物网站安装wordpress安装地址修改
  • 地方网站发展方向网页设计怎么分析网站啊