Hello World Program in Spring Using Annotations with VS Code

SPRING CORE • VS CODE • JAVA 17+ ```

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.

Hello World from Spring Annotation Configuration!
```
```

📘 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.

☕ Java

JDK 17 or later

🌱 Spring

Spring Framework 6.x

🧩 Maven

Project and dependency management

💻 VS Code

Development environment

⚙️ ApplicationContext

Spring IoC container

🏷️ Annotations

@Component, @Configuration, @ComponentScan

```
```

1 Project Structure

Create the following Maven project:

spring-annotation-hello ``` │ ├── pom.xml │ └── src └── main └── java └── com └── example ├── HelloWorld.java ├── AppConfig.java └── MainApp.java
```
Tip: Keeping configuration, bean classes and application classes organized makes Spring applications easier to understand and maintain.
```
```

2 Create Maven Project in VS Code

Open VS Code → Terminal → New Terminal.

Maven Project Creation
mvn archetype:generate -DgroupId=com.example -DartifactId=spring-annotation-hello -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.5 -DinteractiveMode=false

Move into the project:

Terminal
cd spring-annotation-hello
```

code .
```
```
```

3 Configure pom.xml

Open pom.xml and replace its contents with 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-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>
```
What happens here?
Maven downloads Spring Context and its required dependencies automatically.
```
```

4 Create HelloWorld.java

Create:

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

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

@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

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

@Configuration tells Spring that AppConfig is a configuration class.

@ComponentScan

@ComponentScan("com.example") tells Spring to search the com.example package for Spring-managed components.

Spring will discover:

@Component
```

public class HelloWorld
```

6 Create 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();
    }
}
```
```
```

7 Run the Program

First compile the application:

Compile
mvn clean compile

Then execute:

Run Application
mvn exec:java -Dexec.mainClass="com.example.MainApp"
Hello World from Spring Annotation Configuration!
Expected result: The message is printed after Spring creates and supplies the HelloWorld bean.
```
```

8 Understand the Execution Flow

The complete execution flow can be visualized as follows:

MainApp.java
AnnotationConfig
ApplicationContext
AppConfig
@ComponentScan
com.example
@Component
HelloWorld Bean
getBean()
sayHello()
Simple explanation: AppConfig tells Spring where to search. ComponentScan finds HelloWorld. The @Component annotation identifies HelloWorld as a Spring-managed bean. ApplicationContext creates the bean and getBean() retrieves it.
```
```

9 Understanding Each Annotation

@Component

Makes the HelloWorld class a Spring-managed bean.

@Component
```

public class HelloWorld
```
@Configuration

Marks AppConfig as a Spring configuration class.

@Configuration
```

public class AppConfig
```
@ComponentScan

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
Conceptual mapping: Annotation configuration performs the same basic IoC and bean-management work while expressing configuration through Java annotations.
```
```

11 Traditional Java vs Spring Annotation

Traditional Java

Traditional Object Creation
```
HelloWorld hello = new HelloWorld();

hello.sayHello();
```

Here, the programmer directly creates the object using new.

Using Spring

Spring Object Creation
```
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

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

HelloWorld.java

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

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

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 Annotation Configuration!
```
```

🚀 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:

@Configuration
Defines configuration
@ComponentScan
Finds components
@Component
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.
```

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

Previous Post Next Post

Contact Form