Introduction
With the development of modern web applications, the importance of backend development is growing day by day.
The Java-based Spring framework is one of the widely used backend technologies among many developers.
In particular, Spring Boot is emerging as a tool that enables efficient development.
In this course, we will take an in-depth look at the basic concepts of the Spring framework and the characteristics and advantages of Spring Boot.
Overview of the Spring Framework
The Spring framework is an application framework for the Java platform that provides various features to conveniently assist in enterprise-level application development.
Spring is mainly composed of the following modules.
- Spring Core: Provides the basic features of Spring, including IoC (Inversion of Control) and DI (Dependency Injection) capabilities.
- Spring MVC: Supports the MVC architecture for web application development.
- Spring Data: Supports integration with various databases.
- Spring Security: Provides robust authentication and authorization features for application security.
Differences Between Spring and Spring Boot
The Spring framework has traditionally provided flexibility in application composition and management.
However, this has resulted in the need for complex configurations and initialization processes.
On the other hand, Spring Boot is a tool developed to solve these issues.
Here are the main differences between the two frameworks.
- Configuration Method: Spring Boot follows the principle of ‘convention over configuration,’ allowing applications to start with minimal configuration.
- Embedded Server: Spring Boot supports embedded web servers such as Tomcat and Jetty, allowing applications to run without separately configuring a server.
- Starter Dependencies: Spring Boot provides a module called ‘starter’ to easily manage various dependencies, enabling developers to easily add required features.
- Actuator: Spring Boot includes an actuator module that provides various features for monitoring and managing the application’s status.
Installing and Setting Up Spring Boot
To start backend development using Spring Boot, you first need to set up your development environment.
Let’s follow the steps below to install Spring Boot and create a simple project.
1. Preparing the Development Environment
The tools required to use Spring Boot are as follows.
- Java Development Kit (JDK): Java 8 or higher is required.
- IDE: Choose an integrated development environment (IDE) such as IntelliJ IDEA or Eclipse that supports Java development.
- Maven/Gradle: Choose Maven or Gradle for dependency management.
2. Creating a Spring Boot Project
Spring Boot projects can be created in various ways, but the simplest way is to use Spring Initializr.
By visiting the website and entering the required settings, you can automatically generate the initial project structure.
- Spring Initializr Website
- Enter Project Meta Information: Set Group, Artifact, Name, Description, Package name, etc.
- Add Required Dependencies: Choose and add Spring Web, Spring Data JPA, H2 Database, etc.
- Download the generated project and open it in your IDE.
Structure of a Spring Boot Application
The created Spring Boot project has the following structure.
└── src └── main ├── java │ └── com │ └── example │ └── demo │ ├── DemoApplication.java │ └── controller │ └── HelloController.java └── resources ├── application.properties └── static
Creating Your First Web Application
Let’s create a simple RESTful web service.
First, we will create a controller to handle HTTP requests.
1. Creating the HelloController Class
HelloController
class is the most basic class for handling web requests and can be written as follows.
package com.example.demo.controller; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class HelloController { @GetMapping("/hello") public String hello() { return "Hello, Spring Boot!"; } }
2. Running the Application
Running the DemoApplication
class in the IDE will start the embedded server,
and when you access http://localhost:8080/hello,
you can see the message “Hello, Spring Boot!”.
Database Integration
Spring Boot supports integration with various databases.
In this section, we will build a simple CRUD application using H2 Database.
1. Adding Dependencies
Add the H2 database and JPA-related dependencies to the pom.xml
file.
org.springframework.boot spring-boot-starter-data-jpa com.h2database h2 runtime
2. Database Configuration
Add simple configuration in the application.properties
file.
spring.h2.console.enabled=true spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driverClassName=org.h2.Driver spring.datasource.username=sa spring.datasource.password=
3. Creating the Entity Class
Let’s create an entity class to store data in the database.
We will create a User
class to store user information.
package com.example.demo.entity; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; @Entity public class User { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; private String name; private String email; // Getters and Setters }
4. Creating the Repository Interface
Create a repository interface to interact with the database.
package com.example.demo.repository; import com.example.demo.entity.User; import org.springframework.data.jpa.repository.JpaRepository; public interface UserRepository extends JpaRepository{ }
5. Updating the Controller
To add API endpoints that handle CRUD operations,
create a UserController
and add request mappings.
package com.example.demo.controller; import com.example.demo.entity.User; import com.example.demo.repository.UserRepository; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.*; import java.util.List; @RestController @RequestMapping("/users") public class UserController { @Autowired private UserRepository userRepository; @GetMapping public ListgetAllUsers() { return userRepository.findAll(); } @PostMapping public User createUser(@RequestBody User user) { return userRepository.save(user); } }
6. Running and Testing the Application
Restart the application, and use tools like Postman to test the
GET /users
and POST /users
endpoints.
Security Configuration with Spring Security
Security is very important for backend applications.
Let’s add access control and authentication through Spring Security.
1. Adding Dependencies
Add Spring Security dependency to the pom.xml
file.
org.springframework.boot spring-boot-starter-security
2. Creating the Security Configuration Class
Create a configuration class to use Spring Security.
package com.example.demo.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter; @Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().authenticated() .and() .httpBasic(); } }
3. Testing and Verification
Restart the application, and you can connect to API tests using HTTP Basic authentication.
Conclusion
In this course, we covered the basics of the Spring framework and the development process of
backend applications using Spring Boot.
As Spring Boot’s accessibility increases, more developers can
easily develop backend applications.
We encourage you to integrate various tools or add additional features
to create more robust applications in the future.