preface
1. introduction to spring boot
Spring boot helps you create standalone, production level spring based applications that can run.
You can use spring boot to createjava -jar
Or a more traditional War deployment launched Java application. We also provide a runspring scripts
Command line tools for.
The main objectives of spring boot are:
- Provide a faster and easier start-up experience for all spring development
- Out of the box, but as the requirements begin to diverge, you can quickly restart from the default configuration
- Provide a series of non functional functions common to large projects (such as embedded server, security, indicators, health check and external configuration)
- No code generation at all,No XML configuration required
2. system requirements
Spring Boot 2.6.7
needJava 8
, and withJava 17
(including) compatibility. Also requiredSpring Framework 5.3.19
Or later.
Supported build tools:
Build tool | edition |
---|---|
Maven | 3.5+ |
Gradle | 6.8. x、6.9. X and 7 x |
2.1. Servlet container
The following embedded servlet containers are supported:
name | Servlet version |
---|---|
Tomcat 9.0 | 4.0 |
Jetty 9.4 | 3.1 |
Jetty 10.0 | 4.0 |
Undertow 2.0 | 4.0 |
Spring boot applications can also be deployed to any servlet 3.1+ compliant container.
3. install spring boot
Spring boot can be used with “classic” java development tools or installed as a command line tool. Either way, you need Java SDK v1.8 or later. Before you begin, you should check your current Java installation with the following command:
$ java -version
3.1. Java developer installation instructions
You can use spring boot like any standard Java library
3.1.1. Maven installation
slightly
3.1.2. Gradle installation
slightly
3.2. Installing spring boot cli
The spring boot cli (command line interface) is a command line tool that you can use to quickly prototype using spring. It allows you to runGroovyScripts, which means you have familiar Java like syntax without too much boilerplate code.
You do not need to use the CLI to use spring boot, but it is a way to quickly launch spring applications without an IDE.
3.2.1。 Manual installation
You can download the spring cli distribution from the spring Software Repository:
After downloading, extract theINSTALL.txt
Instructions. In short,.zip
In the filebin/
There is a spring script in the directory(spring.bat
For Windows). Or, you can.jar
File usagejava -jar
(this script helps you ensure that the classpath is set correctly.).
3.2.2. Installing using sdkman
slightly
3.2.3. OSX homebrew installation
slightly
3.2.4. Macports installation
slightly
3.2.5. Command line completion
slightly
3.2.6. Windows scoop installation
slightly
3.2.7. Quick start spring cli example
You can use the following web application to test your installation. First, create aapp.groovy
, as follows:
@RestController
class ThisWillActuallyRun {
@RequestMapping("/")
String home() {
"Hello World!"
}
}
Then run it from the shell as follows:
$ spring run app.groovy
Open in your favorite web browserlocalhost:8080
。 You should see the following output:
Hello World!
4. develop your first spring boot application
This section describes how to develop a small “Hello world!”, A web application that highlights some of the key features of spring boot. We used Maven to build this project because most ides support it.
Before we start, open a terminal and run the following command to ensure that you have a valid version of Java and Maven installed:
$ java -version
openjdk version "1.8.0_302"
$ mvn -v
Apache Maven 3.8.3
4.1. Create POM
pom.xml
Is the recipe used to build the project
4.0.0
com.example
myproject
0.0.1-SNAPSHOT
org.springframework.boot
spring-boot-starter-parent
2.6.7
4.2. Add classpath dependency
Spring boot provides a number of “starters” that allow you to add jars to your classpath. Test application POMparent
Used in sectionspring-boot-starter-parent
。
spring-boot-starter-parent
Is a special starter(starter
), providing useful Maven defaults. It provides adependency-management
Part of a dependency (GAV) can be omittedversion
label.
Since we are developing a web application, we have added aspring-boot-starter-web
Dependencies. Before that, we can view Maven dependencies by running the following command:
mvn dependency:tree
mvn dependency:tree
Command prints a tree representation of the project dependencies. You can seespring-boot-starter-parent
Itself does not provide any dependencies. To add the necessary dependencies, edit yourpom.xml
, and add dependencies to it:spring-boot-starter-web
org.springframework.boot
spring-boot-starter-web
4.3. Writing code
To complete our application, we need to create a java file. By default, mavensrc/main/java
Compile the source code, so you need to create the directory structure, and then add a directory namedsrc/main/java/MyApplication.java
File containing the following code:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@EnableAutoConfiguration
public class MyApplication {
@RequestMapping("/")
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(MyApplication.class, args);
}
}
There isn’t much code here, but a lot is happening. We will cover the important parts step by step in the next few sections.
4.3.1. @RestController
and@RequestMapping
annotation
slightly
4.3.2. @EnableAutoConfiguration
annotation
@EnableAutoConfiguration
Annotations tell spring boot to “guess” how you want to configure spring according to the jar dependencies you add. becausespring-boot-starter-web
Tomcat and spring MVC have been added, and automatic configuration assumes that you are developing web applications and setting up spring accordingly.
Initiators and autoconfiguration
Autoconfiguration is designed to work well with “initiators”, but the two concepts are not directly related.
You are free to choose jar dependencies other than the launcher. Spring boot still tries to configure your application automatically.
4.3.3. main
method
main
Method is a standard method that follows the Java conventions of application entry points. oursmain
Method by callingSpringApplication.run
Delegated to spring bootSpringApplication
Class to guide our application to start spring, and then start the automatically configured Tomcat web server. We need toMyApplication.class
Passed as a parameter torun
Method, letSpringApplication
Determine which is the main spring component.args
Arrays are also passed to expose any command line arguments.
4.4. Running example
At this point, your application should be working. Because you usedspring-boot-starter-parent
POM, so you have a usefulrun
Target, which can be used to launch the application. Type from the root project directorymvn spring-boot:run
To start the application.
mvn spring-boot:run
If you open a web browserlocalhost:8080
, you should see the following output:
Hello World!
To exit the application gracefully, pressctrl-c
4.5. Create an executable jar
Executable jars (sometimes calledfat jars
)Is an archive containing compiled classes and all jar dependencies that the code needs to run.
Executable jar and Java
Java does not provide a standard way to load nested jar files (JAR files themselves contained in jars). This can be problematic if you want to distribute a standalone application.
To solve this problem, many developers use “Uber” jars. A Uber jar packages all classes in all application dependencies into one archive. The problem with this approach is that it is difficult to see which libraries are in the application. Problems can also occur if you use the same file name (but different contents) in multiple jars.
Spring boot usesDifferent methods, so that you can actually nest jars directly.
To create an executable jar, we need tospring-boot-maven-plugin
Add topom.xml
。 To do this, pleasedependencies
Insert the following line below the section:
org.springframework.boot
spring-boot-maven-plugin
spring-boot-starter-parent
POM contains bindingsrepackage
Targetedto configure. If you do not use the parent POM, you need to declare this configuration yourself. For details, seePlug in documentation
preservationpom.xml
And run from the command linemvn package
mvn package
If you viewtarget
Directory, you should seemyproject-0.0.1-SNAPSHOT.jar
。 The file should be approximately 10 MB in size. If you want to view the contents, you can use
jar tvf target/myproject-0.0.1-SNAPSHOT.jar
You should alsotarget
See a much smallermyproject-0.0.1-SNAPSHOT.jar.original
Documents. This is the original jar file created by Maven before it is repackaged by spring boot.
To run the application, use the followingjava -jar
Command:
java -jar target/myproject-0.0.1-SNAPSHOT.jar