Lesson 2 - Installation and Development Environment Setup
29/06/2024 - 4 phút
1. Java Development Kit (JDK) Installation Guide
Introduction: The JDK is a software development kit used to develop Java applications. It includes a compiler (javac), a Java Virtual Machine (JVM), and several other development tools.
JDK Installation:
- Windows:
- Download the JDK from the official Oracle website or OpenJDK.
- Run the installer and follow the instructions.
- Configure the JAVA_HOME environment variable:
- Search for “Environment Variables” in Windows and open it.
- Add a new system variable named
JAVA_HOME
with the value being the path to the JDK installation directory. - Add
%JAVA_HOME%\bin
to thePATH
environment variable.
- macOS:
- Download the JDK from the official Oracle website or use Homebrew to install OpenJDK.
- Run the command:
brew install openjdk
. - Configure the JAVA_HOME environment variable:
- Open the
.bash_profile
,.zshrc
, or.bashrc
file and add the line:export JAVA_HOME=$(/usr/libexec/java_home)
.
- Open the
- Save and reload the configuration file by running:
source ~/.zshrc
orsource ~/.bash_profile
.
- Linux:
- Use the package manager of your operating system to install OpenJDK:
- Ubuntu:
sudo apt update && sudo apt install openjdk-11-jdk
- CentOS:
sudo yum install java-11-openjdk-devel
- Ubuntu:
- Verify the installation by running
java -version
.
- Use the package manager of your operating system to install OpenJDK:
- Windows:
2. Installing an Integrated Development Environment (IDE)
Introduction: An IDE is a tool that helps developers write, test, and debug code efficiently. Popular IDEs for Java include VSCode, IntelliJ IDEA, and Eclipse.
Installing VSCode:
- Download VSCode from the official Microsoft website.
- Install VSCode by following the instructions.
- Open VSCode and install the necessary extensions:
- Open the Extensions Marketplace by pressing
Ctrl+Shift+X
. - Search for and install the “Java Extension Pack”.
- Search for and install the “Spring Boot Extension Pack”.
- Open the Extensions Marketplace by pressing
3. Installing Maven/Gradle
Introduction: Maven and Gradle are project management and build automation tools that help manage dependencies and the build process for Java applications.
Installing Maven:
- Download Maven from the official Apache Maven website.
- Extract the downloaded file.
- Configure the M2_HOME environment variable:
- Search for “Environment Variables” in Windows and open it.
- Add a new system variable named
M2_HOME
with the value being the path to the extracted Maven directory. - Add
%M2_HOME%\bin
to thePATH
environment variable.
Installing Gradle:
- Download Gradle from the official Gradle website.
- Extract the downloaded file.
- Configure the GRADLE_HOME environment variable:
- Search for “Environment Variables” in Windows and open it.
- Add a new system variable named
GRADLE_HOME
with the value being the path to the extracted Gradle directory. - Add
%GRADLE_HOME%\bin
to thePATH
environment variable.
4. Using Spring Initializr to Create a New Project
Introduction: Spring Initializr is a web-based tool that quickly generates a Spring Boot project with the necessary configurations and dependencies.
Steps to Create a New Project:
- Visit Spring Initializr.
- Choose the project parameters:
- Project Metadata:
- Group:
com.example
- Artifact:
demo
- Name:
Demo
- Description:
Demo project for Spring Boot
- Package name:
com.example.demo
- Packaging:
Jar
- Java Version:
11
- Group:
- Dependencies: Add necessary dependencies such as:
- Spring Web
- Spring Data JPA
- H2 Database
- Project Metadata:
- Click “Generate” to download the generated project.
- Extract the downloaded file into the working directory.
5. Exploring the Spring Boot Project Structure
Introduction to the Project Structure:
- src/main/java: Contains the Java source code of the application.
- src/main/resources: Contains the configuration files and resources of the application.
- src/test/java: Contains the test cases for the application.
- pom.xml or build.gradle: The Maven or Gradle configuration file, containing information about dependencies and required plugins for the project.
Key Components:
- Application class: The main class to launch the Spring Boot application (annotated with
@SpringBootApplication
). - Controller class: Classes handling HTTP requests.
- Repository class: Classes interacting with the database.
- Application class: The main class to launch the Spring Boot application (annotated with
Example
- Application class:
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
Explanation:
@SpringBootApplication
is a special annotation that combines three annotations:@EnableAutoConfiguration
,@ComponentScan
, and@Configuration
. This annotation helps Spring Boot automatically configure the application based on the dependencies present in the classpath.SpringApplication.run(DemoApplication.class, args)
launches the Spring Boot application by creating anApplicationContext
and starting all the declared beans.
Controller class:
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, World!";
}
}
Explanation:
@RestController
is a special annotation that combines@Controller
and@ResponseBody
, allowing you to handle HTTP requests and return data in JSON or XML format.@GetMapping("/hello")
maps the GET request to thehello()
method, which returns the string “Hello, World!”.
Repository class:
package com.example.demo.repository;
import com.example.demo.model.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UserRepository extends JpaRepository<User, Long> {
}
Explanation:
@Repository
is an annotation that marks the class as a data access object (DAO).JpaRepository<User, Long>
provides standard CRUD methods for theUser
entity.
Entity class:
package com.example.demo.model;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String name;
// getters and setters
}
- Explanation:
@Entity
marks the class as a JPA entity.@Id
and@GeneratedValue(strategy = GenerationType.IDENTITY)
annotate the primary key field and specify that its value should be generated automatically.
With these steps, learners will be able to install and set up the development environment for Spring Boot, create and explore the structure of a basic Spring Boot project. The specific examples will help learners understand how to write and organize code in a Spring Boot project.