Hot reload

A hot reload (r in flutter run, saving in the IDE) puts the new code into the running app without starting it again: main does not run, and the objects in memory stay. For beans that is a problem. The application context was built from the old generated code, so a new @Component would be missing and a changed one would keep its old dependencies.

SpringApplicationWidget takes care of it, as Spring Boot DevTools' restart does. It is on by default and needs no setup.

Why

Without it, every change to a bean or to a configuration file would need a hot restart (R), which loses the navigation, the scroll positions and the app process. With it, a change of a method body keeps everything, and a change of beans or files restarts only the context.

How it decides

Flutter calls the root widget's reassemble() on every hot reload. There the widget compares the fingerprint of the generated code (lib/spring_gen/main.spring.debug.dart, compiled in debug and profile builds only) with the one the running context was built from.

Only method bodies changed: the context stays. The fingerprint is the same, so nothing restarts. Dart already runs the new method bodies, and the beans keep their state.

@ChangeNotifierComponent()
class Counter {
  int value = 0;
  void increment() => value += 2; // was `value++`: after `r` the next tap
                                  // adds 2, and the count so far is kept
}

Beans, annotations or lib/resources/ changed: the context restarts. With dart run build_runner watch running, the generator rewrites main.spring.dart a moment after you save. The next hot reload sees a new fingerprint, and the widget:

  1. closes the running context (@PreDestroy methods run, lifecycle beans stop, ContextClosedEvent);
  2. starts the application again with the new beans and configuration files. The ApplicationRunners do not run again, unless rerunRunnersOnReload: true: the app did not really start again;
  3. builds the widgets below it afresh, their States included, so no widget keeps a bean of the closed context. Navigation and scroll positions start over, as after a hot restart, but the app process (and anything outside the context) stays.
// Added while the app runs; `build_runner watch` regenerates, then `r`:
@Component()
class Clock {
  DateTime now() => DateTime.now();
}

// and in a widget:
Text('${context.bean<Clock>().now()}'); // found: the context restarted
# application.yml: no code changed, still a restart with the new value
shop:
  page-size: 40

What a hot reload cannot do

Turning it off

A context restart drops the state of every bean. To keep the context as it is after every hot reload (and restart by hand with R when beans changed), set Spring Boot DevTools' switch:

spring:
  devtools:
    restart:
      enabled: false
Key Type Default Meaning
spring.devtools.restart.enabled bool true A hot reload restarts the context when the generated code changed: beans, annotations or lib/resources/. false keeps the context and the beans' state until a hot restart
SpringApplicationWidget parameter Default Meaning
rerunRunnersOnReload false Whether ApplicationRunner and CommandLineRunner beans run again after a hot reload restarts the context

Release and profile builds have no hot reload, so none of this happens there.

In Spring (Java)

Spring Boot DevTools restarts the application when the classpath changes, with two class loaders. Here the generated code's fingerprint plays that role, and only the context restarts, not the app.

See also