builder library

The build_runner entry points of spring_core_dart_generator.

build_runner finds the factories here through the package's build.yaml; applications do not import this library. The generator turns annotated Dart classes into plain Dart code at build time, because Dart AOT and Flutter have no dart:mirrors to discover them at runtime:

  • A library with a @ComponentScan() variable gets a sibling *.spring.dart file with a BeanModule: one BeanDefinition per bean found in its directory, plus the generated AOP proxy classes, introspection metadata and @Configurable configurers.
  • A main annotated with @SpringBootApplication() (from spring_boot_dart) gets the same for the whole package, plus springApplication and runApplication(args).
  • A test file with @SpringBootTest classes gets a test/spring/<name>.test.spring.dart file with runSpringTests().
  • A Flutter app with spring.flutter.web.* properties in lib/resources/ gets a web_dev_config.yaml, which sets the host, port, headers and proxy of flutter run on the web.

Setup, in the application's package:

# pubspec.yaml
dependencies:
  spring_core_dart: any
dev_dependencies:
  build_runner: any
  spring_core_dart_generator: any
// lib/src/beans.dart
import 'package:spring_core_dart/spring_core_dart.dart';
import 'package:my_app/src/beans.spring.dart';

// Every bean class under lib/src/ ends up in this module.
@ComponentScan()
final BeanModule appBeans = $appBeans;

Then run dart run build_runner build and pass appBeans to the application context, for example ApplicationContext.run([appBeans]).

Spring (Java):

// Spring discovers beans at runtime: ClassPathBeanDefinitionScanner
// reads class files, ConfigurationClassPostProcessor parses @Bean
// methods and reflection creates the instances.
var context = new AnnotationConfigApplicationContext();
context.scan("com.example.app");
context.refresh();
// Spring 6 AOT (BeanFactoryInitializationAotProcessor) can instead
// generate Java source at build time, which is what this builder
// always does for Dart.

Functions

springCoreDartBuilder(BuilderOptions options) → Builder
Creates the main builder (SpringBuilder), which writes the *.spring.dart files; build.yaml names it in builder_factories.
springFlutterWebBuilder(BuilderOptions options) → Builder
Creates the builder of web_dev_config.yaml (WebDevConfigBuilder), registered in build.yaml as spring_flutter_web.
springTestBuilder(BuilderOptions options) → Builder
Creates the test builder (SpringTestBuilder), registered in build.yaml as spring_test.