Independent study notes 1- Spring application

Spring applications independent of understanding

SpringBoot programming ideas in a book, this chapter focuses on the project SpringBoot command-line and graphical interface creation mode, SpringBoot project, jar package, packages and other war directory structure, and the most important is how the application SpringBoot jar package and war packet mode operation.

Creating SpringBoot project

It describes how to create a graphical interface springboot official website of the project as well as spring through the command line command to create a way to create a maven project. And use maven way to compile and run the project (instead of jar or bag war package).

Command line to create

By the Maven Archetype plugin (a tool Maven project template creation) to create.

1. Create a basic project mvn

To create a basic project using the mvn default base mvn template.

mvn archetype:generate -DgroupId=cn.xxx -DartifactId=springboot-project -Dpackage=cn.xxx.springboot.project -Dversion=1.0.0 -DinteractiveMode=false 

groupId : development team uniform logo, generally use two sections, the first section represents the domain, similar to the org, com, cn, etc., the second paragraph on behalf of the company name or team name.

artifactId : it can be simply understood as the actual project name.

Package : The default name generation program package, typically a combination of GroupId + ArtifactId.

Version : The version number identification.

interactiveMode : whether interactive creation.

2. Add SpringBoot dependent

In the project pom.xml to add Spring Boot Web-dependent and Spring Boot Parent POM file.

<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 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <!--添加SpringBoot Parent POM-->
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.RELEASE</version>
  </parent>

  <groupId>cn.xxx</groupId>
  <artifactId>springboot-project</artifactId>
  <packaging>jar</packaging>
  <version>1.0.0</version>
  <name>springboot-project</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    
    <!--添加springboot web 依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.2.0.RELEASE</version>
    </dependency>

    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>

3. Add the startup code

Automatically generated APP.java class add boot code, making startup classes.

@RestController
@SpringBootApplication //主要启动注解
public class App {
    
    @RequestMapping("/")
    public String index() {
        return "Welcome to my project!";
    }
    
    public static void main( String[] args ) {
        SpringApplication.run(App.class,args); //主要启动代码
    }
}

4. Start project

Used in the project path mvn spring-boot:runto run this project command.

Graphical creation

You can quickly create Spring Boot project in https://start.spring.io website.

1. Build the project

Fill out the site first interface group, artifact, and add dependencies in the web project builds.

2. Project Structure

src:

Store source code, test source code, resource files.

.gitignore:

It defines the git version control ignore list.

.mvn、mvnw、mvnw.cmd:

Maven build simple way, without having to install the operating environment in advance mavne binaries.

.mvn/wrapperThe jar is used to download binary files from maven maven official, properties in the jar file download fails, define download url.

mvnw和mvnw.cmdMvn to download binary files for the boot .mvn in the jar with the script, corresponding linux and windows platform.

3. Start project

In linux platform through the sh mvnw spring-boot:runcommand maven to download and run the project.

NOTE : This startup mode startup mode belongs to the development environment, the production environment is generally initiated by jar / war packet mode.

Create and execute the jar package

Packaged plugin to add pom.xml

SpringBoot project to build an executable jar package on the premise that you need to add spring-boot-maven-plugin to the pom.xml in.

Note : Items included in the graphical creation of this plugin, no need to add.

<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 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.0.RELEASE</version>
  </parent>
  <groupId>cn.xxx</groupId>
  <artifactId>springboot-project</artifactId>
  <packaging>jar</packaging>
  <version>1.0.0</version>
  <name>springboot-project</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-web</artifactId>
      <version>2.2.0.RELEASE</version>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <!--添加SpringBoot的打包插件-->
  <build>
  	<plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
     </plugins>
  </build>
  <!---->
</project>

Mvn package command execution

Execute the command line under the project path mvn packagecommand.

Run jar package

Command line java -jar jar包路径to run this package jar.

SpringBoot in Jar

jar package structure

After running mvn package command in the target / directory of the project will be:

Project name - .jar version number and project name - version number .jar.original two files.

Project name - the version number .jar.original

This file is packaged in the original jar file mvn

Path structure:

/ Classpath storage class file is compiled

/ META-INF stored meta information

Project name - version .jar

This file is the original file after mvn jar package after processing, the introduction of a third-party resource-dependent.

Path structure:

/ BOOT-INF / classes stored in the class file is compiled

/ BOOT-INF / lib storage jar package is dependent on a third party

/ META-INF stored meta information

/ org is stored in the class file associated SpringBoot

The difference between the traditional jar

springboot project out through mvn packaged in a jar with META-INF same except a conventional jar package, other conventional jar package structure is not the same, the structure more like a traditional war package web project.

How jar package is running

Since the jar package structure SpringBoot and traditional jar package is not the same, then the java -jar command to run it how it is?

java -jar command to start the class configuration file

java -jar the boot is arranged in the class / the META-INF / the MANIFEST.MF file MAIN-Class property of.

SpringBoot project jar jar package configuration boot class:

Main-Class: org.springframework.boot.loader.JarLauncher

jar package bootstrap class

MANIFEST.MF boot class file is configured in a jar package \ org \ springframework \ boot \ loader \ path under JarLauncher.class class .

Such mvn pom is configured by the plugin plug when added into repackage jar.original.

JarLauncher class responsibilities :

Start-Class boot and load the class MAINIFEST.MF configured (that is, plus springboot notes that springboot startup class).

Configuration classpath, so BOOT-INF / third party jar package can be found in the lib.

jar package bootstrap class Introduction

public class JarLauncher extends ExecutableArchiveLauncher {
    static final String BOOT_INF_CLASSES = "BOOT-INF/classes/"; //类路径
    static final String BOOT_INF_LIB = "BOOT-INF/lib/";         //第三方依赖jar路径

    public JarLauncher() {
    }

    protected JarLauncher(Archive archive) {
        super(archive);
    }

    protected boolean isNestedArchive(Entry entry) {
        return entry.isDirectory() ? 
            entry.getName().equals("BOOT-INF/classes/") : 			        
        	entry.getName().startsWith("BOOT-INF/lib/");  //判断是否以其开头来判断是jar包还是解压后的文件
    }

    public static void main(String[] args) throws Exception {
        (new JarLauncher()).launch(args);  //启动类引导
    }
}

JarLauncher supports both package also supports jar after jar unzip the package to run.

JarLauncher call the main method Start-Class category in the same process, and is ready to ClassPath before starting.

Proof and source code analysis please read P41-P54 page.

War and Jar of difference SpringBoot

WAR package structure

/ WEB-INF / classes stored in the class file is compiled

/ WEB-INF / lib storage jar package is dependent on a third party

/ WEB-INF / lib-provided storage is dependent Provided are pom

/ META-INF stored meta information

/ org is stored in the class file associated SpringBoot

Because Sevlet container only under the care of INF-the WEB classes and lib , so the lib-provided will be ignored in favor of the use of Servlet container comes api to achieve, such as Servlet API.

So this may be designed by the WAR WarLauncher guide running and to be in a Servlet container operation.

Is similar to the third-party jar package Jar dependent, and therefore not described, and only if the path difference WarLauncher loads in dependence lib-provided.

Published 27 original articles · won praise 1 · views 903

Guess you like

Origin blog.csdn.net/hu853996234/article/details/103334466