springBoot create project

 

1. Introduction Spring Boot

Spring Boot can easily create a separate Spring-based production-level applications can run. We Spring platform and third-party libraries, a self-righteous point of view, so that you can easily get started. Most Spring Boot application requires very little Spring configuration.

You can use Spring Boot can be used to create java -jaror deploy a more traditional war Java application startup  . We also offer a command-line tool to run "spring Script" is.

Our main objectives are:

  • Provide for all Spring developers radically faster experience and entry can be widely accessible.

  • Out of the box, but as demand begins to deviate from the default values ​​and quickly out of the woods.

  • Provide (such as embedded servers, security, metrics, health checks and externalizing configuration) common to a series of non-functional features large-scale projects.

  • Absolutely no code generation and does not require XML configuration.

 

2. System Requirements

Spring Boot 2.2.0.M2 need Java 8, and is compatible with Java 11 (included).  You need Spring Framework {spring-framework-version } or later version .

It provides explicit support for the construction of the following build tools:

Build Tool Version

Maven

3.3+

2.1. Servlet container

Spring Boot supports the following embedded servlet container:

Name Servlet Version

Tomcat 9.0

4.0

You can also Spring Boot application deployed to any Servlet 3.1+ compatible container.

3. Install 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 Java SDK v1.8 or later. Before you begin, you should check the current Java installation using the following command:

$ java -version

If you are not familiar with Java development, or want to try Spring Boot, you may need to try the Spring the Boot CLI (Command Line Interface). Otherwise, please continue reading "classic" installation instructions.

3.1.1. Maven installation

Spring Boot with Apache Maven version 3.3 or higher compatible. If you have not installed Maven, in accordance with the maven.apache.org be operating instructions .

3.2. Spring Boot CLI installation

Spring Boot CLI (Command Line Interface) is a command-line tool that you can use it to quickly use Spring prototyping. It allows you to run Groovy scripts, which means you have the familiar Java-like syntax, without much boilerplate code.

You do not need to use the CLI to use Spring Boot, but it is definitely the fastest way to achieve Spring applications.

3.2.1. Installation Manual

You can download the release from Spring Spring CLI software library:

Also provides the latest  snapshot of the distribution .

After the download is complete, follow  the decompressed archive of  INSTALL.txt operating instructions. In short, the files in the directory have aspring script ( spring.batfor Windows) . Alternatively, you can use the file (the script can help you make sure that the class path is set correctly).bin/.zipjava -jar.jar

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.

 

4.1. Creating POM

We need to create Maven from the  pom.xmlbeginning of the file. This pom.xmlis the formula used to construct the project. 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.0.M2</version>
    </parent>

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

    <!-- (you don't need this if you are using a .RELEASE version) -->
    <repositories>
        <repository>
            <id>spring-snapshots</id>
            <url>https://repo.spring.io/snapshot</url>
            <snapshots><enabled>true</enabled></snapshots>
        </repository>
        <repository>
            <id>spring-milestones</id>
            <url>https://repo.spring.io/milestone</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-snapshots</id>
            <url>https://repo.spring.io/snapshot</url>
        </pluginRepository>
        <pluginRepository>
            <id>spring-milestones</id>
            <url>https://repo.spring.io/milestone</url>
        </pluginRepository>
    </pluginRepositories>
</project>

4.2. Add Classpath dependencies

Spring Boot offers a number of "Starters", can be added to the classpath jar. Our sample application has beenspring-boot-starter-parent in the parentPOM section to use too . This spring-boot-starter-parentis a special starter, provide useful Maven defaults. It also provides a   section, so you can omit the "blessing" dependencies mark.dependency-managementversion

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 add a  spring-boot-starter-webdependency. Prior to this, we can view the current contents by running the following command:

$ mvn dependency:tree 

[INFO] com.example:myproject:jar:0.0.1-SNAPSHOT

The mvn dependency:treecommand prints project dependencies tree representation. You can see that it spring-boot-starter-parentdoes not provide dependency. To add the necessary dependencies, editpom.xml and spring-boot-starter-webin the parentsection below to add  a dependency :

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>
</dependencies>

4.3. Write code

To complete our application, we need to create a Java file. By default, Maven compile the source codesrc/main/java , so you need to create the folder structure, and then add a named src/main/java/Example.javacontains the following code files :

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);
    }

}

4.3.1. In@RestController and @RequestMappingnotes

Our Exampleclass first comment Shi @RestController. This is called  stereotype annotations. It provides tips for the reading of the code, while Spring provides tips for the particular role. In this case, our class is a Web @Controller , and therefore in the Spring when processing incoming Web requests will consider it.

The @RequestMappingnotes provide "routing" information. It tells Spring, with any /path HTTP request should be mapped to the homemethod. The@RestController annotation tells Spring to make the resulting string is returned directly to the caller.

If mvn dependency:treere- run , you will find that there are now many other dependencies, including Tomcat Web server and Spring Boot itself.

4.3.2. @EnableAutoConfiguration comments

The second class-level annotation is @EnableAutoConfiguration. This annotation tells Spring Boot according jar dependencies you add a "guess" how you want to configure Spring. Since the spring-boot-starter-webaddition of Tomcat and Spring MVC  since , automatic configuration assumes that you are developing a Web application and set Spring.

4.3.3. "Main" method

The last part of our application mainmethod. This is just to follow the standard method of Java application entry point agreement. Our main method SpringApplicationby calling the delegate to Spring Boot the classrunSpringApplicationGuide our application, start the Spring, and then start the Tomcat Web server automatically configured. We need Example.classas a parameter passed to therun method to tell SpringApplicationwhich are the main components of Spring. The args array is also passed to disclose any command line parameters.

4.4. Run the sample

At this point, your application should work. Since you use spring-boot-starter-parent POM, runso you can use a useful goal to start the application. mvn spring-boot:runFrom the root project directory , type to launch the application. You should see output similar to the following:

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

(The above is a screenshot of their own)

 

Cipian operations from the original official website: https: //docs.spring.io/spring-boot/docs/2.2.0.M2/reference/html/getting-started.html#getting-started-first-application

Guess you like

Origin www.cnblogs.com/xiaohuomiao/p/10926940.html