A running application's container: it holds the beans and gives access to the configuration, events, translated messages and resource files.
Once started, the context has created every bean, injected each one's dependencies and called their init methods. It is also the place where the rest of the framework meets: beans are looked up here (getBean), events are published to listeners (publishEvent), messages are translated (getMessage) and files are found (getResource). It falls back to a parent context for beans and messages it does not have itself.
Most application code never touches it: beans receive what they need
by injection (an ApplicationEventPublisher, a MessageSource, the
Environment). You meet the context in main, where
ApplicationContext.run or Spring Boot's SpringApplication.run
returns it; in tests; and in code outside the container that needs a
bean, such as a Flutter widget. A bean can also inject it. This
interface only reads; starting and stopping is on
ConfigurableApplicationContext.
// In main: start the beans, then use a bean and the context's services.
final context = ApplicationContext.run([coreBeans, authBeans]);
final auth = context.getBean<AuthFacade>();
context.publishEvent(UserLoggedIn('ada'));
final title = context.getMessage('app.title', locale: 'de');
Spring (Java):
ApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
AuthFacade auth = context.getBean(AuthFacade.class);
context.publishEvent(new UserLoggedIn("ada"));
String title = context.getMessage("app.title", null, Locale.GERMAN);
Properties
- autowireCapableBeanFactory → AutowireCapableBeanFactory
-
The factory that injects objects the container did not create, such
as a
@ConfigurableFlutterState(autowireBean).no setter - beanDefinitionCount → int
-
How many definitions are registered here (objects added with
registerSingletonare not counted).no setterinherited -
beanDefinitionNames
→ List<
String> -
The names of the definitions registered here, in registration order.
Objects added with
registerSingletonare not definitions and are not listed.no setterinherited - displayName → String
-
The name used in error messages and
toString: the id, or the class name with the id when one was given explicitly.no setter - environment → Environment
-
The configuration the context was built with: properties from all
sources, the active profiles and the type conversion used for them.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- id → String
-
A name that tells this context apart from others in logs, for example
a parent and a child.
no setter
- parent → ApplicationContext?
-
The context this one extends, or
null.no setter - parentBeanFactory → BeanFactory?
-
The factory consulted when a lookup finds nothing here;
nullfor a root factory.no setterinherited - runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
- startupDate → DateTime?
-
When the refresh started, for uptime reports;
nullbefore.no setter
Methods
-
addListener<
E> (void listener(E event), {int order}) → ListenerRegistration -
Calls
listenerfor every event published from now on whose type, or payload type, isE. -
containsBean(
String name) → bool -
Whether a bean or alias named
nameexists here or in a parent (&nameonly for a factory bean). Creates nothing.inherited -
containsBeanDefinition(
String name) → bool -
Whether a definition is registered here under exactly
name. Aliases, parent beans and objects added withregisterSingletondo not count.inherited -
containsBeanOfType(
Type type, {String? qualifier, bool includeAncestors = true}) → bool -
Whether injecting
typewould find a bean, without creating any.inherited -
containsLocalBean(
String name) → bool -
Whether this factory itself, ignoring the parent, has a bean or alias
named
name(&nameonly for a factory bean).inherited -
findAnnotationOnBean<
A extends Object> (String beanName) → A? -
The first annotation of type
Aon the class (or@Beanmethod) of the bean namedbeanName, ornull. Creates no bean. Looks in the parent when the bean is not known here, and throwsNoSuchBeanDefinitionExceptionwhen no factory has it. Spring:findAnnotationOnBean.inherited -
findBean<
T extends Object> ({String? qualifier}) → T? -
Like getBean, but returns
nullwhen no bean matches, for a collaborator the caller can do without. Still throwsNoUniqueBeanDefinitionExceptionwhen several match and none wins. Spring:getBeanProvider(T).getIfAvailable().inherited -
getAliases(
String name) → List< String> -
The other names of the bean named
name: its aliases, plus its bean name whennameis itself an alias. Empty when there are none or no bean has that name.inherited -
getBean<
T extends Object> ({String? qualifier}) → T -
The bean of type
T, created if it does not exist yet. Withqualifier, only the bean with that name, alias or@Qualifiervalue.inherited -
getBeanByName(
String name) → Object -
The bean registered under
nameor one of its aliases, created if needed; looks in the parent when the name is not known here.inherited -
getBeanNamesForAnnotation<
A extends Object> () → List< String> -
The names of this factory's definitions whose class (for a
@Beanmethod: the method or the returned class) carries an annotation of typeAor of a subclass ofA, so asking forComponentalso finds@Servicebeans. Creates no bean. Spring:getBeanNamesForAnnotation.inherited -
getBeanNamesForType(
Type type, {bool includeNonSingletons = true}) → List< String> -
The names of this factory's beans that can be used as
type, in registration order: definitions first, then objects added withregisterSingleton. Creates no bean.inherited -
getBeanProvider<
T extends Object> ({String? qualifier}) → ObjectProvider< T> -
A handle on the beans of
Tthat creates nothing until it is used.inherited -
getBeansOfType<
T extends Object> ({bool includeNonSingletons = true}) → Map< String, T> -
This factory's beans of
Tby name, in the order of getBeanNamesForType; creates those that do not exist yet. A bean whose@Beanmethod returnednullis left out.inherited -
getBeansWithAnnotation<
A extends Object> () → Map< String, Object> -
The beans named by getBeanNamesForAnnotation, by name; creates them.
Spring:
getBeansWithAnnotation.inherited -
getMessage(
String code, {List< Object?> args = const [], String? defaultMessage, String? locale}) → String -
The text for
codeinlocale, withargsfilled into its{0},{1}, ... placeholders.inherited -
getMessageFromResolvable(
MessageSourceResolvable resolvable, {String? locale}) → String -
The text for the first code of
resolvablefound inlocale, with its arguments filled in; else its default message; else a NoSuchMessageException. Use it to show validation errors and other messages described by codes.inherited -
getResource(
String location) → Resource -
The resource at
location. Nothing is read yet, and the resource may not exist: check Resource.exists before reading when it may be missing. Never returnsnull; an unknown prefix gives a resource that does not exist.inherited -
getResources(
String locationPattern) → List< Resource> -
All resources matching
locationPattern, possibly none. A location without wildcards gives that one resource when it exists.inherited -
getSortedBeans<
T extends Object> ({String? qualifier}) → List< T> -
Every bean of
There and in the parent factories, sorted to be used in order:PriorityOrderedbeans first, then byOrdered.order,@Orderor jakarta@Priority(lower first), then by registration.inherited -
getType(
String name) → Type? -
The type of the bean named
name, without creating it: the declared class (or what aSmartInstantiationAwareBeanPostProcessorpredicts), the product type for a factory bean (&name: the factory's class), the runtime type of an object added withregisterSingleton.nullwhen no bean has that name.inherited -
isPrototype(
String name) → bool -
Whether every lookup of the bean named
namecreates a new instance. For aFactoryBean, whether its product is created per lookup (this creates the factory);&nameasks about the factory itself. A bean in a custom scope is neither a singleton nor a prototype. ThrowsNoSuchBeanDefinitionExceptionfor an unknown name.inherited -
isSingleton(
String name) → bool -
Whether the bean named
nameis one shared instance. For aFactoryBean, whether its product is shared (this creates the factory);&nameasks about the factory itself. ThrowsNoSuchBeanDefinitionExceptionfor an unknown name.inherited -
isTypeMatch(
String name, Type type) → bool -
Whether the bean named
namecan be used as atype, judged from the types its definition declares, without creating it.inherited -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
publishEvent<
T extends Object> (T event) → void -
Delivers
eventto every listener that accepts it. By default the listeners run before this call returns, and a listener error reaches the caller; with an asynchronous multicaster they run later. Aneventthat is not anApplicationEventis wrapped in aPayloadApplicationEvent<T>.inherited -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
Static Methods
-
run(
List< BeanModule> modules, {ApplicationContext? parent, ConfigurableEnvironment? environment, ResourceLoader? resourceLoader, String? id}) → ConfigurableApplicationContext -
Creates an AnnotationConfigApplicationContext for
modules, starts it with ConfigurableApplicationContext.refresh and returns it. -
runAsync(
List< BeanModule> modules, {ApplicationContext? parent, ConfigurableEnvironment? environment, ResourceLoader? resourceLoader, String? id}) → Future<ConfigurableApplicationContext> - Like run, but starts the context with ConfigurableApplicationContext.refreshAsync and completes once it is running.