第1篇:SprintBoot的前世今生稍后会奉上,本篇是SpringBoot系列的第2篇文章,在后面系列的教程中,会详细分享SpringBoot生态圈中的各个成员,例如:
springboot01-01
springboot01-01
springboot01-01
老码农设置的信息如下,根据你自己的项目实际情况,大家自行灵活调整
属性 | 输入 | 说明 |
---|---|---|
Name | oldgeek-springboot-examples | 没什么好说的,给项目起个名字 |
Location | 选择你本地路径 | |
GroupId | com.coderoldgeek.springboot.examples | 项目组织唯一的标识符 |
ArtifactId | oldgeek-springboot-examples | 项目的唯一的标识符 |
Version | 1.0-SNAPSHOT | 项目版本号 |
知识点:关于GroupId
groupId一般分为多个段,段之间用【.】分割,通常
groupId不要随便设置,最好和包结构保持一致。
设置好这些信息,直接按【Finish】按钮。
springboot01-01
Step5: 关于目录结构说明
oldgeek-springboot-examples
├─.idea
│─src
│ └─main
│ ├─java
│ ├─resources
│ └─test
└─pom.xml
目录详细说明参照下表:
目录 | 说明 |
---|---|
.idea | 存放工程配置信息(我们用IDEA创建的工程) |
src/main/java | 存放Java代码 |
src/main/resources/ | 存放静态资源或者配置文件,后面会用到 |
test | 存放Test代码,比如你要用TestNG写测试代码 |
pom.xml | Maven的项目配置文件,Maven世界中,必须有该文件,可以用来管理源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等 |
“**关于Maven的详细教程:**老码农正在认真整理,稍后会分享给大家。 ”
springboot01-01
springboot01-01
springboot01-01
oldgeek-springboot-examples
├─.idea
├─springboot-hello
│ └─src
│ └─main
│ ├─java
│ └─resources
│ └─pom.xml
│─src
│ └─main
│ ├─java
│ └─resources
│ └─test
└─pom.xml
目录 | 说明 |
---|---|
.idea | 存放工程配置信息(我们用IDEA创建的工程) |
Springboot-hello/src/main/java | 存放子模块Java代码 |
Springboot-hello/src/main/resources | 存放子模块Resources配置文件,后续系列文章会用到,本篇不涉及 |
Springboot-hello/pom.xml | 子工程的Maven的项目配置文件 |
src/main/java | 存放Java代码 |
src/main/resources/ | 存放静态资源或者配置文件 |
test | 存放Test代码,比如你要用TestNG写测试代码 |
pom.xml | Maven的项目配置文件,Maven世界中,必须有该文件,可以用来管理源代码、配置文件、开发者的信息和角色、问题追踪系统、组织信息、项目授权、项目的url、项目的依赖关系等等 |
“**关于Maven的详细教程:**老码农正在认真整理,稍后会分享给大家。 ”
有些同学会有疑问
父工程
springboot01-01
Step2: 输入如下的Package目录信息
com.oldgeek.springboot.examples.hello
springboot01-01
springboot01-01
springboot01-01
打开子工程: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>
重要知识点:
springboot01-01
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是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
springboot01-01
springboot01-01
springboot01-01
向下图输入:controller,完整的package:com.oldgeek.springboot.examples.hello.controller
springboot01-01
选中controller,点击右键:【New】->【Java Class】,创建控制类
springboot01-01
输入控制类名字:HelloController
springboot01-01
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>未来的日子中,我们一起学编程,一起分享技术";
}
}
代码说明: