Backend Development 12 min read

Spring IoC Bean Configuration, Naming, and Instantiation Guide

This article explains how to structure Spring XML configuration files, define bean metadata, use id/name/alias for bean naming, and instantiate beans via constructors, static factory methods, or instance factories, providing detailed code examples and JUnit tests for each approach.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Spring IoC Bean Configuration, Naming, and Instantiation Guide

2.3.1 XML configuration structure – A typical Spring beans file contains <beans> , optional <import> elements, multiple <bean> definitions, and <alias> entries, as illustrated by the sample XML block.

2.3.2 Bean configuration – Each <bean> defines a BeanDefinition that may include the fully‑qualified class name, scope, lazy‑init flag, lifecycle callbacks, constructor‑vs‑factory‑method creation mode, and references to other beans for dependency injection.

2.3.3 Bean naming – Beans can be identified by an id or a name ; the first identifier is the primary name, subsequent names are aliases. Using id enables XML‑level validation of references, while name follows normal XML naming rules. Examples show four naming strategies: no id, explicit id, explicit name, and both id and name with aliases.

2.3.4 Bean instantiation – Spring can create beans in three ways:

Constructor instantiation (default or with arguments). Example class HelloImpl2 and XML: <bean name="bean1" class="cn.javass.spring.chapter2.HelloImpl2"/> <bean name="bean2" class="cn.javass.spring.chapter2.HelloImpl2"> <constructor-arg index="0" value="Hello Spring!"/> </bean>

Static factory method. Example factory class HelloApiStaticFactory and XML: <bean id="bean3" class="cn.javass.spring.chapter2.HelloApiStaticFactory" factory-method="newInstance"> <constructor-arg index="0" value="Hello Spring!"/> </bean>

Instance factory method. Example factory bean HelloApiInstanceFactory and XML: <bean id="beanInstanceFactory" class="cn.javass.spring.chapter2.HelloApiInstanceFactory"/> <bean id="bean4" factory-bean="beanInstanceFactory" factory-method="newInstance"> <constructor-arg index="0" value="Hello Spring!"/> </bean>

JUnit test snippets demonstrate retrieving each bean by name or id and invoking sayHello() to verify successful instantiation.

2.3.5 Summary – The article covers the fundamentals of Spring IoC: container concepts, XML configuration, bean naming rules, and three bean creation strategies, preparing readers for the next topic of dependency injection.

JavaIoCSpringdependency injectionBeanXML Configuration
Qunar Tech Salon
Written by

Qunar Tech Salon

Qunar Tech Salon is a learning and exchange platform for Qunar engineers and industry peers. We share cutting-edge technology trends and topics, providing a free platform for mid-to-senior technical professionals to exchange and learn.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.