context library
The application context: the container that creates an application's beans, runs their lifecycle and connects them through events and messages.
Import it for the annotations that declare beans (@Component,
@Configuration, @Bean, @Profile, @EventListener, ...), for
ApplicationContext and its standard implementation, for application
events, lifecycle beans, conditions and message sources (MessageSource
and messages.properties files). Most programs import
spring_core_dart.dart, which includes this library; a Spring Boot
application gets its context from SpringApplication.run instead of
creating one.
// A small program without Spring Boot: start the beans, use them, stop.
const log = SpringLog('app');
final context = ApplicationContext.run([appBeans]);
context.addListener<UserCreated>((event) => log.info(event.name));
context.publishEvent(UserCreated('Ada'));
log.info(context.getMessage('greeting', args: ['Ada'], locale: 'en'));
context.close();
Spring (Java), the spring-context module:
var context = new AnnotationConfigApplicationContext(AppConfig.class);
context.addApplicationListener(
(PayloadApplicationEvent<UserCreated> e) -> ...);
context.publishEvent(new UserCreated("Ada"));
context.getMessage("greeting", new Object[] {"Ada"}, Locale.ENGLISH);
context.close();
Classes
- AbstractMessageSource
- The base class for message sources: it does the locale fallbacks, the formatting and the parent lookup, so that a subclass only says where the texts are.
- AnnotationConfigApplicationContext
-
The application context of this package: it builds its beans from the
BeanModules the generator writes for
@ComponentScan. - ApplicationContext
- A running application's container: it holds the beans and gives access to the configuration, events, translated messages and resource files.
- ApplicationContextAware
- Implemented by a bean that wants the application context handed to it through a method call.
- ApplicationContextAwareProcessor
-
The bean post-processor that calls the
*Awarecallbacks of this library on each bean, with the context's objects. - ApplicationContextEvent
- The base class of the events a context publishes about itself: refreshed, started, stopped, paused, restarted, closed.
- ApplicationEvent
- An optional base class for application events: it records which object raised the event and when.
- ApplicationEventMulticaster
- The part of the context that holds the listeners and delivers each published event to those that accept it.
- ApplicationEventPublisher
- Sends an event to every listener interested in it.
- ApplicationEventPublisherAware
- Implemented by a bean that wants the event publisher handed to it through a method call.
-
ApplicationListener<
E extends Object> - A bean that listens to one type of event by implementing an interface.
-
ApplicationListenerAdapter<
E> -
A GenericApplicationListener made from a function or an
ApplicationListener, for events (or published objects) of type
E. - Async
-
Runs an
@EventListenermethod on aTaskExecutorinstead of in the publishing call. - Autowired
- Asks the container to inject beans into a field, setter, method or constructor.
- Bean
- Marks a method whose return value is a bean.
- BeanMethodAnnotation
- Marks an annotation class whose methods the runtime can find on beans.
- BeanModule
- Everything the generator found in one scanned directory: the bean definitions, plus the property files, imports and embedded resources that come with them.
- BeanRegistrar
- Code that registers beans, written by hand instead of with annotations.
- BeanRegistry
- Where a BeanRegistrar registers its beans: a short API that turns each call into a bean definition of the context.
- Component
- Marks a class as a bean: an object the container creates once, injects into others and closes at shutdown.
- ComponentScan
- Generates a module with every bean declared in the directory of the annotated file and its subdirectories.
- Condition
- A rule that decides whether a bean is registered at all.
- Conditional
-
Registers a bean only when every listed
Conditionmatches. - ConditionContext
- What a Condition can look at to decide: the configuration, the bean definitions registered so far, resource files, and the definition being decided.
- ConditionEvaluation
- The outcome of one condition evaluated while a context loaded its modules: which bean or class it guarded, and whether it matched.
- ConditionEvaluationCache
- A ConditionContext that remembers results while the modules load, so a ConditionGroup is decided only once.
- ConditionEvaluationRecorder
- Receives the outcome of every condition evaluated, for ConditionEvaluation reports.
- ConditionGroup
- The conditions of one configuration class, decided once for the whole class, so that the class is registered all or nothing.
- Configurable
-
Lets the container inject an object it did not create, such as a
Flutter
State. - ConfigurableApplicationContext
- An application context that can also be set up, started, paused and closed.
- Configuration
- Marks a class whose Bean methods create beans.
- ConfigurationCondition
- A Condition decided in one ConfigurationPhase only; plain conditions count in both.
- ContextClosedEvent
- Published when a context starts closing, while every bean is still available.
- ContextPausedEvent
-
Published after
pause()(orpauseAsync()) was called on a context: the pauseable lifecycle beans are stopped, for example while a Flutter app is in the background. - ContextRefreshedEvent
- Published once a context has started: every eager singleton exists and the auto-startup lifecycle beans run.
- ContextRestartedEvent
-
Published after
restart()was called on a context: the auto-startup lifecycle beans run again, typically after a pause. - ContextStartedEvent
-
Published after
start()was called on a context and its lifecycle beans were started. - ContextStoppedEvent
-
Published after
stop()was called on a context and its running lifecycle beans were stopped. - Controller
- Marks a class as a bean of the presentation layer.
- DefaultLifecycleProcessor
- The LifecycleProcessor every context uses unless it declares its own: it starts the lifecycle beans phase by phase, lowest first, and stops them in the reverse order.
- DefaultMessageRendering
-
Implemented by a MessageSourceResolvable whose default message must
not go through
MessageFormat. - DefaultMessageSourceResolvable
- A ready-made MessageSourceResolvable: codes, arguments and a default message given as values.
- DeferredImportSelector
- An ImportSelector that runs after every other module and component has been loaded.
- DelegatingMessageSource
- A message source with no messages of its own: it asks its parent, or fails when there is none.
- DependsOn
- Names beans that must be created before this one and destroyed after it.
- Description
- A human-readable description of a bean.
- EmbeddedValueResolverAware
-
Implemented by a bean that resolves
@Value-style texts itself: it receives a function that resolves${...}placeholders and#{...}expressions. - EnvironmentAware
- Implemented by an object that wants the configuration (properties and profiles) handed to it through a method call.
- EventListener
- Marks a bean method that the context calls for each published event of its parameter type.
-
EventListenerMethod<
T extends Object> -
One
@EventListenermethod of a bean classT, described by generated code: which events it takes, its condition, its order and how to call it. -
EventResultType<
R extends Object> -
The declared result type
Rof an@EventListenermethod (the element type of anIterableresult), so that its results are published asR. - Fallback
- Makes this bean the one used only when no other bean of the type is there.
- Filter
- A rule that selects classes for ComponentScan.includeFilters or ComponentScan.excludeFilters.
- GenericApplicationListener
- A listener that receives any event and decides at runtime, per event, whether to handle it.
- HierarchicalMessageSource
- A MessageSource that asks a parent message source for the codes it does not have.
- Import
- Adds beans that the component scan does not find: configuration classes from elsewhere, or code that registers beans.
- ImportBeanDefinitionRegistrar
- Code that registers full bean definitions, with access to the ones already registered.
- ImportDeclaration
-
One entry of
@Importthat names code registering beans (a BeanRegistrar, ImportSelector or ImportBeanDefinitionRegistrar), recorded in BeanModule.registrars. - ImportSelector
- Code that chooses, at startup, which modules of beans to add.
- Inject
- Asks the container to inject a field, setter, method or constructor; the standard Java (JSR-330) name for Autowired.
- Lazy
- Delays the creation of a bean, or of what an injection point receives, until it is first used.
- Lifecycle
- A bean that runs something in the background between a start and a stop, such as a timer, a server or a connection.
- LifecycleProcessor
- The part of the context that starts and stops its Lifecycle beans, at refresh, pause, restart and close.
- ListenerRegistration
-
What
ApplicationContext.addListenerreturns: call remove to stop the listener. - Locales
-
Helpers for locales written as strings, such as
de_CH. - Lookup
- Marks a method that returns a bean of the container on every call.
- MessageFormat
-
Fills arguments into a message pattern such as
Hello {0}!, using the syntax of Java'sMessageFormat, which message files are written in. - MessageSource
- Turns a message code and a locale into a text in that language, with the arguments filled in.
- MessageSourceAccessor
- A wrapper around a MessageSource that remembers a locale, so that lookups need not pass it every time.
- MessageSourceAware
- Implemented by a bean that wants the translated messages handed to it through a method call.
- MessageSourceResolvable
- A message described by codes rather than text: the codes to look up, the arguments to fill in and a default text.
- Named
- Marks a class as a bean, or selects a bean at an injection point; the standard Java (JSR-330) name for Component and Qualifier.
-
PayloadApplicationEvent<
T extends Object> - The envelope the context puts around a published object that is not an ApplicationEvent, adding a source and a timestamp.
- PostConstruct
- Marks a method the container calls once a bean is fully injected, before anyone uses it.
- PreDestroy
- Marks a method the container calls when a bean is destroyed: when the context closes for a singleton, when its scope ends for a scoped bean.
- Primary
- Makes this bean the one injected when several beans have the requested type.
- Profile
- Registers a bean only when the active profiles match.
- ProfileCondition
-
The condition behind
@Profile: it matches when one of its profile expressions matches the active profiles. - PropertySource
- Adds property files to the environment, on top of the application's configuration files.
- PropertySourceDeclaration
-
One
@PropertySourceannotation, recorded in a BeanModule: which property files to add to the environment, and when. - Qualifier
- Picks one bean among several of the same type, by a label or a bean name.
- ReloadableResourceBundleMessageSource
-
A message source that reads its texts from
.propertiesfiles, one file per language, and can re-read them when they change. - Repository
- Marks a class as a bean that stores and loads data.
- ResourceLoaderAware
-
Implemented by an object that wants the context's resource loader (to
read files such as
classpath:templates/mail.txt) handed to it through a method call. - Scope
- Sets how many instances of a bean exist and how long each one lives.
- Service
- Marks a class as a bean that holds business logic.
- SimpleApplicationEventMulticaster
- The standard ApplicationEventMulticaster: it calls the accepting listeners in order, in the publishing call or on a task executor.
- SimpleAsyncTaskExecutor
-
A TaskExecutor that runs each task in a later event-loop turn
(
Timer.run), so the caller goes on first. - SmartApplicationListener
- A GenericApplicationListener that decides by the runtime types of the event and its source, and has an order among the listeners.
- SmartLifecycle
- A Lifecycle bean that the context starts by itself at the end of the refresh and stops in order at shutdown.
- StaticMessageSource
- A message source whose messages are added in code rather than read from files.
- SyncTaskExecutor
- A TaskExecutor that runs each task right away, in the calling code.
- TaskExecutor
- Something that runs a task now or later, so that the code handing it over does not have to wait.
- Value
- Injects a value from the configuration (properties, environment variables) or from an expression, instead of a bean.
Enums
- ConfigurationPhase
- The moment of loading at which a ConfigurationCondition is decided.
- FilterType
- How a Filter decides whether a class matches.
Functions
-
conditionsMatch(
Iterable< Condition> conditions, ConditionContext context, {ConfigurationPhase? phase, String? source}) → bool -
Whether every one of
conditionsmatches; the context calls it for each definition while the modules load. -
hasPhaseCondition(
Iterable< Condition> conditions, ConfigurationPhase phase) → bool -
Whether
conditions(also inside a ConditionGroup) include a ConfigurationCondition ofphase; the context uses it to tell which definitions must wait for theregisterBeanphase.
Exceptions / Errors
- ApplicationContextException
- The error a context throws when one of its own steps fails, as opposed to an error in creating a bean.
- NoSuchMessageException
- Thrown when a message code is not found and no default message was given.