Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

StaticApplicationContext is a Spring ApplicationContext implementation for assembling beans and messages directly in Java rather than loading bean definitions from external configuration. Spring documents it as mainly useful for testing, where a small, controlled context can serve as a fixture.

What is StaticApplicationContext in Spring?

org.springframework.context.support.StaticApplicationContext is a subclass of GenericApplicationContext. Its purpose is to let you register beans and messages programmatically. It still participates in Spring’s ApplicationContext contract, which includes bean access, resource loading, event publication, message resolution for internationalization, and parent-context inheritance. Spring’s Javadoc describes it as mainly useful for testing.

This makes it a focused choice when a test or small program needs a context assembled in code. It is not presented as a general replacement for XML, annotated, or other external configuration mechanisms.

How do you register beans and messages?

Create the context, register the beans and messages needed by the test, then refresh it before using it. The following illustrates the documented APIs; it is not a claim that this exact snippet was executed:

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
StaticApplicationContext context = new StaticApplicationContext();
context.registerSingleton("service", MyService.class);
context.registerPrototype("worker", Worker.class);
context.addMessage("greeting", Locale.US, "Hello");
context.refresh();

The no-argument constructor creates a context without a parent. A second constructor accepts an existing ApplicationContext as its parent. The class API documents these constructors and registration methods.

Register singleton beans

Call registerSingleton(name, beanClass) to register a singleton bean class. To provide property values at registration, use registerSingleton(name, beanClass, propertyValues), where the third argument is a MutablePropertyValues.

Register prototype beans

Call registerPrototype(name, beanClass) to register a prototype bean class. The overload accepting MutablePropertyValues lets you provide property values during registration.

Add messages for a locale

Use addMessage(code, locale, defaultMessage) to associate a message code and locale with a default message. You can also call getStaticMessageSource() to access the context’s internal StaticMessageSource directly.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

When is this context a good fit?

Use StaticApplicationContext when you want a deliberately small Spring context assembled directly in Java—for example, a test that needs only a known set of beans and messages. Its documented testing purpose makes it useful as a controlled fixture, not as a statement that it is the right context for every application.

Before choosing it, consider:

  • Configuration style: Do you want registrations written directly in Java, or do you need XML, annotations, or another external configuration approach?
  • Lifecycle: Can your test control when registration happens and when the context is refreshed?
  • Scope: Does each registration need singleton or prototype behavior?
  • Hierarchy: Should the context inherit from a parent ApplicationContext?
  • Messages: Do you need test-specific messages registered through StaticMessageSource?
  • Test scope: Is a narrow, deterministic fixture appropriate for what the test exercises?
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

What should you know about lifecycle and inherited behavior?

StaticApplicationContext inherits behavior from GenericApplicationContext and AbstractApplicationContext, including bean-definition registration and refresh-related lifecycle operations. The class also overrides assertBeanFactoryActive() as a no-op, making it more lenient toward test cases. For exact behavior in a particular Spring release, consult the Javadoc for that release rather than assuming every version has identical details.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.