Spring Hello World Using BeanFactory in VS Code

SPRING CORE • PRACTICAL TUTORIAL

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.

What will you learn?

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.

🎯 Practical Goal
Build a simple Spring application that displays: Hello World from Spring!
STEP 1

Prerequisites

Before creating the Spring project, install the following tools:

  • JDK 17+
  • Visual Studio Code
  • Extension Pack for Java
  • Apache Maven

Verify Java Installation

java -version

Verify Maven Installation

mvn -version
💡 Tip
Make sure both Java and Maven commands work successfully before creating the Spring project.
STEP 2

Create the Maven Project

Open the integrated terminal in VS Code and execute the following Maven command.

VS Code Terminal
mvn archetype:generate ^
-DgroupId=com.example ^
-DartifactId=spring-beanfactory-hello ^
-DarchetypeArtifactId=maven-archetype-quickstart ^
-DarchetypeVersion=1.5 ^
-DinteractiveMode=false

Move into the Project

VS Code Terminal
cd spring-beanfactory-hello
code .

Initial Project Structure

spring-beanfactory-hello/ │ ├── pom.xml │ └── src/ └── main/ └── java/ └── com/ └── example/ ├── HelloWorld.java └── MainApp.java
STEP 3

Add Spring Dependency

Open pom.xml and add the Spring Context dependency.

pom.xml
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>6.2.9</version>
</dependency>

Complete Minimal pom.xml

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>
STEP 4

Create the Bean Class

Create the following file:

src/main/java/com/example/HelloWorld.java
HelloWorld.java
package com.example;

public class HelloWorld {

    public void sayHello() {

        System.out.println("Hello World from Spring!");

    }

}
🔎 What is this class?
HelloWorld is a normal Java class. Its object will be created and managed by Spring.
STEP 5

Create Spring Configuration

Create the following folder if it does not already exist:

src/main/resources/

Inside the folder, create:

applicationContext.xml
applicationContext.xml
<?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

Spring Bean Definition
<bean id="helloWorld"
      class="com.example.HelloWorld"/>
🧠 What does this tell Spring?
Create and manage an object of com.example.HelloWorld and register it with the bean name helloWorld.
STEP 6

Create the Main Program

Create:

src/main/java/com/example/MainApp.java
MainApp.java
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

IoC Container
BeanFactory factory =
    new ClassPathXmlApplicationContext("applicationContext.xml");

Here, ClassPathXmlApplicationContext loads the XML configuration from the application's classpath.

Important Statement #2

Get Bean
HelloWorld hello =
    (HelloWorld) factory.getBean("helloWorld");

Spring searches its container for the bean named helloWorld and returns the corresponding object.

STEP 7

Run the Program

Compile the Project

VS Code Terminal
mvn clean compile

Run the Application

VS Code Terminal
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.

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:

VS Code Terminal
mvn clean compile
mvn exec:java -Dexec.mainClass="com.example.MainApp"

Expected Output

Hello World from Spring!
STEP 8

How It Works

The complete execution flow can be visualized as follows:

applicationContext.xml
Spring IoC Container
BeanFactory
Creates HelloWorld Object
factory.getBean("helloWorld")
HelloWorld Object
sayHello()
Hello World from Spring!

Traditional Java Approach

Without Spring
HelloWorld hello = new HelloWorld();

hello.sayHello();

Spring Approach

Using IoC
HelloWorld hello =
    (HelloWorld) factory.getBean("helloWorld");

hello.sayHello();
🚀 The Big Idea: Inversion of Control
In traditional Java, your program creates the object. In Spring, the Spring IoC container creates and manages the object for you.
INTERVIEW PREPARATION

Important Interview Point

💼 What is BeanFactory?

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
⭐ Remember This
ApplicationContext extends BeanFactory and provides additional Spring infrastructure and enterprise-oriented capabilities.

Practical Takeaway

You write the HelloWorld class normally, but you don't need to manually create its object.

Instead of:

Traditional Java
HelloWorld hello = new HelloWorld();

Spring creates and supplies the object:

Spring IoC
HelloWorld hello =
    (HelloWorld) factory.getBean("helloWorld");

This is the basic idea behind Inversion of Control (IoC).

Quick Recap

1. Maven Creates and manages the Java project and dependencies.
2. Bean HelloWorld is registered as a Spring-managed bean.
3. XML applicationContext.xml defines the bean.
4. BeanFactory Provides access to the Spring IoC container.
5. getBean() Retrieves the object managed by Spring.
6. IoC Spring takes responsibility for object creation and management.
🎓 One-Line Interview Answer

BeanFactory is Spring's basic IoC container that creates, configures, and manages beans and provides them to the application through methods such as getBean().

Thanks a lot for query or your valuable suggestions related to the topic.

Previous Post Next Post

Contact Form