In Spring Developer Tools Source Analysis: First, the file directory monitoring design introduced Devtools provides file monitoring implementation, in the second part, we will use the first part provides the directory monitoring function, to achieve the development environment CLASSPATH The monitoring. second, class path monitoring
First look at some of the class diagrams that may be involved in this section:
In the figure, the upper left part of the red slash is the file directory monitoring class described in the first section, where FileSystemWatcher monitors the specified directory through a separate thread, and when the contents of the directory change, by comparing the snapshots you can obtain all the file changedfiles that monitor the directory changes. The change is then notified to subscribers who have implemented the Filechangelistener listener.
The main classes are described below in the order of the local to the whole. 2.1 classpathfolders Class directory
This class implements the Iterable<file> interface, which is constructed with a parameter of url[], which is the URL form of the classpath. Classpathfolders simply converts the URL into a list<file> collection, and finally returns the iterator through iterator. 2.2 classpathrestartstrategy Restart policy
@FunctionalInterface Public
interface Classpathrestartstrategy {
/**
* Return True if a full restart is Requi Red.
* @param file The changed file
* @return {@code true} if a full restart is required */
Boolean Isrestartrequir Ed (changedfile file);
}
The interface method determines whether a restart is required depending on the file being changed.
The Patternclasspathrestartstrategy implementation is provided by default, which supports Ant-style pattern matching by setting the Excludepatterns class to set the file name (relative path starting from the Classpath) that does not need to be restarted. When a file that is not in the excluded range changes, it returns true to cause a subsequent restart. 2.3 classpathchangedevent class path Change event
The class inherits the Applicationevent, which contains the changed collection of files and flags that the system needs to be restarted. This class, in the later Classpathfilechangelistener, converts messages that monitor directory changes to Spring events that can be conveniently decoupled from @EventListener annotations for further event snooping. 2.4 Classpathfilechangelistener class path Change listener
This class implements Filechangelistener and is added to the FileSystemWatcher subscription list in later Classpathfilesystemwatcher.
When the file changes, the following method is triggered:
@Override public
void OnChange (set<changedfiles> changeSet) {
Boolean restart = isrestartrequired ( ChangeSet);
Publishevent (New Classpathchangedevent (this, changeSet, restart));
}
This first uses the restart strategy mentioned earlier to determine if this change requires a reboot, and then creates a Classpathchangedevent event that is released through Spring's Applicationeventpublisher. After the event is published, all listeners listening to the Classpathchangedevent event trigger execution, and in subsequent blogs, the listener and the connection to the event are established here. 2.4 Classpathfilesystemwatcher class path file monitoring
Class path monitoring implementation class for Classpathfilesystemwatcher, first look at the construction method of this class:
/**
* Create a new {@link Classpathfilesystemwatcher} instance.
* @param filesystemwatcherfactory a factory to create the underlying
* {@link FileSystemWatcher} used to monitor the L ocal file System
* @param restartstrategy the classpath restart strategy
* @param urls the URLs to watch
*/
public Classpathfilesystemwatcher (filesystemwatcherfactory filesystemwatcherfactory,
Classpathrestartstrategy Restartstrategy, url[] URLs) {
assert.notnull (filesystemwatcherfactory,
" Filesystemwatcherfactory must not being null ");
Assert.notnull (URLs, "URLs must not being null");
This.filesystemwatcher = Filesystemwatcherfactory.getfilesystemwatcher ();
This.restartstrategy = Restartstrategy;
This.fileSystemWatcher.addSourceFolders (new Classpathfolders (URLs));
}
When you create the class, you need to provide the factory class for FileSystemWatcher, patternclasspathrestartstrategy Restart the policy class, and the classpath to be monitored url[].
In the construction method, the FileSystemWatcher is obtained through the factory class, the current restart policy is set, and the url[] array is wrapped by classpathfolders. Then set FileSystemWatcher to monitor these directories (FileSystemWatcher via Iterator interface and classpathfolders decoupling).
The Classpathfilesystemwatcher also implements the Initializingbean interface and Applicationcontextaware interface, where the Setapplicationcontext method Before the Afterpropertiesset method is executed, the two methods are implemented as follows:
@Override public
void Setapplicationcontext (ApplicationContext applicationcontext)
throws Beansexception {
this.applicationcontext = ApplicationContext;
}
@Override public
void Afterpropertiesset () throws Exception {
if (this.restartstrategy! = null) {
FileSystemWatcher watchertostop = null;
if (this.stopwatcheronrestart) {
watchertostop = this.filesystemwatcher;
}
This.fileSystemWatcher.addListener (New Classpathfilechangelistener (
This.applicationcontext, This.restartstrategy, Watchertostop));
}
This.fileSystemWatcher.start ();
}
Although here will judge Restartstrategy, but devtools default configuration is to provide this policy, regardless of whether you configure the exclusion directory, will provide this policy, only provide this policy, there will be Classpathfilechangelistener, Subsequent listener classpathchangedevent events will work. After all the bean properties have been set (Afterpropertiesset), This.fileSystemWatcher.start () is started.
Now that the classpath has been monitored, we need to know when Classpathfilesystemwatcher was created, where Classpathchangedevent listens, and how it will continue to execute when changes occur.
Not to be continued ...