关于springboot:全网最细的SpringBoot系列教程不一样的Hello

5次阅读

共计 7015 个字符,预计需要花费 18 分钟才能阅读完成。

对于 SpringBoot

第 1 篇:SprintBoot 的前世今生稍后会奉上,本篇是 SpringBoot 系列的第 2 篇文章,在前面系列的教程中,会具体分享 SpringBoot 生态圈中的各个成员,例如:

  • 最根底的 3 层架构
  • 拜访数据库的 3 种罕用计划
  • MyBatis
  • MyBatis-Plus
  • fluent mybatis
  • NoSQL
  • Redis
  • MongoDB
  • ElasticSearch
  • 音讯队列
  • RabbitMQ
  • Kafka
  • RocketMQ

系列教程特点

  • 支流:分享的都是支流的技术点
  • 具体:十分具体,会交叉各种小知识点
  • 全面:如前文所述,数据拜访层会分享
  • MyBatis
  • MyBatis-Plus
  • fluent mybatis
  • sharding-JDB
  • 深度:会分享研发过程中须要留神的各种知识点,比方日志输入常遇到的坑,相对的干货

创立工程

  • Step1: 启动 idea,我用的是 IDEA Community Edition(不同版本,界面长的会略微有些差异),点击【New Project】按钮,就是下图中的【+】图标

  • Step2:如下图:
  • 抉择 Maven
  • Project SDK:抉择你本地 JDK 版本,我本地装置的是 JDK11,想尝鲜的同学,能够装置 JDK 的最新版本:JDK17
  • 设置好 JDK 后,按【Next】按钮

  • Step3:下图中设置我的项目的信息,点击【Artifact Coordinates】会开展更具体的信息

老码农设置的信息如下,依据你本人的我的项目理论状况,大家自行灵便调整

属性 输出 阐明

知识点:对于 GroupId

groupId 个别分为多个段,段之间用【.】宰割,通常

  • 第一段为域:比方 org(非营利组织)、com(商业组织)、cn(中国)、top(国内通用顶级 域名GTLD(Generictop-level domain top))等
  • 第二段为公司名称:比方我的就设置成 coderoldgeek
  • 第三段为项目名称:我设置成的是【springboot】
  • 第四段能够是子项目名称:我设置成的是【examples】

groupId 不要轻易设置,最好和包构造保持一致。

设置好这些信息,间接按【Finish】按钮。

  • Step4: 我的项目开始创立,可能会须要几秒钟,创立好后,如下图展现

  • Step5: 对于目录构造阐明
oldgeek-springboot-examples
├─.idea     
│─src
│ └─main
│   ├─java
│   ├─resources
│ └─test
└─pom.xml    

目录具体阐明参照下表:

目录 阐明

对于 Maven 的具体教程:老码农正在认真整顿,稍后会分享给大家。

创立子模块

为什么要创立子模块?

  • 本篇文章前面波及的例子,其实不必创立子工程,间接在:src/main/java 编写代码也能够实现。
  • 创立子模块:前面会分享很多内容,所以想依照知识点创立子工程,便于大家依据本人须要去参考。

创立子模块

  • Step1: 右键选父工程:【oldgeek-springboot-examples】间断点击【New】->【Module…】

  • Step2: 同样,抉择【Maven】->【Module SDK】,按【Next】按钮

  • Step3: 下图中,只须要输出 Name 即可,其余的不要批改
  • Name:springboot-hello

  • Step4: 创立子模块,工程的目录构造如下
oldgeek-springboot-examples
├─.idea
├─springboot-hello
│ └─src
│   └─main
│      ├─java
│      └─resources
│   └─pom.xml 
│─src
│ └─main
│   ├─java
│   └─resources
│ └─test
└─pom.xml    
目录 阐明

对于 Maven 的具体教程:老码农正在认真整顿,稍后会分享给大家。

有些同学会有疑难

父工程

  • src/main/java 和 src/main/resources/ 还有用吗?能够删掉吗?
  • 答:如果按模块创立工程,这两个目录能够删掉
  • 父工程:pom.xml 文件能够删掉吗?
  • 答:不能够,这个有大用处,本篇文章临时不开展,咱们徐徐来,莫急

筹备编写第一个能启动工程,激动人心的时刻马上就要来了,持续跟着做

  • Step1: 顺次点击:【springboot-hello】->【src】->【main】->【java】按右键,如下图,顺次点击【New】->【Package】

  • Step2: 输出如下的 Package 目录信息 com.oldgeek.springboot.examples.hello

  • Step3: 创立启动类
Package 门路 类名 阐明

创立过程,如下图,右键选 Package 门路【com.oldgeek.springboot.examples.hello】,顺次【New】->【Java Class】

  • Step4: 输出类名:HelloApplication,回车,启动类就创立好了

  • Step5: 本次咱们要分享的是 SpringBoot,接下来咱们须要配置 SpringBoot 的包

关上子工程:springboot-hello/pom.xml 文件,文件内容如下。

<?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">
    <parent>
        <artifactId>oldgeek-springboot-examples</artifactId>
        <groupId>com.coderoldgeek.springboot.examples</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot-hello</artifactId>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
    </properties>
</project>

