@Configurable
@Configurable() lets the container inject beans and settings into an object it did not create.
Why
Some objects are created by a framework, not by the container: a Flutter State, an object built by a parser, a plugin loaded at run time. They still need beans and settings.
Mark the class @Configurable(). The generator writes an injector for its @Autowired and @Value members. Once the object exists, call autowireBean on the context's autowireCapableBeanFactory, and the members are filled in.
The class is not a bean. The container does not keep it and does not hand it to others.
Related methods of AutowireCapableBeanFactory:
autowireBean(object): injects the@Autowiredand@Valuemembers;initializeBean(object, name): runs the aware callbacks, the post-processors and@PostConstruct;destroyBean(object): runs@PreDestroy, for example when a FlutterStateis disposed.
Example
import 'package:spring_core_dart/spring_core_dart.dart';
class User {
User(this.name);
final String name;
}
@Service()
class UserService {
User current() => User('Ada');
}
// Created by Flutter (or any other framework), not by the container.
@Configurable()
class ProfileScreenState {
@Autowired()
late final UserService users;
@Value(r'${app.title:Profile}')
late final String title;
String headline() => '$title: ${users.current().name}';
}
const log = SpringLog('app');
void main() {
final context = ApplicationContext.run([appBeans]);
// The framework created the object; fill in its beans and settings.
final state = ProfileScreenState();
context.autowireCapableBeanFactory.autowireBean(state);
log.info(state.headline());
context.close();
}
In a Flutter State, call autowireBean(this) in initState and destroyBean(this) in dispose.
Parameters
None.
In Spring (Java)
@Configurable
class ProfileScreenState {
@Autowired UserService users;
}
// With AspectJ weaving, `new ProfileScreenState()` is injected
// automatically. Without it:
context.getAutowireCapableBeanFactory().autowireBean(state);
Difference that matters: Spring weaves @Configurable classes with AspectJ so that new triggers the injection. Dart has no weaving, so the autowireBean call is always explicit.