Some preparatory work:
This section describes how to develop a simple "Hello World!" Web application, the application highlights some of the key features of Spring Boot. We use Maven to build the project, because most of the IDE supports it.
Tips: spring.io site contains many of the "Getting Started" use Spring Boot's Guide . If you need to solve specific problems, please first check. By going start.spring.io and select the "Web" from the start dependencies searcher, the following steps can be simplified. This will generate a new project structure, so you can start coding immediately. View Spring Initializr documentation for more details.
Before you begin, open a terminal and run the following command to make sure you have a valid Java and Maven version:
$ java -version java version "1.8.0_102" Java(TM) SE Runtime Environment (build 1.8.0_102-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.102-b14, mixed mode)
$ mvn -v Apache Maven 3.5.4 (1edded0938998edf8bf061f1ceb3cfdeccf443fe; 2018-06-17T14:33:14-04:00) Maven home: /usr/local/Cellar/maven/3.3.9/libexec Java version: 1.8.0_102, vendor: Oracle Corporation
Tips: This example needs to be created in its own folder. Subsequent instructions assume that you have created an appropriate folder, and it is the current directory.
1. Create POM
We need to create a Maven file. It is a recipe for building projects. You can choose your favorite text editor and add the following:pom.xml
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 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> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.1.8.RELEASE</version> </parent> <!-- Additional lines to be added here... --> </project>
The above list should provide an effective build for you. You can run to test command (Currently, you can "jar is empty! - no content marked as containing" the warning is ignored) .mvn package
At this point, you can project into IDE (most modern Java IDE includes built-in support for Maven). For simplicity, we continue to use a plain text editor in this example.
2. Add classpath dependencies
Spring Boot offers a number of "starter" so that you can add the jar to your classpath. Our sample application has been in the POM parent
use section spring-boot-starter-parent
. spring-boot-starter-parent
Is a special starter, provide useful Maven defaults. It also provides a dependency-management
section so that you can ignore the "blessed" dependency version
label.
Other "starter" provided dependencies in the development of specific types of applications that might be required. Since we are developing Web applications, so we add a spring-boot-starter-web
dependency. Prior to this, we can view the current status by running the following command:
$ mvn dependency:tree [INFO] com.example:myproject:jar:0.0.1-SNAPSHOT
mvn dependency:tree
Command will project dependencies tree representation printed. You can see spring-boot-starter-parent
itself does not provide any dependencies. To add the necessary dependencies, edit pom.xml
and add directly below the dependencies: parent
spring-boot-starter-web
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
If you run again mvn dependency:tree
, you will see that there are many other dependencies, including Tomcat Web server and Spring Boot itself.
3. Write code
To complete our application, we need to create a Java file. By default, Maven through src/main/java
the path to compile the source code, so you need to create the folder structure, and then src/main/java
add a file called Example.java under the path that contains the following code:
import org.springframework.boot.*; import org.springframework.boot.autoconfigure.*; import org.springframework.web.bind.annotation.*; @RestController @EnableAutoConfiguration public class Example { @RequestMapping("/") String home() { return "Hello World!"; } public static void main(String[] args) { SpringApplication.run(Example.class, args); } }
While there is not much the code, but many things are taking place. We will gradually introduce an important part in the next sections.
3.1 @RestController and @RequestMapping comment
Example
The first annotation class is . This is called stereotype annotations. It provides tips for people to read the code, it is for Spring, @ RestController notes played a particular role. In this example, our class is a Web request controller ( ), and therefore in the Spring when processing incoming Web requests would consider using it. @RestController
@Controller
@RequestMapping notes provide "routing" information. It tells Spring, with any HTTP requests will be mapped to the method. @RestController annotation tells Spring will result string directly presented to the caller. /
home
Tips: and annotations are annotations Spring MVC (they are not specific to Spring Boot's). For more detailed information, see the reference documentation of Spring MVC part . @RestController
@RequestMapping
3.2 @EnableAutoConfiguration comment
The second class-level annotations are . This annotation tells Spring Boot your jar depend added to "guess" how you configure Spring. Since the addition of Tomcat and Spring MVC, and therefore auto-configuration assumes that you are developing a Web application and set Spring. @EnableAutoConfiguration
spring-boot-starter-web
Launcher and automatic configuration (Starters and Auto-configuration)
automatic configuration designed to work with "starter" used in conjunction with, but these two concepts are not directly linked. You are free to choose jar dependencies outside start the program. Spring Boot will do everything automatically configure your application.
3.3 main method
The last part of our application method. It just follows the standard Java convention method of application entry. Our primary method by calling the delegate to Spring Boot the class. We will guide our application and start the Spring, which in turn will start the Tomcat Web server is configured automatically. We need to be passed as a parameter to the method, in order to tell which are the main components of Spring. Parameter array is also passed to the command line parameters.main
run
SpringApplication
SpringApplication
Example.class
run
SpringApplication
args
4. Run the sample
At this point, your application should work. Since you use POM t, so you have to run a useful goal, it can be used to launch the application. Open cmd window and enter the project root directory, type to launch the application. You should see output similar to the following: spring-boot-starter-parent
mvn spring-boot:run
$ mvn spring-boot:run . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.8.RELEASE) ....... . . . ....... . . . (log output here) ....... . . . ........ Started Example in 2.222 seconds (JVM running for 6.514)
Access a Web browser , you should see the following output: localhost:8080
Hello World!
To properly exit the application, press ctrl-c
5. Create an executable Jar
By creating a completely independent executable jar file that can be run in a production environment to the end of the examples. Executable jar (sometimes referred to as "fat jars") that contains your compiled classes and all dependencies jar archive code that needs to run.
Executable jar and the Java (and the Java Executable JARs)
the Java does not provide a standard method to load the nested jar files (jar file the jar itself contains) a. If you want to distribute stand-alone application, you may encounter problems.
To solve this problem, many developers use the "uber" jars. uber jar from the application of all the dependencies of all classes into a package archive. The problem with this approach is that it is difficult to see what the library is included in the application.
If you use the same file name in multiple jar (but with a different content), may also cause problems.
Spring Boot uses a Different Approach , in fact, allows you to directly nested jar.
To create an executable jar, we need to add to our . In. To do this, insert the following line in the following section: spring-boot-maven-plugin
pom.xml
dependencies
<build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
spring-boot-starter-par
The POM contains configured to bind to repackage ( ) target. If you do not use the parent POM, you need to own statement this configuration. For more information, see the plug-in documentation . <executions>
repackage
Save your and run from the command line , as follows: pom.xml
mvn package
$ mvn package [INFO] Scanning for projects... [INFO] [INFO] ------------------------------------------------------------------------ [INFO] Building myproject 0.0.1-SNAPSHOT [INFO] ------------------------------------------------------------------------ [INFO] .... .. [INFO] --- maven-jar-plugin:2.4:jar (default-jar) @ myproject --- [INFO] Building jar: /Users/developer/example/spring-boot-example/target/myproject-0.0.1-SNAPSHOT.jar [INFO] [INFO] --- spring-boot-maven-plugin:2.1.8.RELEASE:repackage (default) @ myproject --- [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------
如果查看target
目录,则应该看到 myproject-0.0.1-SNAPSHOT.jar
。该文件的大小应为10 MB左右。如果想窥探内部,可以使用jar tvf,如下所示:
$ jar tvf target/myproject-0.0.1-SNAPSHOT.jar
您还将在target
目录中看到一个名为 myproject-0.0.1-SNAPSHOT.jar.original
的小得多的文件。这是Maven在Spring Boot重新打包之前创建的原始jar文件。
要运行该应用程序,请使用java -jar
命令,如下所示:
$ java -jar target/myproject-0.0.1-SNAPSHOT.jar . ____ _ __ _ _ /\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \ ( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \ \\/ ___)| |_)| | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: (v2.1.8.RELEASE) ....... . . . ....... . . . (log output here) ....... . . . ........ Started Example in 2.536 seconds (JVM running for 2.864)
As before, we want to exit the application, press . ctrl-c
If you look at the target directory, you should see myproject-0.0.1-SNAPSHOT.jar. The file size should be about 10 MB. If you want to look into the inside, can be used jar tvf, as follows: