全网最细的SpringBoot系列教程-不一样的Hello

2021-10-28  本文已影响0人  小码匠和老码农

关于SpringBoot

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

系列教程特点

创建工程

img img 0 (1).png

老码农设置的信息如下,根据你自己的项目实际情况,大家自行灵活调整

属性 输入 说明

知识点:关于GroupId

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

groupId不要随便设置,最好和包结构保持一致。

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

img img

oldgeek-springboot-examples
├─.idea
│─src
│ └─main
│ ├─java
│ ├─resources
│ └─test
└─pom.xml

目录详细说明参照下表:

目录 说明

关于Maven的详细教程:老码农正在认真整理,稍后会分享给大家。

创建子模块

为什么要创建子模块?

创建子模块

img img img

oldgeek-springboot-examples
├─.idea
├─springboot-hello
│ └─src
│ └─main
│ ├─java
│ └─resources
│ └─pom.xml
│─src
│ └─main
│ ├─java
│ └─resources
│ └─test
└─pom.xml

关于Maven的详细教程:老码农正在认真整理,稍后会分享给大家。

有些同学会有疑问

父工程

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

img

[图片上传失败...(image-471b33-1635397700065)]

Package路径 类名 说明

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

img 0 (2).png

打开子工程: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相关的依赖

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

<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>

<?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>

重要知识点:

img

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 {

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
@Indexed
public @interface SpringBootConfiguration

img img img

添加业务逻辑

img

向下图输入:controller,完整的package:com.oldgeek.springboot.examples.hello.controller

0 (3).png img

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

img

输入控制类名字:HelloController

0 (4).png

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家园
未来的日子中,我们一起学编程,一起分享技术";
}
}
代码说明:

0 (5).png

特别关注

关注公众号【小码匠和老码农】收获小码匠和老码农精心挑选的电子书

上一篇 下一篇

猜你喜欢

热点阅读