1. 构建第一个SpringBoot工程

it2022-05-09  25

1.File -  New - Module

 

2.选项的是Spring Initializr(官方的构建插件,需要联网) ,一定要选择jdk

 

3.填写项目基本信息

Group:组织ID,一般分为多个段,这里我只说两段,第一段为域,第二段为公司名称。域又分为org、com、cn等等,其中org为非营利组织,com为商业组织。如阿里、淘宝(com.alibaba/com.taobao)Artifact:唯一标识符,一般是项目名称

 

 

4.选择包

* Spring Initializr为我们提供了很多的选项,不同的选项有不同的作用,在初期我们只需要依赖Web -> Web就可以了,选择好依赖包之后点击Next->Finish *

 

 

5.输入项目名

 

 

6.目录结构如下

1 - src 2 -main 3 -java 4 -package 5 #主函数,启动类,运行它如果运行了 Tomcat、Jetty、Undertow 等容器 6 -SpringbootApplication 7 -resouces 8 #存放静态资源 js/css/images 等 9 - statics 10 #存放 html 模板文件 11 - templates 12 #主要的配置文件,SpringBoot启动时候会自动加载application.yml/application.properties 13 - application.yml 14 #测试文件存放目录 15 -test 16 # pom.xml 文件是Maven构建的基础,里面包含了我们所依赖JAR和Plugin的信息 17 - pom

 

7.pom.xml 文件的 依赖

 因为使用了Spring Initializr插件,所以如下的配置都不需要我们自己去写啦,需要注意的是版本要选择RELEASE,稳定版本BUG少

1 <?xml version="1.0" encoding="UTF-8"?> 2 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 3 xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4 <modelVersion>4.0.0</modelVersion> 5 6 <groupId>cn.kgc</groupId> 7 <artifactId>springboot1</artifactId> 8 <version>0.0.1-SNAPSHOT</version> 9 <packaging>jar</packaging> 10 11 <name>springboot1</name> 12 <description>我用的第一个maven工程</description> 13 14 <!--版本采用的是最新的 2.0.1.RELEASE 15 TODO 开发中请记得版本一定要选择 RELEASE ,因为是稳定版本且BUG少--> 16 <parent> 17 <groupId>org.springframework.boot</groupId> 18 <artifactId>spring-boot-starter-parent</artifactId> 19 <version>2.0.5.RELEASE</version> 20 <relativePath/> <!-- lookup parent from repository --> 21 </parent> 22 23 <properties> 24 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 25 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 26 <java.version>1.8</java.version> 27 </properties> 28 29 <dependencies> 30 31 <!-- 默认就内嵌了Tomcat 容器,如需要更换容器也极其简单--> 32 <dependency> 33 <groupId>org.springframework.boot</groupId> 34 <artifactId>spring-boot-starter-web</artifactId> 35 </dependency> 36 <!-- 测试包,当我们使用 mvn package 的时候该包并不会被打入,因为它的生命周期只在 test 之内--> 37 <dependency> 38 <groupId>org.springframework.boot</groupId> 39 <artifactId>spring-boot-starter-test</artifactId> 40 <scope>test</scope> 41 </dependency> 42 </dependencies> 43 44 <build> 45 <plugins> 46 <!-- 编译插件 --> 47 <plugin> 48 <groupId>org.springframework.boot</groupId> 49 <artifactId>spring-boot-maven-plugin</artifactId> 50 </plugin> 51 </plugins> 52 </build> 53 54 55 </project>

 

8.主函数入口

注意事项:一个项目中切记不要出现多个main函数,否在在打包的时候spring-boot-maven-plugin将找不到主函数(主动指定打包主函数入口除外…)

 

1 package cn.kgc; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 import org.springframework.web.bind.annotation.GetMapping; 6 import org.springframework.web.bind.annotation.RestController; 7 8 /*我的第一个springboot程序 9 其中 @RestController 等同于 (@Controller 与 @ResponseBody)*/ 10 @RestController 11 @SpringBootApplication 12 public class Springboot1Application { 13 public static void main(String[] args) { 14 SpringApplication.run(Springboot1Application.class, args); 15 } 16 @GetMapping("/demo1") 17 public String demo1(){ 18 return "Hello World!"; 19 } 20 }

 

9.初窥配置文件

从启动日志中可以发现,SpringBoot默认的端口是 8080 ,那么如果端口被占用了怎么办呢?不要慌,问题不大,配置文件分分钟解决你的困扰…

修改application.properties属性文件

1 # 默认的 8080 我们将它改成 9090 2 server.port=9090 3 # 未定义上下文路径之前 地址是 http://localhost:8080 定义了后 http://localhost:9090 你能在tomcat做的事情,配置文件都可以 4 server.servlet.context-path=/springboot1

 

10.启动项目,查看控制台输出

SpringBoot启动的时候我们可以看到控制台如下内容可以在在resouces目录下添加指定命名的的文件类型有*.txt、*.jpg、*.gif、*.jpeg等等,

 

 

 

本文原创作者:唐亚峰 本文原创链接:http://blog.battcn.com/2018/04/22/springboot/v2-config-properties/ 版权声明:本博客所有文章除特别声明外,均采用CC BY-NC-SA 3.0许可协议。转载请注明出处!

 

 

 

 

 

转载于:https://www.cnblogs.com/holly8/p/SpringBoot.html

相关资源:数据结构—成绩单生成器

最新回复(0)