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:
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.
<bean id="helloWorld"
class="com.example.HelloWorld"/>
In Java-based configuration, the same Bean can be configured using a Java method annotated with @Bean.
@Bean
public HelloWorld helloWorld() {
return new HelloWorld();
}
3Project Structure
Create the following Maven project:
applicationContext.xml file.
Spring configuration is written using Java.
4Create Maven Project in VS Code
Open:
VS Code → Terminal → New Terminal
Run:
Move into the project:
Open the project in VS Code:
5Configure pom.xml
Open pom.xml and use the following configuration:
<?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>
6Create HelloWorld.java
Create:
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.
7Create AppConfig.java
Create:
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:
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.
return new HelloWorld();
becomes a Spring-managed Bean.
What Is the Bean Name?
By default, Spring uses the method name as the Bean name.
helloWorld
Therefore, the method:
public HelloWorld helloWorld()
creates a Bean whose default name is helloWorld.
8Create MainApp.java
Create:
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
Successful compilation should display:
Step 2 — Execute
Expected Output
10Understand the Complete Flow
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.
@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
15Complete Source Code
package com.example;
public class HelloWorld {
public void sayHello() {
System.out.println(
"Hello World from Spring Java-Based Configuration!"
);
}
}
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();
}
}
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();
}
}
🎤 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:
Java-based configuration is therefore a clean and powerful way to define Spring Beans without maintaining a separate XML configuration file.