咱们增加和 SpringBoot 相干的依赖

  • 增加咱们应用的 SpringBoot 版本,在属性中定义版本信息
    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <!-- spring-boot version -->
        <spring-boot.version>2.5.0</spring-boot.version>
    </properties>
  • 增加 SpringBoot 依赖的包
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
    </dependencies>
  • 子工程残缺的 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">
    <parent>
        <artifactId>oldgeek-springboot-examples</artifactId>
        <groupId>com.coderoldgeek.springboot.examples</groupId>
        <version>1.0-SNAPSHOT</version>
    </parent>
    <modelVersion>4.0.0</modelVersion>

    <artifactId>springboot-hello</artifactId>

    <properties>
        <maven.compiler.source>11</maven.compiler.source>
        <maven.compiler.target>11</maven.compiler.target>
        <!-- spring-boot version -->
        <spring-boot.version>2.5.0</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <version>${spring-boot.version}</version>
        </dependency>
    </dependencies>
</project>

重要知识点:

  • 依赖包的版本尽量在属性中定义定义,不要散落在各子工程中间接硬编码,容易导致版本凌乱
  • 尽量在父工程中定义,前面会讲到,本篇不开展。
  • 不要增加没有应用的依赖,用啥天啥,千万不要整太多垃圾。
  • 上面这张图要留神,批改完 pom.xml 文件配置选项,个别不会主动刷新,须要依照上面步骤刷新,次要是从远端仓库获取 jar 包,放到本地仓库。
  • 如下图:选中工程,按右键,抉择【Maven】->【Reload project】, 按下后,会去 Maven 地方服务器拉取咱们所须要的 jar 包,拉取的工夫看你过后的网速,慢的话,可能须要几分钟。急躁期待就行。

  • Step6: 编写启动类代码,残缺代码如下
package com.oldgeek.springboot.examples.hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class HelloApplication {public static void main(String[] args) {SpringApplication.run(HelloApplication.class, args);
    }
}
代码解释:
  • 启动类增加注解:@SpringBootApplication
  • Main 办法中调用 SpringApplication.run(HelloApplication.class, args);
  • SpringBoot 实例初始化实现后,就会调用 run 办法启动服务。

延长知识点:咱们能够看下 SpringBootApplication 是 SpringBoot 的外围注解,他是一个组合注解,咱们能够查看注解的源代码,截取局部源代码,源代码的分享不是本篇文章重点。

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
  • SpringBootConfiguration:如下源代码,继承自:Configuration,此注解是个配置类,容器启动时会配置初始化参数
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
public @interface SpringBootConfiguration
  • EnableAutoConfiguration:启动 SpringBoot 主动配置机制,依据 pom 包中依赖为以后我的项目主动进行配置,例如,增加了 spring-boot-starter-web 依赖,会主动增加 Tomcat 和 Spring MVC 的依赖,那么 Spring Boot 会对 Tomcat 和 Spring MVC 进行主动配置。
  • ComponentScan:扫描包的配置,留神:他会主动扫描同级目录或者上级包里的 Bean,所以本入口类倡议搁置在 grounpID + arctifactID 组合的包名下,咱们是:com.oldgeek.springboot.examples.hello
  • Step7: 点击启动类:HelloApplication,按右键,抉择【Run ‘HelloApplication.main()’】开始启动咱们的工程。

  • Step8: 若果你能看到如图所示的画面,祝贺你,大概率工程是曾经失常启动了。

  • 小知识点
  • 呈现问题,先看日志,养成看日志习惯
  • 日志从下往上看,容易定位问题
  • Step9: 咱们试着拜访下,关上你的浏览器,输出:http://localhost:8080,点回车,喜剧,上面画面会呈现 没关系,咱们当初只是把服务启动了,还没给他增加任何业务逻辑呢,天然会挂。

增加业务逻辑

  • Step1: 选中 Package:com.oldgeek.springboot.examples.hello,【New】->【Package】

向下图输出:controller,残缺的 package:com.oldgeek.springboot.examples.hello.controller

  • Step2: 增加管制类:HelloController

选中 controller,点击右键:【New】->【Java Class】, 创立管制类

输出管制类名字:HelloController

  • Step3: 编写业务逻辑
package com.oldgeek.springboot.examples.hello.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class HelloController {
    /**
     * Hello World
     */
    @GetMapping("hello")
    @ResponseBody
    public String hello() {return "欢迎您光顾小码匠和老码农的 SpringBoot 家园 <br> 将来的日子中,咱们一起学编程,一起分享技术";}
}

代码阐明:

  • 注解:@Controller:控制器 Controller 负责解决由 DispatcherServlet 散发的申请,它把用户申请的数据通过业务解决层解决之后封装成一个 Model,而后再把该 Model 返回给对应的 View 进行展现。
  • 注解:@GetMapping(“hello”):定义 Request 申请和 Controller 办法之间的映射,此处咱们是用 Get 形式申请
  • 注解:@ResponseBody:把解决的后果间接写入 HTTP response body 中,返回给调用方
  • Step4:
  • 重新启动服务
  • 拜访,再次启动浏览器,地址栏中输出:http://localhost:8080/hello

特地关注

  • pom 中增加新依赖时,要执行 maven 的【Reload Project】操作,不然很可能编译失败
  • groupId 和 artifactId 的命名规定,不要随便命名,专业人士做业余事

关注公众号【小码匠和老码农】播种小码匠和老码农精心筛选的电子书

  • 回复: java, 取得 Java 基础教程
  • 回复: jvm, 取得 JVM 材料
  • 回复: springboot, 取得 SpringBoot 材料
  • 回复: springcloud, 取得 springcloud 材料
  • 回复: python, 取得 Python 材料
  • 回复: 算法, 取得算法材料
正文完
 0