/**
* Launch the application. This method is the initial entry point that should be
* called by a subclass {@code public static void main(String[] args)} method.
* @param args the incoming arguments
* @throws Exception if the application fails to launch
*/protectedvoidlaunch(String[] args)throws Exception {
JarFile.registerUrlProtocolHandler();//创建spring boot自己的类加载器
ClassLoader classLoader =createClassLoader(getClassPathArchives());//getMainClass()获取到springboot中启动类的路径launch(args,getMainClass(), classLoader);}protected ClassLoader createClassLoader(List<Archive> archives)throws Exception {
List<URL> urls =newArrayList<>(archives.size());for(Archive archive : archives){//获取到每个具体归档文件的路径
urls.add(archive.getUrl());}returncreateClassLoader(urls.toArray(newURL[0]));}/**
* Create a classloader for the specified URLs.
* @param urls the URLs
* @return the classloader
* @throws Exception if the classloader cannot be created
*/protected ClassLoader createClassLoader(URL[] urls)throws Exception {returnnewLaunchedURLClassLoader(urls,getClass().getClassLoader());}
设置当前线程上下文类加载器
/**
* Launch the application given the archive file and a fully configured classloader.
* @param args the incoming arguments
* @param mainClass the main class to run
* @param classLoader the classloader
* @throws Exception if the launch fails
*/protectedvoidlaunch(String[] args, String mainClass, ClassLoader classLoader)throws Exception {
Thread.currentThread().setContextClassLoader(classLoader);createMainMethodRunner(mainClass, args, classLoader).run();}