阅读SpringBoot代码先看spring.factoeies。
RestartScopeInitializer
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
applicationContext.getBeanFactory().registerScope(“restart”, new RestartScope());
}
/**
* {@link Scope} that stores beans as {@link Restarter} attributes.
*/
private static class RestartScope implements Scope {
@Override
public Object get(String name, ObjectFactory<?> objectFactory) {
return Restarter.getInstance().getOrAddAttribute(name, objectFactory);
}
@Override
public Object remove(String name) {
return Restarter.getInstance().removeAttribute(name);
}
@Override
public void registerDestructionCallback(String name, Runnable callback) {
}
@Override
public Object resolveContextualObject(String key) {
return null;
}
@Override
public String getConversationId() {
return null;
}
}
ApplicationContextInitializer接口是在Spring容器启动之前执行的,在Spring的容器中注入RestartScope(这是一个特殊的Bean,也类似一个容器吧),在这个容器中创建的对象由它自己保管,而不是Spring容器中托管。
RestartConfiguration
@Lazy(false)
@Configuration(proxyBeanMethods = false)
@ConditionalOnProperty(prefix = “spring.devtools.restart”, name = “enabled”, matchIfMissing = true)
static class RestartConfiguration {
private final DevToolsProperties properties;
RestartConfiguration(DevToolsProperties properties) {
this.properties = properties;
}
@Bean
ApplicationListener<ClassPathChangedEvent> restartingClassPathChangedEventListener(
FileSystemWatcherFactory fileSystemWatcherFactory) {
return (event) -> {
if (event.isRestartRequired()) {
Restarter.getInstance().restart(new FileWatchingFailureHandler(fileSystemWatcherFactory));
}
};
}
@Bean
@ConditionalOnMissingBean
ClassPathFileSystemWatcher classPathFileSystemWatcher(FileSystemWatcherFactory fileSystemWatcherFactory,
ClassPathRestartStrategy classPathRestartStrategy) {
URL[] urls = Restarter.getInstance().getInitialUrls();
ClassPathFileSystemWatcher watcher = new ClassPathFileSystemWatcher(fileSystemWatcherFactory,
classPathRestartStrategy, urls);
watcher.setStopWatcherOnRestart(true);
return watcher;
}
@Bean
@ConditionalOnMissingBean
ClassPathRestartStrategy classPathRestartStrategy() {
return new PatternClassPathRestartStrategy(this.properties.getRestart().getAllExclude());
}
@Bean
FileSystemWatcherFactory fileSystemWatcherFactory() {
return this::newFileSystemWatcher;
}
@Bean
@ConditionalOnProperty(prefix = "spring.devtools.restart", name = "log-condition-evaluation-delta",
matchIfMissing = true)
ConditionEvaluationDeltaLoggingListener conditionEvaluationDeltaLoggingListener() {
return new ConditionEvaluationDeltaLoggingListener();
}
private FileSystemWatcher newFileSystemWatcher() {
Restart restartProperties = this.properties.getRestart();
FileSystemWatcher watcher = new FileSystemWatcher(true, restartProperties.getPollInterval(),
restartProperties.getQuietPeriod());
String triggerFile = restartProperties.getTriggerFile();
if (StringUtils.hasLength(triggerFile)) {
watcher.setTriggerFilter(new TriggerFileFilter(triggerFile));
}
List<File> additionalPaths = restartProperties.getAdditionalPaths();
for (File path : additionalPaths) {
watcher.addSourceFolder(path.getAbsoluteFile());
}
return watcher;
}
}
● classPathFileSystemWatcher 监听classpath下文件变动。
ClassPathFileSystemWatcher实现了InitializingBean接口,根据Bean的生命周期在创建完这个Bean之前会执行afterPropertiesSet方法。在这个方法中看到注册了ClassPathFileChangeListener监听器,并启动。那么我们在往下看下start方法里面干了那些事情。
我们看到这里又创建了一个watchThread线程并启动,这里才是监听文件变动的核心线程。
Spring的方法命名还是很有意思的,看到这个scan我们大概猜到,在这里进行扫描文件变化,并做出相应的动作的。
看到这里是不是心里有底了?先活动当前文件信息,判断文件是否变动,如果变动测进行修改(也就是后面的热部署)。FolderSnapshot和Spring的定义一样的,封装了文件信息。previous是上一次检查是的文件信息,current是当前最查询的最新class信息。
监听到文件变动最终会发送一个ClassPathChangedEvent事件。
到这里文件监听布置已经完成了。不知道你是否还记得在前面已经看到那个事件监听器了呢,我们继续往下看。
● restartingClassPathChangedEventListener
看到这个顿时心里不慌了,流程是不是也串起来了?我们点进restart文件看看,有一个stop和start方法,你能猜测一下都干了什么呢?
我们先看stop方法。
也就是关闭Spring容器,进行一些资源的回收。为后面重新启动容器做准备。在start方法中最终调用的是doStart方法,不知道你是否还记着,在Spring中do开头的方法都是真正干活的。
我们还看见在这里创建了一个RestartClassLoader加载器,这个就是Spring热部署和核心加载器。
看到RestartLauncher不知道你收没有想到什么呢?想想JVM的启动入口在哪里。
先初始化ExtClassLoader和AppCLassLoader类加载器,由于BootstrapClassLoader是系统最底层的类加载器,使用C语言编写,所以这里看不到,最后将AppClassLoader设置为当前现在的类加载器。那我们在回过头来看下Spring是如何做的。
1、设置当前线程的名称
2、 方法设置时调用的处理这个线程突然终止由于未捕获到异常。
3、设置非守护线程。
4、将Spring的类加载器设置到线程上下文中,后面就是使用这个类加载器加载了。
在这里拿到main函数的类和入口,偷换了启动的概览,使用另外一个线程启动main函数,这样就可以使用自定义的类加载器加载所有的类了。那我们思考一下,Spring在第一次启动的时候,并没有classPath文件变动,那是如何加载的呢?细心的你可能发现,我们在第一次启动的时候打印的日志线程也是restartedMain线程。
RestartApplicationListener
这里就要提到热部署的另外一个监听器RestartApplicationListener。
● org.springframework.boot.devtools.restart.RestartApplicationListener#onApplicationStartingEvent
● org.springframework.boot.devtools.restart.Restarter#initialize(java.lang.String[], boolean, org.springframework.boot.devtools.restart.RestartInitializer, boolean)
● org.springframework.boot.devtools.restart.Restarter#initialize(boolean)
● org.springframework.boot.devtools.restart.Restarter#immediateRestart
在immediateRestart方法中最后由到了前面说的doStart方法了。这里主动抛出了一个异常,是为了让当前启动线程终止,使用类加载器所在线程初始化容器。