springboot 是如何识别web项目的
2017-09-06 16:46
946 查看
之前有朋友问题这样的一个问题
springboot通过main方法是如何启动web系统的,它是如何识别当前系统是不是web项目呢。
今天突然想起来了,翻看了下springboot的代码,在此坐下记录
springboot通过SpringApplication的run方法作为入口启动。
系统启动的时候我们传入的是一个source的object对象。一般是传入springboot在我们系统的启动类
在内部实例化了一个SpringApplication对象并且初始化
在初始化过程中检查当前系统是否是web系统代码如下
通过以上代码我们可以看到,系统检查是不是存在以上两个接口的实例化对象以推断当前系统是不是web项目
springboot通过main方法是如何启动web系统的,它是如何识别当前系统是不是web项目呢。
今天突然想起来了,翻看了下springboot的代码,在此坐下记录
springboot通过SpringApplication的run方法作为入口启动。
系统启动的时候我们传入的是一个source的object对象。一般是传入springboot在我们系统的启动类
在内部实例化了一个SpringApplication对象并且初始化
在初始化过程中检查当前系统是否是web系统代码如下
private void initialize(Object[] sources) { if (sources != null && sources.length > 0) { this.sources.addAll(Arrays.asList(sources)); } this.webEnvironment = deduceWebEnvironment(); setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class)); setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); this.mainApplicationClass = deduceMainApplicationClass(); }
private boolean deduceWebEnvironment() { for (String className : WEB_ENVIRONMENT_CLASSES) { if (!ClassUtils.isPresent(className, null)) { return false; } } return true; }
private static final String[] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet", "org.springframework.web.context.ConfigurableWebApplicationContext" };
通过以上代码我们可以看到,系统检查是不是存在以上两个接口的实例化对象以推断当前系统是不是web项目
相关文章推荐
- SpringBoot Web项目中中如何使用Junit
- SpringWeb项目和SpringBoot微服务项目如何进行junit调试
- 利用spring boot如何快速启动一个web项目详解
- Spring Boot基础知识(一)----如何构建spring boot的web项目
- 在web项目中如何启动spring容器?
- Spring-boot web 工程构建,并把maven子项目模块依赖jar打包,笔记
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- SpringBoot 项目如何在tomcat容器中运行
- web项目中如何加入spring
- SpringBoot Web项目的参数绑定:URL传参及默认参数设置
- spring boot项目中使用spring-boot-devtools模块进行代码热部署,避免重新启动web项目
- spring boot项目中使用spring-boot-devtools模块进行代码热部署,避免重新启动web项目
- 【spring boot】5.spring boot 创建web项目并使用jsp作前台页面
- 【JAVA-OSS】如何在spring-boot项目中使用oss上传下载文件
- 从web服务内部停止并关闭springboot项目
- 如何将已有项目迁移到Spring Boot
- 使用Spring Boot开发Web项目(二)之添加HTTPS支持
- web项目中spring如何整合RabbitMQ?
- web项目中spring如何整合RabbitMQ?