Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)
- Creating the 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.</modelVersion> <groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1..RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
</project>- 通过POM的dependencies 实现 Adding Classpath Dependencies
关于
spring-boot-starter-parent:You should need to specify only the Spring Boot version number on this dependency. If you import additional starters, you can safely omit the version number.
- 使用
mvn dependency:tree查看project dependencies - 编写java代码
- src/main/java/Example.java
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) throws Exception{
SpringApplication.run(Example.class,args);
}
}@RestControllerand@RequestMappingannotations are Spring MVC annotations@EnableAutoConfiguration. This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Sincespring-boot-starter-webadded Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.Starters and Auto-configuration
Auto-configuration is designed to work well with “Starters”, but the two concepts are not directly tied. You are free to pick and choose jar dependencies outside of the starters. Spring Boot still does its best to auto-configure your application.
- Running the Example
- mvn spring-boot:run
- Creating an Executable Jar
- To create an executable jar, we need to add the
spring-boot-maven-pluginto ourpom.xml. To do so, insert the following lines just below thedependenciessection: - Spring Boot includes a Maven plugin that can package the project as an executable jar. Add the plugin to your
<plugins>section if you want to use it, as shown in the following example: <build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build> <?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.</modelVersion> <groupId>com.example</groupId>
<artifactId>myproject</artifactId>
<version>0.0.-SNAPSHOT</version> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1..RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>- mvn package
- java -jar target/myproject-0.0.1-SNAPSHOT.jar
- To create an executable jar, we need to add the
- 参考:https://docs.spring.io/spring-boot/docs/2.1.1.RELEASE/reference/htmlsingle/#getting-started-first-application
Spring boot 官网学习笔记 - 开发第一个Spring boot web应用程序(使用mvn执行、使用jar执行)的更多相关文章
- Spring boot 官网学习笔记 - Spring Boot 属性配置和使用(转)-application.properties
Spring Boot uses a very particular PropertySource order that is designed to allow sensible overridin ...
- Spring boot 官网学习笔记 - Auto-configuration(@SpringBootApplication、@EnableAutoConfiguration、@Configuration)
Spring Boot auto-configuration attempts to automatically configure your Spring application based on ...
- Spring boot 官网学习笔记 - logging
commons-logging和slf4j是java中的日志门面,即它们提供了一套通用的接口,具体的实现可以由开发者自由选择.log4j和logback则是具体的日志实现方案. 比较常用的搭配是com ...
- Spring boot 官网学习笔记 - Spring DevTools 介绍
想要使用devtools支持,只需使用dependencies将模块依赖关系添加到你的构建中 运行打包的应用程序时,开发人员工具会自动禁用.如果你通过 java -jar或者其他特殊的类加载器进行启动 ...
- Spring boot 官网学习笔记 - Using Spring Boot without the Parent POM,但是还要使用Parent POM提供的便利
If you do not want to use the spring-boot-starter-parent, you can still keep the benefit of the depe ...
- Spring boot 官网学习笔记 - Spring Boot CLI 入门案例
安装CLI https://repo.spring.io/release/org/springframework/boot/spring-boot-cli/2.1.1.RELEASE/spring-b ...
- Spring boot 官网学习笔记 - Configuration Class(@import)
推荐使用 Java-based configuration ,也可以使用xml we generally recommend that your primary source be a single ...
- React官网学习笔记
欢迎指导与讨论 : ) 前言 本文主要是笔者在React英文官网学习时整理的笔记.由于笔者水平有限,如有错误恳请指出 O(∩_∩)O 一 .Tutoial 篇 1 . React的组件类名的首字母必须 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:第一个Spring程序
1. 创建项目 在 MyEclipse 中创建 Web 项目 springDemo01,将 Spring 框架所需的 JAR 包复制到项目的 lib 目录中,并将添加到类路径下,添加后的项目如图 2. ...
随机推荐
- unity_小功能实现(敌人追踪主角)
1.敌人发现主角有两种形式: a.看见主角(主角出现在敌人的视野之内) b.听见主角(听见主角走路声或者是跑步声) a:看(see) 首先判断主角是否在敌人视野角度内,那么我们只需要判断B<0. ...
- Codeforces 976E
题意略. 思路: 容易知道那a次倍增放在同一个怪身上是最优的,其余的怪我们只需要取hp值和damage值中间最大的那个就好了(在b值的限制下). 然而我们并不知道把那a次倍增放在哪个怪身上最好,那么我 ...
- java.lang.Thread类详解
java.lang.Thread类详解 一.前言 位于java.lang包下的Thread类是非常重要的线程类,它实现了Runnable接口,今天我们来学习一下Thread类,在学习Thread类之前 ...
- UGUI的图集处理方式-SpriteAtlas的前世今生
最糟糕的是人们在生活中经常受到错误志向的阻碍而不自知,真到摆脱了那些阻碍时才能明白过来. —— 歌德 说到UGUI的图集初学者可能觉得没什么难度,包括我刚开始接触的时候也是,甚至你在开发的时候只需要把 ...
- HTML 画布(摘自菜鸟教程)
颜色.样式和阴影 属性 描述 fillStyle 设置或返回用于填充绘画的颜色.渐变或模式. strokeStyle 设置或返回用于笔触的颜色.渐变或模式. shadowColor 设置或返回用于阴影 ...
- Docker容器技术的核心
容器技术的核心 所谓容器,其实是由Linux Namespace.Linux Cgroups和rootfs三种技术构建出来的进程的隔离环境 对于Docker项目来说,其实最核心就是为待创建的用户进程: ...
- 知识图谱推理与实践 (2) -- 基于jena实现规则推理
本章,介绍 基于jena的规则引擎实现推理,并通过两个例子介绍如何coding实现. 规则引擎概述 jena包含了一个通用的规则推理机,可以在RDFS和OWL推理机使用,也可以单独使用. 推理机支持在 ...
- AOJ-2249-Road Construction-dijkstra-最小花费
Road Construction 题意:在一个无向图中,每条边上有建设的花费和路径的长度,要求求得,在保持每个点到1号点最小距离不变的情况下,求最小的总花费: 思路:用dijkstra 找出每个点的 ...
- dp递推 hdu1978
How many ways Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- hadoop之数据倾斜
数据倾斜介绍 在做Shuffle阶段的优化过程中,遇到了数据倾斜的问题,造成了对一些情况下优化效果不明显.主要是因为在Job完成后的所得到的Counters是整个Job的总和,优化是基于这些Count ...
