20220506 Getting Started

Time:2022-6-20

preface

Document address

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 -jarOr a more traditional War deployment launched Java application. We also provide a runspring scriptsCommand 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.7needJava 8, and withJava 17(including) compatibility. Also requiredSpring Framework 5.3.19Or 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.txtInstructions. In short,.zipIn the filebin/There is a spring script in the directory(spring.batFor Windows). Or, you can.jarFile 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.xmlIs 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 POMparentUsed in sectionspring-boot-starter-parent

spring-boot-starter-parentIs a special starter(starter), providing useful Maven defaults. It provides adependency-managementPart of a dependency (GAV) can be omittedversionlabel.

Since we are developing a web application, we have added aspring-boot-starter-webDependencies. Before that, we can view Maven dependencies by running the following command:

mvn dependency:tree

mvn dependency:treeCommand prints a tree representation of the project dependencies. You can seespring-boot-starter-parentItself 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/javaCompile the source code, so you need to create the directory structure, and then add a directory namedsrc/main/java/MyApplication.javaFile 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. @RestControllerand@RequestMappingannotation

slightly

4.3.2. @EnableAutoConfigurationannotation

@EnableAutoConfigurationAnnotations tell spring boot to “guess” how you want to configure spring according to the jar dependencies you add. becausespring-boot-starter-webTomcat 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. mainmethod

mainMethod is a standard method that follows the Java conventions of application entry points. oursmainMethod by callingSpringApplication.runDelegated to spring bootSpringApplicationClass to guide our application to start spring, and then start the automatically configured Tomcat web server. We need toMyApplication.classPassed as a parameter torunMethod, letSpringApplicationDetermine which is the main spring component.argsArrays 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-parentPOM, so you have a usefulrunTarget, which can be used to launch the application. Type from the root project directorymvn spring-boot:runTo 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-pluginAdd topom.xml。 To do this, pleasedependenciesInsert the following line below the section:

org.springframework.boot
            spring-boot-maven-plugin

spring-boot-starter-parentPOM contains bindingsrepackageTargetedto configure. If you do not use the parent POM, you need to declare this configuration yourself. For details, seePlug in documentation

preservationpom.xmlAnd run from the command linemvn package

mvn package

If you viewtargetDirectory, 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 alsotargetSee a much smallermyproject-0.0.1-SNAPSHOT.jar.originalDocuments. This is the original jar file created by Maven before it is repackaged by spring boot.

To run the application, use the followingjava -jarCommand:

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

Recommended Today

Playing with Python data processing in the way of tidyverse

preface Today, we will introduce aPythonThird party Library——plydata, this library is based onRMediumdplyr、tidyrandforcatsPackage. Many function names are borrowed directly plydatause>>As a pipe operator, used to overrideply(data, *verbs)Function, currently only pandasDataFrameData structure, and database support may be added later Let’s see how to use itplydataLet’s play with data manipulation use First, usepipInstall pip install plydata Let’s […]