Spring Boot Reference Guide Memorandum

此文章爲Spring Boot Reference Guide(2.1.5.RELEASE)的備忘錄。

Chapter 8. Introducing Spring Boot

You can use Spring Boot to create a Java application that can be started by using java -jar or more traditional war deployments.

Spring Boot 2.5.1.RELEASE requires:

  1. Java 8 and is compatible up to Java 11(included).
  2. Spring Framework 5.1.6.RELEASE or above
  3. Maven 3.3 or above

Chapter 10. Installing Spring Boot

Installation Instruction for the Java Developer (Ways)

  1. Including the appropriate spring-boot-*.jar files on your classpath in the same way as any standard Java library.
  2. Install appropriate Maven, make POM file inheriting from the spring-boot-starter-patent project and declare dependence spring-boot-starter-web. Optionally config Maven plugin to package the project as an executable jar file.
<?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>

    <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>2.1.5.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>
The spring-boot-start-parent provides a dependency-management section so that we can omit version tags for "blessed" dependences.
  1. Install the Spring Boot CLI and run spring run app.groovy.
@RestController
class ThisWillActuallyRun {

    @RequestMapping("/")
    String home() {
        "Hello World!"
    }

}
  1. Generate a new project structure by going to https://start.spring.io to shortcut steps.
Upgrading from an Early Version of Spring Boot, you may need spring-boot-properties-migrator

Chapter 11. Developing First Spring Boot Application

  1. Create a Maven pom.xml file and complete it, then run mvn package to test the working build.
mvn dependency:tree
  1. src/main/java/Example.java
“import org.springframework.boot.*;
import org.springframework.boot.autoconfigure.*;
import org.springframework.web.bind.annotation.*;

@RestController
@EnableAutoConfiguration    // Tell Spring Boot to "guess" how you want to configure Spring based on the jar dependences.
public class Example {

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

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

}
  1. Run mvn spring-boot:run to start the application.
  2. Add the spring-boot-maven-plugin to out pom.xml to used to create an executable jar.
<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
the spring-boot-starter-parent POM includes <executions> configuration to bind the package goal. if you do not use the parent POM, you need to declare this configuration yourself. See https://docs.spring.io/spring-boot/docs/2.1.5.RELEASE/maven-plugin/usage.html for more detail.
  1. run mvn package to create executable jar.
  2. run jave -jar target/myproject-0.0.1-SNAPSHOT.jar to launch the application.
You can use jar -tvf to peed inside.

[To Be Continued]

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章