Hello World Program in Spring Using Java-Based Configuration with VS Code

SPRING CORE • JAVA CONFIGURATION • VS CODE

Hello World Program in Spring Using Java-Based Configuration with VS Code

Learn how to configure Spring Beans using Java classes instead of XML, using @Configuration, @Bean and ApplicationContext.

🎯 Learning Objectives

After completing this practical, you will be able to:

  • Understand Java-based Spring configuration.
  • Create a Spring Maven project in VS Code.
  • Configure Spring using @Configuration.
  • Create Beans using @Bean.
  • Create an IoC container using ApplicationContext.
  • Retrieve a Spring Bean using getBean().
  • Understand the difference between XML, annotation and Java-based configuration.
  • Understand the basic concept of Inversion of Control.

1Technology Stack

This practical uses:

Java 17+ Spring Framework 6 Maven VS Code ApplicationContext @Configuration @Bean
Expected Output:
Hello World from Spring Java-Based Configuration!

2What Is Java-Based Configuration?

Spring supports multiple ways to configure Beans. One traditional approach is XML configuration.

XML Configuration
<bean id="helloWorld"
      class="com.example.HelloWorld"/>

In Java-based configuration, the same Bean can be configured using a Java method annotated with @Bean.

Java-Based Configuration
@Bean
public HelloWorld helloWorld() {
    return new HelloWorld();
}
Simple idea: Java itself becomes the Spring configuration instead of using a separate XML configuration file.

3Project Structure

Create the following Maven project:

spring-java-config-hello │ ├── pom.xml │ └── src └── main └── java └── com └── example ├── HelloWorld.java ├── AppConfig.java └── MainApp.java
Notice: There is no applicationContext.xml file. Spring configuration is written using Java.

4Create Maven Project in VS Code

Open:

VS Code → Terminal → New Terminal

Run:

mvn archetype:generate -DgroupId=com.example -DartifactId=spring-java-config-hello -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=false

Move into the project:

cd spring-java-config-hello

Open the project in VS Code:

code .

5Configure pom.xml

Open pom.xml and use the following configuration:

pom.xml
<?xml version="1.0" encoding="UTF-8"?>

<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-java-config-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>

    <build>
        <plugins>

            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>exec-maven-plugin</artifactId>
                <version>3.5.0</version>
            </plugin>

        </plugins>
    </build>

</project>
Maven will download Spring Framework and its required dependencies automatically.

6Create HelloWorld.java

Create:

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 Java-Based Configuration!"
        );

    }
}

Important Observation

This is an ordinary Java class. There are no Spring annotations in this class.

The responsibility for registering this class as a Spring Bean will be handled by AppConfig.java.

7Create AppConfig.java

Create:

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

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public HelloWorld helloWorld() {

        return new HelloWorld();

    }
}

Understanding @Configuration

@Configuration
public class AppConfig

@Configuration tells Spring:

"This Java class contains Spring configuration information."

It replaces the need for a traditional XML configuration file for this example.

Understanding @Bean

@Bean
public HelloWorld helloWorld() {

    return new HelloWorld();

}

@Bean tells Spring to call the method and manage the object returned by that method as a Spring Bean.

The object created by return new HelloWorld(); becomes a Spring-managed Bean.

What Is the Bean Name?

By default, Spring uses the method name as the Bean name.

Default Bean Name
helloWorld

Therefore, the method:

public HelloWorld helloWorld()

creates a Bean whose default name is helloWorld.

8Create MainApp.java

Create:

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

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {

    public static void main(String[] args) {

        // Create Spring IoC Container
        ApplicationContext context =
                new AnnotationConfigApplicationContext(
                        AppConfig.class);

        // Get Spring Bean
        HelloWorld hello =
                context.getBean(HelloWorld.class);

        // Call method
        hello.sayHello();
    }
}

9Run the Application

Step 1 — Compile

mvn clean compile

Successful compilation should display:

BUILD SUCCESS

Step 2 — Execute

mvn exec:java -Dexec.mainClass="com.example.MainApp"

Expected Output

Hello World from Spring Java-Based Configuration!

10Understand the Complete Flow

MainApp.java AnnotationConfigApplicationContext AppConfig @Configuration @Bean new HelloWorld() Spring Bean Created context.getBean() HelloWorld Object sayHello() Hello World from Spring Java-Based Configuration!

11Annotation vs Java-Based Configuration

Annotation-based and Java-based configuration can look similar, but their purpose is different.

Annotation-Based

@Component
public class HelloWorld {
}

Spring discovers the class using component scanning.

Java-Based

@Bean
public HelloWorld helloWorld() {
    return new HelloWorld();
}

We explicitly tell Spring how to create the Bean.

This Example

@Configuration
@Bean

The Bean is configured explicitly in AppConfig.

Important: This example does not use @Component or @ComponentScan. The Bean is explicitly declared using @Bean.

12XML vs Annotation vs Java-Based Configuration

Configuration Bean Definition Configuration Mechanism
XML <bean> XML configuration file
Annotation @Component @Configuration + @ComponentScan
Java-Based @Bean @Configuration

XML

<bean id="helloWorld"
      class="com.example.HelloWorld"/>

Annotation

@Component
public class HelloWorld {

}

Java-Based

@Bean
public HelloWorld helloWorld() {

    return new HelloWorld();

}

13Why Use Java-Based Configuration?

  • No separate XML configuration file is required.
  • Configuration is written using normal Java syntax.
  • Java configuration is easier to refactor using an IDE.
  • Bean creation logic can be written directly in Java.
  • Compile-time checking can help catch configuration errors.
  • Java configuration can contain programmatic logic when required.
  • It works naturally with modern Spring applications.

14Complete Project Structure

spring-java-config-hello │ ├── pom.xml │ └── src └── main └── java └── com └── example ├── HelloWorld.java ├── AppConfig.java └── MainApp.java

15Complete Source Code

HelloWorld.java
package com.example;

public class HelloWorld {

    public void sayHello() {

        System.out.println(
            "Hello World from Spring Java-Based Configuration!"
        );

    }
}
AppConfig.java
package com.example;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {

    @Bean
    public HelloWorld helloWorld() {

        return new HelloWorld();

    }
}
MainApp.java
package com.example;

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MainApp {

    public static void main(String[] args) {

        ApplicationContext context =
                new AnnotationConfigApplicationContext(
                        AppConfig.class);

        HelloWorld hello =
                context.getBean(HelloWorld.class);

        hello.sayHello();
    }
}
Output
Hello World from Spring Java-Based Configuration!

🎤 Interview Questions

1. What is Java-based configuration in Spring?

Java-based configuration is a Spring configuration approach in which Java classes and annotations such as @Configuration and @Bean are used instead of XML configuration.

2. What does @Configuration do?

It identifies a Java class as a source of Spring Bean configuration.

3. What does @Bean do?

It tells Spring that the object returned by the annotated method should be registered and managed as a Spring Bean.

4. What is the default Bean name in this example?

The default Bean name is helloWorld, because it is derived from the method name.

5. Does HelloWorld require a Spring annotation?

No. In this example, HelloWorld is an ordinary Java class. The Bean is registered through the @Bean method in AppConfig.

6. Which ApplicationContext is used?

AnnotationConfigApplicationContext is used to load the Java-based configuration class.

📝 Quick Revision

  • @Configuration identifies the Spring configuration class.
  • @Bean registers the returned object as a Spring Bean.
  • ApplicationContext represents the Spring IoC container.
  • AnnotationConfigApplicationContext loads Java configuration.
  • getBean() retrieves the Spring-managed object.
  • The HelloWorld class does not need Spring annotations.
  • Java-based configuration eliminates the need for XML configuration.
  • This practical demonstrates Inversion of Control (IoC).

🚀 Final Takeaway

The heart of this example is the following configuration:

@Configuration
public class AppConfig {

    @Bean
    public HelloWorld helloWorld() {

        return new HelloWorld();

    }
}

Think of it in three simple steps:

@Configuration "This is my Spring configuration." @Bean "Spring, create and manage this object." ApplicationContext "Give me the object managed by Spring."

Java-based configuration is therefore a clean and powerful way to define Spring Beans without maintaining a separate XML configuration file.

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

Previous Post Next Post

Contact Form