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.
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.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
