此文章为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:
- Java 8 and is compatible up to Java 11(included).
- Spring Framework 5.1.6.RELEASE or above
- Maven 3.3 or above
Chapter 10. Installing Spring Boot
Installation Instruction for the Java Developer (Ways)
- Including the appropriate spring-boot-*.jar files on your classpath in the same way as any standard Java library.
- 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.
- Install the Spring Boot CLI and run spring run app.groovy.
@RestControllerclass ThisWillActuallyRun { @RequestMapping("/") String home() { "Hello World!" }}
- 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
- Create a Maven pom.xml file and complete it, then run mvn package to test the working build.
mvn dependency:tree
- 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); }}
- Run mvn spring-boot:run to start the application.
- 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.
- run mvn package to create executable jar.
- 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]