Backend Development 11 min read

Understanding Spring IoC Container, Beans, and a Hello World Example

This article explains the concepts of Spring's IoC container and beans, guides through setting up a development environment, details the required JARs, demonstrates creating a simple Hello World application with interface and implementation, and shows how to configure and retrieve beans using XML configuration.

Qunar Tech Salon
Qunar Tech Salon
Qunar Tech Salon
Understanding Spring IoC Container, Beans, and a Hello World Example

IoC (Inversion of Control) container is a dependency‑injection container that instantiates, locates, configures, and assembles objects in a Spring application, eliminating the need for explicit new statements; BeanFactory is the core representation of the container.

The container discovers managed objects through configuration metadata, typically supplied via XML files but also possible with annotations or Java‑based configuration.

Objects managed by the Spring IoC container are called Beans; BeanDefinition represents the metadata that tells the container how to instantiate and assemble a Bean.

Environment setup : install JDK 1.5+, use Eclipse (or SpringSource Tool Suite) with the SpringSource Tool plugin, and add the required Spring repositories.

Required JARs (from spring-framework-3.0.5.RELEASE‑with‑docs.zip ): org.springframework.asm-3.0.5.RELEASE.jar , org.springframework.core-3.0.5.RELEASE.jar , org.springframework.beans-3.0.5.RELEASE.jar , org.springframework.context-3.0.5.RELEASE.jar , org.springframework.expression-3.0.5.RELEASE.jar . Dependency JARs include Log4j, Commons‑Logging, Commons‑Collections, etc.

Project creation : create a standard Java project in Eclipse, add the above JARs to lib , configure JUnit4, and set up src , resources , and lib directories.

Code example – Hello World :

package cn.javass.spring.chapter2.helloworld;
public interface HelloApi {
    void sayHello();
}

Implementation:

package cn.javass.spring.chapter2.helloworld;
public class HelloImpl implements HelloApi {
    @Override
    public void sayHello() {
        System.out.println("Hello World!");
    }
}

XML bean definition ( helloworld.xml ) placed under resources :

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!-- id is the bean name, class is the implementation -->
    <bean id="hello" class="cn.javass.spring.chapter2.helloworld.HelloImpl"/>
</beans>

JUnit test that loads the container and invokes the bean:

package cn.javass.spring.chapter2.helloworld;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class HelloTest {
    @Test
    public void testHelloWorld() {
        // 1. Instantiate IoC container from configuration
        ApplicationContext context = new ClassPathXmlApplicationContext("helloworld.xml");
        // 2. Retrieve bean by name and type (interface‑oriented programming)
        HelloApi helloApi = context.getBean("hello", HelloApi.class);
        // 3. Execute business logic
        helloApi.sayHello();
    }
}

The Spring IoC container can be instantiated via several implementations: XmlBeanFactory , ClassPathXmlApplicationContext , and FileSystemXmlApplicationContext , each differing in how they locate configuration files.

Bean retrieval methods include Object getBean(String name) , <T> T getBean(String name, Class<T> requiredType) , <T> T getBean(Class<T> requiredType) , and Map<String, T> getBeansOfType(Class<T> type) .

The article concludes that Spring’s IoC container enables low‑coupling, interface‑driven code, easy swapping of implementations via configuration, and straightforward testing without hard‑coded dependencies.

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.