0-1 from manual application and development SpringBoot explain in detail (version 2.X) - the next day

Disclaimer: if reproduced - please add the next micro-channel can inform 457,556,886 knowledge is shared https://blog.csdn.net/wolf_love666/article/details/90380967

1, system requirements (Spring Boot 2.1.5.RELEASE):

  • Java environment: jdk8 + also adapted jdk11
  • Spring框架:Spring Framework 5.1.7.RELEASE
  • Build tools:Here Insert Picture Description
  • Servlet container (SpringBoot built container version):
    Here Insert Picture Description
    note:You can also be deployed to other Servlet3.1 + own version of the above container

2, installation SpringBoot application:

To make sure your java environment prior to installation is jdk8 +, you can get the current version of java cmd window system through the implementation of java -version, if not already installed, you need to install under java environment. You can see here the novice Linux environment jdk installed , Windows environment to install jdk
Here Insert Picture Description
If you are new to development, or experience SpringBoot you can refer to the article command line SpringBoot , otherwise read on.

2.1) java development installation instructions
include the appropriate springboot in the class path -. * Jar files, you can use the same libraries as java springboot. Since no special integration, so you can run it with any editor.
Although you can copy springboot jar package, but the official website is recommended gradle or maven to build management.

  • 2.1.1) maven installation
    springboot requirements maven version 3.3+, if you do not have maven install, maven you need to go to the official website to download and install. If installed, then you can view your maven version by cmd.
    Here Insert Picture Description
    If not, you need to install maven configure the environment, but only if you configure the java environment.
    How to configure maven environment variables?
    Computer - Right Properties - Advanced System Configuration - Environment Variables ->
    Configure maven_home = maven installation directory;
    configure the path variables:% MAVEN_HOME% /
    bin; ,,,,,,,,,,,,,,,,,
  • SpringBoot dependence can be used groupId positioning, it is clear that other documents maven integration can rely on "Starters" from the spring-boot-starter-parent project and the way to get a statement. springboot also offers an optional plug-in performs maven executable jar package. as follows:
  • Spring Boot Maven plug-ins provide support in Maven Spring guide that allows you to pack executable jar or war archives and "in situ" to run the application. To use it, you must use the Maven 3.2 (or later), the specific information is available here
  • maven pom file contains plug-ins, other plug-reference
<?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>
	<!-- ... -->
	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
				<version>2.1.5.RELEASE</version>
				<executions>
					<execution>
						<goals>
							<goal>repackage</goal>
						</goals>
					</execution>
				</executions>
			</plugin>
		</plugins>
	</build>
</project>

