Spring boot là gì? Viết chương trình đầu tiên dùng spring boot.

Spring boot trong spring io - Ảnh: Internet

Spring boot là gì?
- Spring boot là một project trong Spring io platform nằm ở layer IO Execution.  Nó giảm thiểu effort cần thiết để tạo production-ready, DevOps-friendly, XML-free Spring applications. Đơn giản hóa bootstrapping của các dự án Spring với source code tối thiểu, thực hiện khả năng mở rộng các tính năng hoạt động như tự động kiểm tra metrics, enpoints,... và hỗ trợ embedded containers cho phép tạo ra các tập lệnh có khả năng tự thực thi. Làm cho project dùng spring trở nên độc lập và gọn nhẹ.

- Bạn có thể dùng spring boot để tạo ứng dụng Java chạy bằng command line 'java -jar' hoặc export gói war để deploy lên server như thông thường. Spring boot cung cấp cho bạn một cli tool để run 'spring scripts'.

Yêu cầu hệ thống
Spring Boot 1.2.2.RELEASE yêu cầu JDK 7 và Spring Framework 4.1.3 hoặc cao hơn. Bạn có thể dùng spring boot với Java 6 nhưng phải chỉnh lại cấu hình 'cấu hình với java 6details. Yêu cầu maven (3.2+) hoặc Gradle (1.12+).

* Mặc dùng spring boot support Java 6 hoặc 7 nhưng tốt nhất bạn nên dùng Java 8.

Spring boot embedded servlet containers:
Name      Servlet Version Java Version

Tomcat 8         3.1              Java 7+

Tomcat 7         3.0              Java 6+    

Jetty 9          3.1              Java 7+

Jetty 8          3.0              Java 6+

Undertow 1.1     3.1              Java 7+


Tạo ứng dụng spring boot đầu tiên

* Kiểm tra máy bạn đã cài JDK và Maven chưa?

$ java -version
java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)

$ mvn -v
Apache Maven 3.2.3 (33f8c3e1027c3ddde99d3cdebad2656a31e8fdf4; 2014-08-11T13:58:10-07:00)
Maven home: /Users/user/tools/apache-maven-3.1.1
Java version: 1.7.0_51, vendor: Oracle Corporation

* Tạo Maven project với file pom.xml

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.example</groupId>
    <artifactId>myproject</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <!-- Inherit defaults from Spring Boot -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.2.RELEASE</version>
    </parent>

    <!-- Add typical dependencies for a web application -->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <!-- Package as an executable jar -->
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

* Tạo lớp java của chương trình in ra "hello spring boot!"

import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.stereotype.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello spring boot!";
    }

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Example.class, args);
    }

}

@RestController - Trả về kết quả trực tiếp theo mô hình MVC. 
@RestController và @RequestMapping annotation là spring MVC.

SpringApplication.run(Example.class, args);

Sẽ tự động cấu hình và gọi web server tomcat chạy.

* Để chạy ứng dụng ta dùng lệnh: mvn spring-boot:run 

$ mvn spring-boot:run

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v1.2.2.RELEASE)
....... . . .
....... . . . (log output here)
....... . . .
........ Started Example in 2.222 seconds (JVM running for 6.514)


Mở trình duyệt truy cập vào localhost:8080 sẽ hiển thị kết quả:

Hello spring boot!


SHARE THIS

1 comment:

  1. This comment has been removed by the author.

    ReplyDelete