标签:.config lse gap main 就是 servlet tin ext lis
springboot的两种启动方式
1
ConfigurableApplicationContext context = SpringApplication.run(Springboot10Application.class, args);
源码
public static ConfigurableApplicationContext run(Object source, String... args) { return run(new Object[] { source }, args); } public static ConfigurableApplicationContext run(Object[] sources, String[] args) { return new SpringApplication(sources).run(args); }
最后还是new了一个SpringApplication.调用了run方法.
2
SpringApplication application=new SpringApplication(Springboot10Application.class);
ConfigurableApplicationContext context = application.run(args);
这两种方式其实是一样的
这是一个构造器,有一个可变参数列表:源
public SpringApplication(Object... sources) { initialize(sources); }
构造器调用了initialize方法,把可变参数列表传过去了
定义了一个变量
private final Set<Object> sources = new LinkedHashSet<Object>();
private boolean webEnvironment;//定义是不是web环境
private void initialize(Object[] sources) { if (sources != null && sources.length > 0) {
//判断这个source不为空的话就把这个source转为list后加入到set中去 this.sources.addAll(Arrays.asList(sources)); }
//判断是不是web环境 this.webEnvironment = deduceWebEnvironment();
// setInitializers((Collection) getSpringFactoriesInstances( ApplicationContextInitializer.class)); setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class)); this.mainApplicationClass = deduceMainApplicationClass(); }
//判断是不是web环境
private static final String[] WEB_ENVIRONMENT_CLASSES = { "javax.servlet.Servlet", "org.springframework.web.context.ConfigurableWebApplicationContext" };
当这两个类都存在的时候就是web环境
private boolean deduceWebEnvironment() { for (String className : WEB_ENVIRONMENT_CLASSES) { if (!ClassUtils.isPresent(className, null)) { return false; } } return true; }
标签:.config lse gap main 就是 servlet tin ext lis
原文地址:https://www.cnblogs.com/songfahzun/p/9256399.html