Spring Boot 2.2.2.RELEASE version of the Chinese reference documentation

EDITORIAL

When I first contacted the MongoDB is to make a monitoring system and log analysis system. At that time, when the data in MongoDB Java operating data, are to find in the online Demo sample then complete the function, I believe we have the same experience, the Internet is such a big entry of a small Demo, there is little in-depth explanation. After the function call through, on her own efficiency issues, they try to start their own blind road, then initialize multiple MongoClient objects in the product launch phase, engage in a kind of pond a similar connection pool, after the completion of that psyche they really a smart boy. After about a year, in order to systematically learn some advanced features of MongoDB, the Internet looking for Blog and Mooc no fruit, I put eyes fell on the official document provided by MongoDB. Look documents from the beginning, I felt that the perception is too shallow. MongoDB Java Driver official document outset introduce MongoClient has already done a kind of connection pooling mechanism, and software developers only need to create the object stage set parameters. Then slowly more and more aware that a new entry-technology is the best way to begin to read the official document, like Spring, MongoDB official documentation of such technologies, entry, advanced features are very detailed. Do the right thing the right way. About Spring Boot reference document translation, because the later work began on a large scale with Spring Boot technology, in their own learning process and found that Spring Boot relatively new version of reference documents are generally available only in English, and in some communities Issue spontaneous translation is not up to date. In the process of learning, just translate the reference document also sorted out, it can be considered an archive. For inquiries, please share with everyone.

About the content of the document, there are a few things to the next statement:

  • Due to time constraints, the initial document put before using Maven way Spring Boot partially translated, Gradle way and Spring CLI translation section will follow-up if necessary sorted out.
  • Documents from the second part of the Spring Boot official document of the beginning, because the first part only provides navigation Spring Boot reference document.
  • On translation of the letter, and Elegance. I can only try to do "trust" and "up", "elegant" did not reach because of lack of capacity. Document has translated some of the words, in the case of the letter can not be done, I will refer to the English words or text document posted, we can appreciate the intent of the official documents themselves want to express.

Start

If you are from Spring Boot or "Spring" began, read this section. It answers the basic "what" how "and" why "questions. It includes an introduction and installation instructions for Spring Boot Then, we will guide you through building your first Spring Boot application, and discuss some of the discussions The core principle.

2.1 Introduction to Spring Boot

Spring Boot the creation of an independent operational, production-level easy Spring-based applications. We hold opinionated [opinionated] perspective on Spring platform and third-party libraries, so that you can with a minimum of trouble [fuss] started. Most Spring Boot application requires very little Spring configuration.

You can create Java applications can be deployed started by using the java -jar or more traditional war using Spring Boot. We also offer a command-line tool to run "spring Script" is.

Our main objectives are:

  • Getting faster and provide a fundamental experience can be widely accessible to all Spring developers.
  • Out of the box, but the demand began to differ from the default values, so it will soon be off the hook. [Be opinionated out of the box but get out of the way quickly as requirements start to diverge from the defaults.]
  • Offers a range of large-scale projects of common non-functional features (such as embedded servers, security, index, health checks and externalize configuration).
  • No code generation and does not require XML configuration.

2.2 System Requirements

Spring Boot 2.2.2.RELEASE need Java 8, and is compatible with Java 13 (inclusive). Also you need to Spring Framework 5.2.2.RELEASE or later.

Provides a clear build tools to build support for the following:

2.2.1 Servlet container

Spring Boot supports the following embedded Servlet container:

2.3 Installation Spring Boot

Spring Boot can be used with the "classic" Java development tools, it can be used as a command line tool installed. Either way, you need to release Java SDK v1.8 or higher.

Before you begin, you should check the current version of Java installed, use the following command:

$ java -veriosn

If you are not familiar with Java development, or want to try using Spring Boot, you might first try to use Spring Boot CLI (Command Line Interface). Otherwise, proceed to "classic" installation instructions.

Installation instructions 2.3.1 Java developers

You can use any standard Java libraries use the same Spring Boot. To do this, comprising a respective spring-boot- * in the class path. jar file. Spring Boot does not require any special tools are integrated, so you can use any IDE or text editor. Moreover, Spring Boot application there is nothing special, so you can run like other debug and run Java programs, like Spring Boot application.

Although you can copy the Spring Boot jar, but we usually recommend that you use to build support for dependency management tools (such as Maven or Gradle).

Maven installation

Spring Boot with Apache Maven version 3.3 or higher compatible. Maven if not already installed, you can follow maven.apache.org operating instructions.

On many operating systems, Maven can be installed with Package Manager. If you use OSX Homebrew, try the brew install maven. Ubuntu users can run sudo apt-get install maven. Windows users have Chocolatey choco install maven can run from an elevated (Administrator) prompt.

Spring Boot dependencies used groupId is org.springframework.boot. Typically, your Maven POM file is inherited from the spring-boot-starter-parent project, and declare a dependency on one or more "Starters" is. Spring Boot also offers an optional Maven plugin to build an executable jar.

The following listing shows a typical pom.xml 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.2.2.RELEASE</version>
    </parent>

    <!-- Override inherited settings -->
    <description/>
    <developers>
        <developer/>
    </developers>
    <licenses>
        <license/>
    </licenses>
    <scm>
        <url/>
    </scm>
    <url/>

    <!-- 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>
