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

The Spring IoC container creates and assembles application objects called beans. XML is one way to describe those beans and their dependencies; it does not change how the container works. Define beans in a <beans> document, use ref to connect beans and value for literal settings, then load the file with an application context.

What is the Spring IoC container?

Spring’s Inversion of Control (IoC) container manages application objects, known as beans. Instead of each object constructing or locating its own dependencies, the container supplies them. That supply of collaborators is called dependency injection (DI), and it can happen through constructor arguments, factory-method arguments or properties.

BeanFactory is Spring’s basic contract for configuring and managing beans. ApplicationContext extends it with features including integration with AOP, message resources, event publication and application-specific context support. For most standalone applications that load XML, ClassPathXmlApplicationContext is a common choice. Spring container overview

How XML bean definitions work

Spring XML is configuration metadata. A top-level <beans> element contains one or more <bean> definitions. Spring reads those definitions as BeanDefinition metadata: records of a bean’s class, identity, dependencies and options such as scope, lazy initialization and lifecycle callbacks. A bean definition describes how Spring should create and configure an object; the object itself is the bean.

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.

A definition typically names the package-qualified class and gives the bean an id or name. It can also declare aliases, constructor arguments, properties, autowiring and initialization or destruction callbacks. Spring bean definition reference

Define beans and inject dependencies in XML

This example declares a finder bean and injects it into a lister through its constructor:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
                           https://www.springframework.org/schema/beans/spring-beans.xsd">
  <bean id="movieFinder" class="example.MovieFinder"/>
  <bean id="movieLister" class="example.SimpleMovieLister">
    <constructor-arg ref="movieFinder"/>
  </bean>
</beans>

For constructor injection, the ref attribute points to another bean by its identifier. Spring can also express the reference as a nested element, <constructor-arg><ref bean="movieFinder"/></constructor-arg>.

Use a property for setter injection

If the class exposes a writable property named movieFinder, inject the same collaborator with:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
<property name="movieFinder" ref="movieFinder"/>

The property’s name must match the JavaBean property Spring is to set. Use ref when the value is another bean; use value for a literal, such as a timeout:

<property name="timeout" value="30"/>

Spring’s XML reference covers constructor arguments, properties, bean references and literal values. Spring dependency and collaborator reference

Load the XML file in a standalone application

Place applicationContext.xml on the application classpath, then create an application context and request a bean:

try (var context = new ClassPathXmlApplicationContext("applicationContext.xml")) {
    var lister = context.getBean("movieLister", SimpleMovieLister.class);
}

Creating the context makes Spring read the XML metadata and assemble the configured application. The context is closed automatically here when the try-with-resources block ends.

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

When Spring creates beans and reports wiring errors

An ApplicationContext validates configuration as it starts. Singleton beans are pre-instantiated by default, so Spring generally creates them during context initialization and many missing-reference or wiring errors appear at startup. A bean marked for lazy initialization can be deferred until it is needed.

Spring creates beans according to their dependency graph, then applies configured initialization callbacks. Destruction callbacks are associated with the bean definition for the bean’s shutdown lifecycle. These settings, along with scope and lazy mode, belong to the definition rather than to XML as a separate runtime mechanism. Spring dependency and collaborator reference

Watch for circular constructor dependencies

If bean A requires B through its constructor and B requires A through its constructor, neither can be constructed first. Spring cannot resolve that cycle and throws BeanCurrentlyInCreationException. Setter-based cycles may sometimes be configured, but Spring’s reference documentation recommends avoiding circular dependencies where possible.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

XML or Java configuration?

Spring supports XML, Java configuration and annotation-based configuration; the container is not tied to a single metadata format. In Java configuration, an @Bean method serves the same role as an XML <bean/> element. Spring Java configuration: @Bean and @Configuration

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Consideration XML Java or annotation configuration
Where configuration lives In external XML files, separate from application source code. In Java source alongside configuration classes and annotations.
Refactoring and type checking Bean references and class names are text in a separate file, so IDE refactoring and compile-time checks may be less direct. Java references and methods can participate more directly in IDE refactoring and compiler checks.
Verbosity Often requires more explicit markup for equivalent wiring. Can express wiring in Java methods and annotations, though the result depends on the configuration.
Deployment-time changes An external file can be convenient when wiring needs to be adjusted without editing Java source, subject to how the application packages and loads it. Configuration changes are part of source code and ordinarily follow the application’s build and deployment process.
Visibility of wiring Relationships are centralized in XML, which can make wiring easy to inspect separately from implementation. Configuration is visible in source code, but may be distributed across configuration classes and annotations.
Existing applications A practical fit when maintaining applications that already use XML. A practical fit for applications built around Java configuration or annotations.

These are engineering trade-offs, not measured performance or adoption differences. Spring’s documentation establishes the supported formats and the correspondence between @Bean and <bean/>; it does not establish a universal winner. Spring container overview

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.