org.springframework.boot.devtools.restart

Class Restarter



  • public class Restarter
    extends Object
    Allows a running application to be restarted with an updated classpath. The restarter works by creating a new application ClassLoader that is split into two parts. The top part contains static URLs that don't change (for example 3rd party libraries and Spring Boot itself) and the bottom part contains URLs where classes and resources might be updated.

    The Restarter should be initialized early to ensure that classes are loaded multiple times. Mostly the RestartApplicationListener can be relied upon to perform initialization, however, you may need to call initialize(String[]) directly if your SpringApplication arguments are not identical to your main method arguments.

    By default, applications running in an IDE (i.e. those not packaged as "fat jars") will automatically detect URLs that can change. It's also possible to manually configure URLs or class file updates for remote restart scenarios.

    Since:
    1.3.0
    Author:
    Phillip Webb, Andy Wilkinson
    See Also:
    RestartApplicationListener, initialize(String[]), getInstance(), restart()
    • Constructor Detail

      • Restarter

        protected Restarter(Thread thread,
                            String[] args,
                            boolean forceReferenceCleanup,
                            RestartInitializer initializer)
        Internal constructor to create a new Restarter instance.
        Parameters:
        thread - the source thread
        args - the application arguments
        forceReferenceCleanup - if soft/weak reference cleanup should be forced
        initializer - the restart initializer
        See Also:
        initialize(String[])
    • Method Detail

      • initialize

        protected void initialize(boolean restartOnInitialize)
      • addUrls

        public void addUrls(Collection<URL> urls)
        Add additional URLs to be includes in the next restart.
        Parameters:
        urls - the urls to add
      • addClassLoaderFiles

        public void addClassLoaderFiles(ClassLoaderFiles classLoaderFiles)
        Add additional ClassLoaderFiles to be included in the next restart.
        Parameters:
        classLoaderFiles - the files to add
      • getThreadFactory

        public ThreadFactory getThreadFactory()
        Return a ThreadFactory that can be used to create leak safe threads.
        Returns:
        a leak safe thread factory
      • restart

        public void restart()
        Restart the running application.
      • restart

        public void restart(FailureHandler failureHandler)
        Restart the running application.
        Parameters:
        failureHandler - a failure handler to deal with application that doesn't start
      • start

        protected void start(FailureHandler failureHandler)
                      throws Exception
        Start the application.
        Parameters:
        failureHandler - a failure handler for application that won't start
        Throws:
        Exception - in case of errors
      • relaunch

        protected Throwable relaunch(ClassLoader classLoader)
                              throws Exception
        Relaunch the application using the specified classloader.
        Parameters:
        classLoader - the classloader to use
        Returns:
        any exception that caused the launch to fail or null
        Throws:
        Exception - in case of errors
      • stop

        protected void stop()
                     throws Exception
        Stop the application.
        Throws:
        Exception - in case of errors
      • removeAttribute

        public Object removeAttribute(String name)
      • getInitialUrls

        public URL[] getInitialUrls()
        Return the initial set of URLs as configured by the RestartInitializer.
        Returns:
        the initial URLs or null
      • disable

        public static void disable()
        Initialize and disable restart support.
      • initialize

        public static void initialize(String[] args,
                                      boolean forceReferenceCleanup,
                                      RestartInitializer initializer,
                                      boolean restartOnInitialize)
        Initialize restart support for the current application. Called automatically by RestartApplicationListener but can also be called directly if main application arguments are not the same as those passed to the SpringApplication.
        Parameters:
        args - main application arguments
        forceReferenceCleanup - if forcing of soft/weak reference should happen on each restart. This will slow down restarts and is intended primarily for testing
        initializer - the restart initializer
        restartOnInitialize - if the restarter should be restarted immediately when the RestartInitializer returns non null results
      • clearInstance

        public static void clearInstance()
        Clear the instance. Primarily provided for tests and not usually used in application code.