Featured Article

Java Spring Code Example Tutorial for Beginners

Kenneth Jul 13, 2026

Java Spring, a popular open-source framework, simplifies enterprise-level application development on the Java platform. Let's dive into a code example to understand its basic concepts and syntax.

Spring Framework Cheat Sheet for Beginners | Spring Boot Quick Guide for Java Developers
Spring Framework Cheat Sheet for Beginners | Spring Boot Quick Guide for Java Developers

Spring provides a comprehensive infrastructure supporting rapid Java application development and enterprise integration. It follows the Inversion of Control (IoC) principle, managing the complexity of object lifecycles.

the web page for an app showing different types of programming
the web page for an app showing different types of programming

Setting Up a Simple Spring Boot Project

To start with, let's set up a simple Spring Boot project using the Spring Initializr tool (start.spring.io). Choose "Web" as a dependency, and download the project as a ZIP file.

Java Cheatsheet
Java Cheatsheet

Unzip the file and import it into your preferred Integrated Development Environment (IDE) such as IntelliJ IDEA or Eclipse.

Hello World with Spring Boot

Spring vs Spring Boot – Key Differences Every Java Developer Should Know
Spring vs Spring Boot – Key Differences Every Java Developer Should Know

In the generated project, navigate to the com.example.demo package and create a new class named HelloWorldController. This will be our Spring Boot controller, handling HTTP requests.

Implement the HelloWorldController class with the following code:

```java import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { @GetMapping("/hello") public String helloWorld() { return "Hello, Spring Boot!"; } } ```

The @RestController annotation indicates that this class handles HTTP requests, and @GetMapping("/hello") maps the HTTP GET request to the helloWorld() method. When accessed, this method returns "Hello, Spring Boot!".

an image of a computer screen with text and numbers on it, as well as other items
an image of a computer screen with text and numbers on it, as well as other items

Running the Application

To run the application, right-click on the main class (DemoApplication) in your IDE and select "Run 'DemoApplication' main class". The Spring Boot server will start on http://localhost:8080.

Access http://localhost:8080/hello in your web browser, and you should see "Hello, Spring Boot!" displayed in the response.

10 Examples of WebClient In Spring Framework and Java
10 Examples of WebClient In Spring Framework and Java

Dependency Injection with Spring

Spring uses IoC to manage dependencies between objects. It does this by injecting the required dependencies into objects when they're created.

☕ Build a Simple Web App with Spring Boot in Java
☕ Build a Simple Web App with Spring Boot in Java
Spring MVC and Angular 6 Example - Java Code Geeks
Spring MVC and Angular 6 Example - Java Code Geeks
Spring MVC Interceptor with Example - Dinesh on Java
Spring MVC Interceptor with Example - Dinesh on Java
concept of java #java  #javanotes
concept of java #java #javanotes
10 Spring MVC and REST Annotations Every Java Developer Should Learn
10 Spring MVC and REST Annotations Every Java Developer Should Learn
Spring with Maven Tutorial - Java Code Geeks
Spring with Maven Tutorial - Java Code Geeks
Spring boot
Spring boot
Java Streams Cheat Sheet. Creation, Intermediate & Terminal Operations (Java 17+)
Java Streams Cheat Sheet. Creation, Intermediate & Terminal Operations (Java 17+)
#codeavail #infographic #infographics #javainfographic #graphics #graphics #knowledge #programming #
#codeavail #infographic #infographics #javainfographic #graphics #graphics #knowledge #programming #

Let's create a service class and demonstrate dependency injection.

Creating a Service Class

In the com.example.demo package, create a new class named HelloWorldService. Implement the class with the following code:

```java public class HelloWorldService { public String getGreeting() { return "Hello, World!"; } } ```

The HelloWorldService class has a single method, getGreeting(), which returns a greeting message.

Injecting the Service into the Controller

Modify the HelloWorldController class to inject the HelloWorldService and use its method:

```java import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloWorldController { private HelloWorldService service; @Autowired public HelloWorldController(HelloWorldService service) { this.service = service; } @GetMapping("/hello-injected") public String helloWorldInjected() { return service.getGreeting(); } } ```

The @Autowired annotation tells Spring to inject an instance of HelloWorldService into the controller. The helloWorldInjected() method now uses the injected service to return the greeting message.

Start the application again, and access http://localhost:8080/hello-injected. You should see the message "Hello, World!" displayed in the response. This demonstrates how Spring injects dependencies into objects.

As the application evolves, dependency injection helps manage complexity and promotes high Cohesion and low Coupling, two fundamental principles of software design.

Spring Boot makes it easy to create production-grade applications quickly. By understanding the basic principles of IoC and dependency injection, you're well on your way to becoming a proficient Spring developer. Happy coding!