Spring Hello World Using BeanFactory in VS Code
Learn how Spring's IoC container creates and manages a Java object using BeanFactory, XML configuration, Maven and VS Code.
This classic Spring Core example demonstrates how the Spring IoC container creates and manages a Java bean using BeanFactory.
Instead of creating the object directly using
new HelloWorld(), Spring creates and supplies
the object through its container.
Prerequisites
Before creating the Spring project, install the following tools:
- JDK 17+
- Visual Studio Code
- Extension Pack for Java
- Apache Maven
Verify Java Installation
Verify Maven Installation
Create the Maven Project
Open the integrated terminal in VS Code and execute the following Maven command.
mvn archetype:generate ^ -DgroupId=com.example ^ -DartifactId=spring-beanfactory-hello ^ -DarchetypeArtifactId=maven-archetype-quickstart ^ -DarchetypeVersion=1.5 ^ -DinteractiveMode=false
Move into the Project
cd spring-beanfactory-hello code .
Initial Project Structure
Add Spring Dependency
Open pom.xml and add the Spring Context dependency.
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.2.9</version>
</dependency>
Complete Minimal pom.xml
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://maven.apache.org/POM/4.0.0
https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>spring-beanfactory-hello</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>6.2.9</version>
</dependency>
</dependencies>
</project>
Create the Bean Class
Create the following file:
package com.example;
public class HelloWorld {
public void sayHello() {
System.out.println("Hello World from Spring!");
}
}
Create Spring Configuration
Create the following folder if it does not already exist:
Inside the folder, create:
<?xml version="1.0" encoding="UTF-8"?>
<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="helloWorld"
class="com.example.HelloWorld"/>
</beans>
The Important Part
<bean id="helloWorld"
class="com.example.HelloWorld"/>
com.example.HelloWorld and register it with the
bean name helloWorld.
Create the Main Program
Create:
package com.example;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
BeanFactory factory =
new ClassPathXmlApplicationContext(
"applicationContext.xml");
HelloWorld hello =
(HelloWorld) factory.getBean("helloWorld");
hello.sayHello();
}
}
Important Statement #1
BeanFactory factory =
new ClassPathXmlApplicationContext("applicationContext.xml");
Here, ClassPathXmlApplicationContext loads the XML
configuration from the application's classpath.
Important Statement #2
HelloWorld hello =
(HelloWorld) factory.getBean("helloWorld");
Spring searches its container for the bean named
helloWorld and returns the corresponding object.
Run the Program
Compile the Project
mvn clean compile
Run the Application
mvn exec:java -Dexec.mainClass="com.example.MainApp"
If Exec Plugin Is Not Configured
Add the following plugin inside the
<build> section of pom.xml.
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>exec-maven-plugin</artifactId>
<version>3.5.0</version>
</plugin>
</plugins>
</build>
Then execute:
mvn clean compile mvn exec:java -Dexec.mainClass="com.example.MainApp"
Expected Output
How It Works
The complete execution flow can be visualized as follows:
Traditional Java Approach
HelloWorld hello = new HelloWorld(); hello.sayHello();
Spring Approach
HelloWorld hello =
(HelloWorld) factory.getBean("helloWorld");
hello.sayHello();
Important Interview Point
BeanFactory is the basic IoC container interface in the Spring Framework.
It is responsible for creating and managing Spring beans and providing them to the application when requested.
BeanFactory vs ApplicationContext
| Feature | BeanFactory | ApplicationContext |
|---|---|---|
| Type | Basic IoC container | Advanced IoC container |
| Relationship | Base container interface | Extends BeanFactory |
| Features | Basic bean management | Additional enterprise features |
| Events | Limited | Supports event publication |
| Resources | Basic | Resource/message support |
Practical Takeaway
You write the HelloWorld class normally,
but you don't need to manually create its object.
Instead of:
HelloWorld hello = new HelloWorld();
Spring creates and supplies the object:
HelloWorld hello =
(HelloWorld) factory.getBean("helloWorld");
This is the basic idea behind Inversion of Control (IoC).
Quick Recap
BeanFactory is Spring's basic IoC container that creates, configures, and manages beans and provides them to the application through methods such as getBean().