Now that we’ve set up our environment, it’s time to create a simple Spring application. In this chapter, we will create a basic Hello World application using Spring Framework to help you understand how to structure a Spring project, define beans, and leverage the Inversion of Control (IoC) container.
By the end of this chapter, you’ll know how to:
Let’s get started!
If you are using Spring Initializr to generate the project, follow these steps:
After the download, unzip the project and open it in your favorite IDE.
If you are setting up manually, follow the steps from the previous
chapter to create a Maven or Gradle project and include dependencies
like spring-context
in the pom.xml
or
build.gradle
file.
First, let’s create a simple bean that will hold the logic for displaying the Hello World message.
com.example.springapplication
in your
project.HelloWorldService.java
.package com.example.springapplication;
public class HelloWorldService {
public void sayHello() {
System.out.println("Hello, World!");
}
}
In this class, we have a simple method sayHello()
that
prints the string “Hello, World!” to the console.
Now, we need to tell Spring how to manage the
HelloWorldService
class by configuring it as a Spring
bean.
AppConfig.java
in
the same package com.example.springapplication
:package com.example.springapplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class AppConfig {
@Bean
public HelloWorldService helloWorldService() {
return new HelloWorldService();
}
}
In this configuration class, we use the @Configuration
annotation to indicate that this class contains Spring configuration.
The @Bean
annotation tells Spring to create a bean of type
HelloWorldService
that can be injected into other
components.
Next, let’s create a MainApp.java
file to run the Spring
application and invoke the HelloWorldService
.
com.example.springapplication
,
create the MainApp.java
class:package com.example.springapplication;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class MainApp {
public static void main(String[] args) {
// Initialize Spring IoC container
= new AnnotationConfigApplicationContext(AppConfig.class);
AnnotationConfigApplicationContext context
// Retrieve the bean from the container
= context.getBean(HelloWorldService.class);
HelloWorldService helloWorldService
// Call the method on the bean
.sayHello();
helloWorldService
// Close the context
.close();
context}
}
Here’s a breakdown of what the code does:
AnnotationConfigApplicationContext
. This container will
scan the AppConfig.class
configuration and initialize the
beans defined there.HelloWorldService
bean from the
container using
context.getBean(HelloWorldService.class)
.sayHello()
method on the
HelloWorldService
bean, which prints “Hello, World!” to the
console.context.close()
to release resources.To run the application, simply execute the MainApp.java
class. If you’re using Maven, you can also run the application using the
following command:
mvn clean install
mvn exec:java
When you run the program, you should see the output:
Hello, World!
This confirms that the Spring IoC container is managing the
HelloWorldService
bean and has successfully injected it
into the application.
AppConfig
class, we configured the HelloWorldService
as a Spring bean
using the @Bean
annotation.AppConfig
).AnnotationConfigApplicationContext
is used to load the
configuration class and manage the Spring beans.In this chapter, we created a simple Hello World application using Spring Framework. We:
HelloWorldService
).AppConfig
).MainApp
).This is just the beginning. In the next chapters, we will explore more advanced Spring concepts, such as IoC Containers, Bean Scopes, and Dependency Injection.