spring-boot-starter-parent is a good way to use Spring Boot, but may not always be appropriate. Sometimes you may need to inherit from the other parent POM, or you may not like our default settings. In these cases, see Using Spring Boot without a parent POM in order to obtain an alternative use to import a range of solutions.

Gradle installation

2.3.2  Installation Spring Boot CLI

2.3.3  upgrade from an earlier version of Spring Boot

2.4 Develop your first Spring Boot application

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.

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 accessing 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 immediately start coding . 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
The 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.

2.4.1 Creating POM

We need to create a Maven pom.xml file. pom.xml is formula [recipe] for building projects. Open your favorite text editor and add the following:

<?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.2.2.RELEASE</version>
    </parent>

    <description/>
    <developers>
        <developer/>
    </developers>
    <licenses>
        <license/>
    </licenses>
    <scm>
        <url/>
    </scm>
    <url/>

    <!-- Additional lines to be added here... -->

</project>

The above list should provide you with a valid construct. You can test it by running the command mvn package (Currently, you can ignore the "jar will be empty - no content was marked for inclusion!" Warning).

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.4.2 Add classpath dependencies

Spring Boot offers a number of "starter" so that you can add the jar to your classpath. Our smoke test application uses spring-boot-starter-parent in the parent POM's part. spring-boot-starter-parent is a special starter, provide useful Maven default value. It also provides a dependency management section, so you can save the best version of the label dependencies.

Other "Starters" provide a dependency in the development of specific types of applications that might be required. Since we are developing Web applications, so we added 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 displays the project dependencies tree representation of the relationship. You can see the spring-boot-starter-parent itself does not provide any dependencies. To add the necessary dependencies, edit pom.xml and add the spring-boot-starter-web dependencies directly below the parent section:

<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 there are now many other dependencies, including Tomcat Web server and Spring Boot itself.

2.4.3 write code

To complete our application, we need to create a Java file. By default, Maven from src / main / java compiler source code, so you need to create the folder structure, and then add a file named src / main / java / Example.java file to contain 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);
    }

}

Although there is not much code, but the code is a lot of things happen. We will gradually introduce an important part in the next sections.

@RestController and @RequestMapping comment

The first class is annotated Example @RestController. This is called stereotype annotations. It provides tips for people to read the code, for Spring, the class played a particular role. In this case, our class is a Web @Controller, therefore in the Spring when processing incoming Web requests would consider using it.

@RestController and @RequestMapping Spring MVC annotations are comments (they are not Spring Boot proprietary annotation). For more detailed information, see the reference documentation of Spring MVC section part.

@EnableAutoConfiguration comment

The second class-level annotations are @EnableAutoConfiguration. This annotation tells Spring Boot according to the added jar dependency "guess" how you configure Spring. Since the spring-boot-starter-web adds Tomcat and Spring MVC, and therefore automatic configuration assumes that you are developing a Web application and set Spring.

Launcher and automatic configuration

Automatic configuration designed to work with "starter" used in conjunction with, but the 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.

Main method

The last part is the main method of our application. It just follows the standard Java convention method of application entry point. Our primary method delegates to the Spring Boot of SpringApplication class by calling the run. SpringApplication will guide our application and start the Spring, which in turn will start the Tomcat Web server is configured automatically. We need to Example.class pass to run as a parameter to tell SpringApplication which are the main components of Spring. args an array is also disclosed in passing through any command line parameters.

2.4.4 Running the sample

At this point, your application should work. Since you're using a spring-boot-starter-parent parent POM, so you have to run a useful goal, it can be used to launch the application. Enter the command from the root project directory mvn spring-boot: run to start the application. You should see output similar to the following:

$ mvn spring-boot:run

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

If you open a Web browser to localhost: 8080, you should see the following output:

Hello World!

To properly exit the application, press ctrl-c.

2.4.5 Build the executable Jar

By building a completely independent executable jar file that can be run in a production environment to the end of the example section. Executable jar (sometimes referred to as "fat jar") that contains your compiled classes and all dependencies jar archive code that needs to run.

And Java executable jar

Java does not provide load nested jar files (jar file the jar itself contains) standard method. If you want to distribute stand-alone application, you may encounter problems. To solve this problem, many developers use the "super [uber]" container. Super [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 using another method actually allows you to directly nested jar.

To create an executable jar, we need to add spring-boot-maven-plugin in our pom.xml. To do this,  dependencies  insert the following line in the following section:

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>
spring-boot-starter-parent POM contains the configuration of repackaging to bind target. If you do not use the parent POM, you need to own statement this configuration. For more information, see the plug-in documentation .

Save your pom.xml and run mvn package from the command line, as follows:

$ 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.2.2.RELEASE:repackage (default) @ myproject ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

If you view the target directory, you should see myproject-0.0.1-SNAPSHOT.jar package. The file size should be about 10 MB. If you want to see the internal structure, can be used jar tvf command as follows:

$ jar tvf target/myproject-0.0.1-SNAPSHOT.jar

You will also see the size of a file named myproject-0.0.1-SNAPSHOT.jar.original relatively small files in the target directory. This is the original jar file Maven created before repackaging Spring Boot.

To run the application, use the java -jar command as follows:

$ java -jar target/myproject-0.0.1-SNAPSHOT.jar

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::  (v2.2.2.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.

The second chapter is finished.

 


Subsequent chapters and other Java-related knowledge and information gets updated in the public numbers, the next focus slightly ~

 

Guess you like

Origin www.cnblogs.com/feizao/p/12046503.html
Recommended