前面的配置重新打包了在Maven生命周期的包阶段构建的jar或war。
下面的示例显示了重新打包的jar和目标目录中的原始jar
$ mvn package
$ ls target/*.jar
target/myproject-1.0.0.jar target/myproject-1.0.0.jar.original
如果不包含<execution/>配置(如前面的示例所示),
则可以单独运行插件(但只有在同时使用包目标的情况下),如下面的示例所示
$ mvn package spring-boot:repackage
$ ls target/*.jar
target/myproject-1.0.0.jar target/myproject-1.0.0.jar.original
如果使用里程碑或快照版本,还需要添加适当的pluginRepository元素,如下面的清单所示
<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>
  • And packaged into executable jar package war package
    once spring-boot-maven-plugin is incorporated into the pom. In xml, it automatically tries to rewrite an archive, using spring-boot: repackage goal to make them enforceable. You should use elements project normally packed configured to construct a jar or WAR (as appropriate), as shown in the following example
可执行jar包
<?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">
	<!-- ... -->
	<packaging>jar</packaging>
	<!-- ... -->
</project>
您想要启动的main类可以通过使用配置选项来指定,
也可以通过以通常的方式向清单添加main - class属性来指定。
如果您没有指定一个main类,
插件将搜索一个带有公共静态void main(String[] args)方法的类,要构建和运行项目构件,您可以输入以下内容
$ mvn package
$ java -jar target/mymodule-0.0.1-SNAPSHOT.jar
要构建一个既可执行又可部署到外部容器中的war文件,
需要将嵌入的容器依赖项标记为“provided”,如下例所示:
<?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">
	<!-- ... -->
	<packaging>war</packaging>
	<!-- ... -->
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-tomcat</artifactId>
			<scope>provided</scope>
		</dependency>
		<!-- ... -->
	</dependencies>
</project>
<?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.1.5.RELEASE</version>
	</parent>

	<!-- 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>
  • 2.1.2)Gradle的安装
    springboot适配的gradle版本是4.4以上,如果没有安装则需要去官网下载并安装,springboot的依赖是使用group来定位的,其他则类似maven,也官方推荐需要个gradle的插件来管理可执行jar包。具体信息可参考

2.2)安装SpringBoot命令行接口
Spring Boot CLI(命令行接口)是一个命令行工具,您可以使用它来快速原型化Spring。它允许您运行Groovy脚本,这意味着您拥有一个熟悉的类似java的语法,而没有那么多样板代码
而且不需要使用CLI来处理Spring Boot,但它绝对是启动Spring应用程序的最快方法
这里省略掉如需要安装可以参考这里

2.3)SpringBoot的版本升级
如果你是从以前的版本进行升级,需要查看升级的内容清单,以及查看发布的重要信息

2.3.1)当升级一个新的特点时候,一些属性可能已经被重命名护着去掉了。SpringBoot强大的优点就是提供一个方法来分析你的应用程序的环境并启动时打印诊断信息,还可以在运行时临时迁移属性。想要使用这个特性的话需要引入下面的依赖:
迁移完成后,请确保将此模块从项目的依赖项中删除

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-properties-migrator</artifactId>
	<scope>runtime</scope>
</dependency>

晚添加到环境中的属性,例如使用@PropertySource修饰的,将不被考虑
要升级现有的CLI安装,
请使用适当的包管理器命令(例如brew upgrade),
或者,如果手动安装CLI,请遵循标准说明,
记住更新PATH环境变量以删除任何旧的引用

3、开发你的第一个SpringBoot应用

本节描述如何开发一个简单的“Hello World!”web应用程序,突出了Spring Boot的一些关键特性。我们使用Maven来构建这个项目,因为大多数ide都支持它。
如果想要快速进入开发那么可以点击这里进入上篇文章生成自己的应用,快速编码文档,可以从初始化文档获取具体信息

切记开发之前确保环境版本
Here Insert Picture Description
3.1)创建POM文件(如下示例)

<?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.xiaochengxinyizhan.demo</groupId>
    <artifactId>springboot2.X</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--构建本地springboot应用功能  -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>

</project>

上面的pom文件应该提供了一个可工作的构建。您可以通过运行mvn包来测试它,警告:“jar will be empty - no content was marked for inclusion!” 可以忽略。
现在,将项目导入IDE就可以了(大多数现代Java IDE都包含对Maven的内置支持)。为了简单起见,我们在本例中继续使用纯文本编辑器

3.2)添加类路径依赖
找到刚才添加pom文件的项目,在目录下CMD框输入mvn dependency:tree,如果想要查看完整的目录可以mvn dependency:tree -Dverbose,如果是idea中使用的话可以通过指定某个artifactId如图:
Here Insert Picture Description
Here Insert Picture Description
这里只有被maven构建后的jar包信息。Here Insert Picture Description
由于我这里是上篇文章生成好的一个项目,所以有部分依赖。其他module是自己新加的。可以再看下如下图:
Here Insert Picture Description
3.3)现在假设你想要开发一个springboot的web项目,则可以引入之前我们说过的springboot下好多启动器中的starter-web.
Here Insert Picture Description
Here Insert Picture Description

修改后的pom文件:
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.xiaochengxinyizhan.demo</groupId>
    <artifactId>springboot2.X</artifactId>
    <version>1.0-SNAPSHOT</version>

    <!--构建本地springboot应用功能  -->
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
    </parent>
    <!--构建springboot的web应用功能-->
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
</project>
package com.xiaochengxinyizhan.demo.web;

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;

/**
 * springboot的web应用例子restful风格
 *
 * @author 百度谷歌可搜{wolf_love666}关注我呦, 公众号:小诚信驿站
 * @create 2019-05-21 11:14
 **/
@RestController
@EnableAutoConfiguration
public class Example {

    @RequestMapping("/")
    String home() {
        return "Hello World!";
    }

    public static void main(String[] args) {
        SpringApplication.run(Example.class, args);
    }

}

可以通过idea启动main方法Here Insert Picture Description
如果我这里不想通过idea启动可以通过如下命令:mvn spring-boot:run
信息都是一样的和上图。通过浏览器也可以访问直接返回Hello World!,如果想要退出直接ctrl+c就可以了
Here Insert Picture Description
Here Insert Picture Description
从上面的代码中我们可以知道我们只是配置了2个jar包,一个Example例子,但是如上图却发生了很多事情,实际上springboot的优点就是如此正如上篇文章所述:独立的spring应用功能,不需要xml配置,内嵌的服务器,可插拔式的各种starter启动器,以及maven管理的各种第三方依赖的jar包。
接下来逐步介绍重要的部分。

3.3.1)刚才提到的@RestController 和@RequestMapping注解。
@RestController这个例子中的使用主要是作为web服务@Controller等同作用。当进来web请求的时候,会通过该注解标识的类进行处理对应的请求。
@RequestMapping注释提供了“路由”信息。它告诉Spring,任何带有/ path的HTTP请求都应该映射到home方法。@RestController注释告诉Spring将结果字符串直接呈现回调用者
@RestController和@RequestMapping注释是Spring MVC注释。(它们不是特定于Spring Boot的。)了解更多mvc注解可以点击这里

3.3.2)@EnableAutoConfiguration注解
这个注释会告诉Spring Boot根据添加的jar依赖项“猜测”该如何配置Spring。由于Spring -boot-starter-web添加了Tomcat和Spring MVC,因此自动配置假定您正在开发一个web应用程序,并相应地设置Spring。而这个也是springboot吸引人的特点,可以在我上篇文章看到。
注意:启动器starters和自动配置的关系
这两个概念并不直接相关。我们可以在启动程序之外自由地选择jar依赖项。Spring Boot仍然会自动配置应用程序,根据你添加的jar依赖项,并不会由于你添加了而不能自动配置,如果不想自动配置,可以通过在profile文件中禁止掉。如下例子:

如果你发现使用@EnableAutoConfiguration的结果不是你期望的,你可以手工配置
exclude来排除某些配置,也可以在properties中设置排除配置
properties配置排除:
spring.autoconfigure.exclude=xxx

注解中配置排除:
import org.springframework.boot.autoconfigure.*;
import org.springframework.boot.autoconfigure.jdbc.*;
import org.springframework.context.annotation.*;

@Configuration
@EnableAutoConfiguration(exclude={DataSourceAutoConfiguration.class})
public class MyConfiguration {
}

3.3.3)main方法
应用程序的最后一部分是主方法。这只是应用程序入口点遵循Java约定的标准方法。我们的主方法通过调用run将委托给Spring Boot的SpringApplication类。SpringApplication引导我们的应用程序,启动Spring,而Spring又启动自动配置的Tomcat web服务器。我们需要将Example.class作为参数传递给run方法,以告诉Spring application哪个是主Spring组件。还传递args数组来公开任何命令行参数。这样就完成了。

4)创建一个可执行的jar包
最后,我们maven构建的时候创建了一个完全自包含的可执行jar文件,可以在生产环境中运行。可执行jar(有时称为“fat jar”)是包含编译类以及代码运行所需的所有jar依赖项的归档文件
比如之前看的这个图Here Insert Picture Description
可执行jar和java之间的关系
Java没有提供加载嵌套jar文件(jar文件本身包含在一个jar中)的标准方法。如果想要分发一个自包含的应用程序,那么这可能会有问题。
为了解决这个问题,许多开发人员使用“uber”jar。uber jar将应用程序的所有依赖项中的所有类打包到一个单独的归档文件中。这种方法的问题是,很难看到应用程序中有哪些库。如果在多个jar中使用相同的文件名(但是内容不同),也会有问题
Spring Boot采用了一种不同的方法,允许您直接嵌套jar,可以参考这篇文章
如果想要创建一个可执行jar在我们的应用里面,那么我们需要添加这个依赖:

<build>
	<plugins>
		<plugin>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-maven-plugin</artifactId>
		</plugin>
	</plugins>
</build>

spring-boot-starter-parent POM contains configuration to bind repackage target. If you do not use the parent POM, you need to own statement this configuration. You can refer to this article
Here Insert Picture Description
execute mvn package
Here Insert Picture Description
this time we see the emergence of a directory on the jar package cmd chart
you can extract the jar files jar tvf target / springboot2.X-1.0- SNAPSHOT.jar
another with the suffix orginal he is the original jar file jar file Maven created before repackaging Spring Boot Here Insert Picture Description
now because it is an executable jar file so we can directly execute springboot the following command
java -jar target / springboot2.X-1.0- SNAPSHOT.jar
Here Insert Picture Description
other Ibid browser can be accessed directly, ctrl + c to exit. Not repeat.

summary:

This article explains how springboot simplify the configuration set up a web service application. This also explains the role and significance of each step, and how to manually set up a springboot applications, as well as providing other people an executable jar file.

The above code can click here to download

Guess you like

Origin blog.csdn.net/wolf_love666/article/details/90380967