spring_core_dart
The core package of Spring for Dart. You declare classes as beans with
annotations, and a container creates them, wires them together and
configures them. The wiring code is written by spring_core_dart_generator
at build time, so nothing is looked up by reflection at run time.
dependencies:
spring_core_dart: ^0.7.1
dev_dependencies:
build_runner: ^2.17.0
spring_core_dart_generator: ^0.7.1
import 'package:spring_core_dart/spring_core_dart.dart';
import 'main.spring.dart';
@Service()
class Greeter {
Greeter(@Value(r'${greeting:Hello}') this.greeting);
final String greeting;
}
@ComponentScan()
final BeanModule appBeans = $appBeans;
const log = SpringLog('app');
void main() {
final context = ApplicationContext.run([appBeans]);
log.info(context.getBean<Greeter>().greeting);
context.close();
}
Run dart run build_runner build once, and again after you change the
annotations, to write main.spring.dart.
What is inside
- Container and beans: the annotations that declare beans, inject them and manage their life, and the context that runs them.
- Configuration: values from properties, YAML, the environment and the command line; profiles; typed settings.
- Events: beans that publish and listen to events, synchronously or not.
- Messages: texts in several languages, with arguments.
- Resources: files, assets and URLs behind one interface; path patterns.
- Type conversion: strings to numbers, durations, sizes and your own types.
- Logging:
SpringLog, levels and the line format. - Expressions (SpEL):
#{...}expressions in annotations and in code. - JSON: objects to JSON and back, with annotations.
- Validation: constraints on fields and parameters, and the validator.
- Aspects (AOP): code that runs around the methods of beans.
Libraries
package:spring_core_dart/spring_core_dart.dart exports everything. A
library that needs only one part can import it alone: core.dart,
beans.dart, context.dart, expression.dart, json.dart,
validation.dart, aop.dart. The Jakarta names (@Inject, @Named)
are in jakarta.dart.