您的位置:首页 > 编程语言 > Java开发

springboot 是如何识别web项目的

2017-09-06 16:46 946 查看
之前有朋友问题这样的一个问题

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项目
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: