Code ví dụ Spring Boot Hello World với Intellij IDEA
(Xem lại: Code ví dụ Spring Boot Hello World với Eclipse)
1. Tạo Project Spring Boot Hello World
Trên thanh menu bar chọn File > New > Module
Hoặc click vào biểu tượng module trên màn hình:
Ở phần bên trái chọn loại module là Spring Initializr. Ở phần bên phải chọn bản jdk mà bạn cài trên máy.
Nhập các thông tin của module
Ở đây mình làm ví dụ hiển thị trang html với nội dung hello world nên sẽ dùng 2 thư viện là Spring Web và Thymeleaf để hiển thị
Chọn nơi lưu trữ module
Kết quả:
File pom.xml chứa các thư viện mà bạn đã thêm ở bên trên:
<?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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>stackjava.com</groupId>
<artifactId>helloworld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>helloworld</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
FIle HelloworldApplication.java được tự động tạo.
package stackjava.com.helloworld;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class HelloworldApplication {
public static void main(String[] args) {
SpringApplication.run(HelloworldApplication.class, args);
}
}
Spring Boot cung cấp SpringApplication class để khởi động cho ứng dụng Spring từ hàm main() sử dụng static method SpringApplicaiton.run:
@SpringBootApplication là một annotation tiện ích, nó tương đương với việc ta thêm các annotation sau:
@Configurationgán class này thành một bean của application context.@EnableAutoConfiguration@EnableWebMvc.@ComponentScan.
Kể từ khi spring-boot-starter-web thêm Tomcat và Spring MVC, auto-configuration sẽ giả sử rằng bạn phát triển một ứng dụng web và sẽ setup Spring phù hợp.
package stackjava.com.helloworld.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class BaseController {
@RequestMapping("/")
public String welcome() {
return "index";
}
}
- Annotation @Controller và @RequestMapping annotations (Xem lại @Controller, @RequestMapping trong Spring MVC): sẽ tự động mapping đường dẫn
/tới fileindex.htmltrong folderresources
Tạo file view
Tạo file index.html trong folder resources/templates
<html> <head> <title>Spring Boot</title> </head> <body> <h1>Hello Spring Boot</h1> </body> </html>
Chạy module
Click chuột phải vào file HelloworldApplication.java và chọn run.
Kết quả:
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.3.0.RELEASE) 2020-06-01 16:58:28.002 INFO 400 --- [ main] s.com.helloworld.HelloworldApplication : Starting HelloworldApplication on superman with PID 400 (F:\project\stackjava\helloworld\target\classes started by stackjava in F:\project\stackjava) 2020-06-01 16:58:28.021 INFO 400 --- [ main] s.com.helloworld.HelloworldApplication : No active profile set, falling back to default profiles: default 2020-06-01 16:58:28.920 INFO 400 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2020-06-01 16:58:28.928 INFO 400 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2020-06-01 16:58:28.928 INFO 400 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.35] 2020-06-01 16:58:28.929 INFO 400 --- [ main] o.a.catalina.core.AprLifecycleListener : An older version [1.2.21] of the Apache Tomcat Native library is installed, while Tomcat recommends a minimum version of [1.2.23] 2020-06-01 16:58:28.929 INFO 400 --- [ main] o.a.catalina.core.AprLifecycleListener : Loaded Apache Tomcat Native library [1.2.21] using APR version [1.6.5]. 2020-06-01 16:58:28.929 INFO 400 --- [ main] o.a.catalina.core.AprLifecycleListener : APR capabilities: IPv6 [true], sendfile [true], accept filters [false], random [true]. 2020-06-01 16:58:28.929 INFO 400 --- [ main] o.a.catalina.core.AprLifecycleListener : APR/OpenSSL configuration: useAprConnector [false], useOpenSSL [true] 2020-06-01 16:58:28.947 INFO 400 --- [ main] o.a.catalina.core.AprLifecycleListener : OpenSSL successfully initialized [OpenSSL 1.1.1a 20 Nov 2018] 2020-06-01 16:58:29.016 INFO 400 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2020-06-01 16:58:29.017 INFO 400 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 912 ms 2020-06-01 16:58:29.127 INFO 400 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2020-06-01 16:58:29.183 INFO 400 --- [ main] o.s.b.a.w.s.WelcomePageHandlerMapping : Adding welcome page template: index 2020-06-01 16:58:29.329 INFO 400 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2020-06-01 16:58:29.341 INFO 400 --- [ main] s.com.helloworld.HelloworldApplication : Started HelloworldApplication in 1.732 seconds (JVM running for 2.654)
Mặc định spring web sẽ chạy trên port 8080.
Mở trình duyệt và truy cập địa chỉ localhost:8080
Okay, Done!
Download code ví dụ trên tại đây.
References:







