Hello World Program in Spring Using Annotations
Learn how to build a simple Spring Core application using annotation-based configuration, Maven, VS Code and the ApplicationContext IoC container.
📘 Tutorial Overview
This practical demonstrates how Spring can discover, create and manage Java objects using annotations instead of manually defining every bean inside an XML configuration file.
JDK 17 or later
Spring Framework 6.x
Project and dependency management
Development environment
Spring IoC container
@Component, @Configuration, @ComponentScan
1 Project Structure
Create the following Maven project:
2 Create Maven Project in VS Code
Open VS Code → Terminal → New Terminal.
mvn archetype:generate -DgroupId=com.example -DartifactId=spring-annotation-hello -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=false
Move into the project:
cd spring-annotation-hello ``` code .```
3 Configure pom.xml
Open pom.xml and replace its contents with 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-annotation-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 downloads Spring Context and its required dependencies automatically.
4 Create HelloWorld.java
Create:
src/main/java/com/example/HelloWorld.java
package com.example;
import org.springframework.stereotype.Component;
@Component
public class HelloWorld {
public void sayHello() {
System.out.println(
"Hello World from Spring Annotation Configuration!"
);
}
}
```
What does @Component do?
@Component tells Spring that HelloWorld should be discovered and managed as a Spring Bean.
Instead of manually writing:
HelloWorld hello = new HelloWorld();
Spring creates and manages the object for us.
5 Create AppConfig.java
Create:
src/main/java/com/example/AppConfig.java
package com.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example")
public class AppConfig {
}
```
@Configuration
@ComponentScan
Spring will discover:
@Component ``` public class HelloWorld
6 Create MainApp.java
Create:
src/main/java/com/example/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();
}
}
```
7 Run the Program
First compile the application:
mvn clean compile
Then execute:
mvn exec:java -Dexec.mainClass="com.example.MainApp"
8 Understand the Execution Flow
The complete execution flow can be visualized as follows:
ApplicationContext
9 Understanding Each Annotation
Makes the HelloWorld class a Spring-managed bean.
@Component ``` public class HelloWorld
Marks AppConfig as a Spring configuration class.
@Configuration ``` public class AppConfig
Tells Spring where to search for components.
@ComponentScan("com.example")
10 XML Configuration vs Annotation Configuration
Annotation-based configuration reduces the amount of XML required in a Spring application.
| XML Configuration | Annotation Configuration |
|---|---|
| <bean> | @Component |
| XML configuration file | @Configuration class |
| <context:component-scan> | @ComponentScan |
| ClassPathXmlApplicationContext | AnnotationConfigApplicationContext |
11 Traditional Java vs Spring Annotation
Traditional Java
HelloWorld hello = new HelloWorld(); hello.sayHello();```
Here, the programmer directly creates the object using new.
Using Spring
ApplicationContext context =
new AnnotationConfigApplicationContext(AppConfig.class);
HelloWorld hello =
context.getBean(HelloWorld.class);
hello.sayHello();
```
Here, Spring creates and manages the object.
💡 IoC — Inversion of Control
This is the fundamental idea demonstrated by the example.
Instead of your application code controlling the creation of every object, the Spring IoC container takes responsibility for creating and managing configured beans.
12 Complete Project
HelloWorld.java
package com.example;
import org.springframework.stereotype.Component;
@Component
public class HelloWorld {
public void sayHello() {
System.out.println(
"Hello World from Spring Annotation Configuration!"
);
}
}
```
AppConfig.java
package com.example;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
@Configuration
@ComponentScan("com.example")
public class AppConfig {
}
```
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
🚀 Quick Recap
- Created a Maven-based Spring Core project.
- Added the Spring Context dependency.
- Created a Spring Bean using @Component.
- Created Java-based configuration using @Configuration.
- Used @ComponentScan to discover the bean.
- Created the IoC container using ApplicationContext.
- Retrieved the bean using getBean().
- Executed the application using Maven in VS Code.
🎯 Key Takeaway
The three most important annotations in this practical are:
Defines configuration
Finds components
Defines managed bean
In simple terms:
@Configuration → tells Spring about configuration
@ComponentScan → tells Spring where to search
@Component → tells Spring which class to manage
This approach is a modern alternative to manually declaring every application bean in an XML configuration file.
```🎤 Interview Preparation
1. What is @Component?
Answer: @Component is a Spring stereotype annotation that marks a class as a candidate for component scanning and allows Spring to register it as a bean.
2. What is @Configuration?
Answer: @Configuration indicates that a class contains Spring configuration information and can be used as a source for defining the application context.
3. What does @ComponentScan do?
Answer: @ComponentScan tells Spring which package or packages should be scanned for component classes such as @Component.
4. What is ApplicationContext?
Answer: ApplicationContext is a Spring IoC container interface responsible for managing beans and providing additional framework services.
5. Why use getBean()?
Answer: getBean() retrieves an object managed by the Spring IoC container. The application does not need to create that bean directly using the new operator.
🧪 Practical Verification Checklist
- JDK 17+ is installed.
- Maven is installed and working.
- VS Code with Java support is installed.
- Spring Context dependency is available.
- HelloWorld contains @Component.
- AppConfig contains @Configuration.
- AppConfig contains @ComponentScan("com.example").
- MainApp uses AnnotationConfigApplicationContext.
- context.getBean(HelloWorld.class) executes successfully.
- The expected Hello World message appears in the terminal.