Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit df3a248

Browse files
[feat]文件位置移动
1 parent f7a5770 commit df3a248

File tree

2 files changed

+27
-26
lines changed

2 files changed

+27
-26
lines changed
File renamed without changes.

‎docs/start/springboot-hello-world.md

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
### 新建 Spring Boot 项目常用的两种方式
1+
示例项目源代码地址:[https://github.com/CodingDocs/springboot-guide/tree/master/source-code/hello-world](https://github.com/CodingDocs/springboot-guide/tree/master/source-code/hello-world)
22

3-
你可以通过 https://start.spring.io/ 这个网站来生成一个 Spring Boot 的项目。
3+
## 新建 Spring Boot 项目
44

5-
![start.spring.io](https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-7/spring.start.io.png)
5+
**1. 你可以通过 [https://start.spring.io/](https://start.spring.io/) 这个网站来生成一个 Spring Boot 的项目。**
6+
7+
![start.spring.io](https://img-blog.csdnimg.cn/img_convert/69f88b648b5f94fe8fa509fc4d987057.png)
68

79
注意勾选上 Spring Web 这个模块,这是我们所必需的一个依赖。当所有选项都勾选完毕之后,点击下方的按钮 Generate 下载这个 Spring Boot 的项目。下载完成并解压之后,我们直接使用 IDEA 打开即可。
810

9-
当然你也可以直接通过 IDEA 来生成一个 Spring Boot 的项目,具体方法和上面类似:`File->New->Project->Spring Initializr`
11+
另外,阿里也有一个类似的网站 [https://start.aliyun.com/bootstrap.html](https://start.aliyun.com/bootstrap.html) ,功能甚至还要更强大点,支持生成特定应用架构的项目模板!
1012

11-
### Spring Boot 项目结构分析
13+
![](https://img-blog.csdnimg.cn/20210421220259726.png)
1214

13-
成功打开项目之后,项目长下面这个样子:
15+
**2.你也可以直接通过 IDEA 来生成一个 Spring Boot 的项目,具体方法和上面类似:`File->New->Project->Spring Initializr`**
1416

15-
<imgsrc="https://my-blog-to-use.oss-cn-beijing.aliyuncs.com/2019-7/springboot-hellowold-structure.png"style="zoom:50%;" />
17+
## Spring Boot 项目结构分析
1618

19+
成功打开项目之后,项目长下面这个样子:
1720

21+
![](https://img-blog.csdnimg.cn/img_convert/1f1a3ee40347e941891988cbb72ceee1.png#pic_center)
1822

19-
Application为后缀名的 Java 类一般就是 Spring Boot 的启动类,比如本项目的启动项目就是`HelloWorldApplication` 。我们直接像运行普通 Java 程序一样运行它,由于 Spring Boot 本身就嵌入servlet容器的缘故,我们的 web 项目就运行成功了, 非常方便。
23+
Application 为后缀名的 Java 类一般就是 Spring Boot 的启动类,比如本项目的启动项目就是`HelloWorldApplication` 。我们直接像运行普通 Java 程序一样运行它,由于 Spring Boot 本身就嵌入 servlet 容器的缘故,我们的 web 项目就运行成功了, 非常方便。
2024

2125
需要注意的一点是 **Spring Boot 的启动类是需要最外层的,不然可能导致一些类无法被正确扫描到,导致一些奇怪的问题。** 一般情况下 Spring Boot 项目结构类似下面这样
2226

@@ -35,19 +39,19 @@ com
3539
|
3640
+- controller
3741
| +- CustomerController.java
38-
|
42+
|
3943
+- config
4044
| +- swagerConfig.java
4145
|
4246
```
4347

44-
1. `Application.java`是项目的启动类
45-
2. domain目录主要用于实体(Entity)与数据访问层(Repository)
48+
1. `Application.java`是项目的启动类
49+
2. domain 目录主要用于实体(Entity)与数据访问层(Repository)
4650
3. service 层主要是业务类代码
4751
4. controller 负责页面访问控制
4852
5. config 目录主要放一些配置类
4953

50-
### @SpringBootApplication 注解分析
54+
## @SpringBootApplication 注解分析
5155

5256
`HelloWorldApplication`
5357

@@ -91,21 +95,21 @@ public @interface SpringBootConfiguration {
9195
}
9296
```
9397

94-
可以看出大概可以把 `@SpringBootApplication`看作是 `@Configuration``@EnableAutoConfiguration``@ComponentScan` 注解的集合。根据 SpringBoot官网,这三个注解的作用分别是:
98+
可以看出大概可以把 `@SpringBootApplication`看作是 `@Configuration``@EnableAutoConfiguration``@ComponentScan` 注解的集合。根据 SpringBoot 官网,这三个注解的作用分别是:
9599

96100
- `@EnableAutoConfiguration`:启用 SpringBoot 的自动配置机制
97-
- `@ComponentScan`: 扫描被`@Component` (`@Service`,`@Controller`)注解的bean,注解默认会扫描该类所在的包下所有的类。
98-
- `@Configuration`:允许在上下文中注册额外的bean或导入其他配置类
101+
- `@ComponentScan`: 扫描被`@Component` (`@Service`,`@Controller`)注解的 bean,注解默认会扫描该类所在的包下所有的类。
102+
- `@Configuration`:允许在上下文中注册额外的 bean 或导入其他配置类
99103

100-
所以说 `@SpringBootApplication`就是几个重要的注解的组合,为什么要有它?当然是为了省事,避免了我们每次开发 Spring Boot 项目都要写一些必备的注解。这一点在我们平时开发中也经常用到,比如我们通常会提一个测试基类,这个基类包含了我们写测试所需要的一些基本的注解和一些依赖。
104+
所以说 `@SpringBootApplication`就是几个重要的注解的组合,为什么要有它?当然是为了省事,避免了我们每次开发 Spring Boot 项目都要写一些必备的注解。这一点在我们平时开发中也经常用到,比如我们通常会提一个测试基类,这个基类包含了我们写测试所需要的一些基本的注解和一些依赖。
101105

102-
### 新建一个 Controller
106+
## 新建一个 Controller
103107

104108
上面说了这么多,我们现在正式开始写 Spring Boot 版的 "Hello World" 吧。
105109

106-
新建一个 controller 文件夹,并在这个文件夹下新建一个名字叫做 `HelloWorldController` 的类。
110+
新建一个 `controller` 文件夹,并在这个文件夹下新建一个名字叫做 `HelloWorldController` 的类。
107111

108-
`@RestController`是Spring 4 之后新加的注解,如果在Spring4之前开发 RESTful Web服务的话,你需要使用`@Controller` 并结合`@ResponseBody`注解,也就是说`@Controller` +`@ResponseBody`= `@RestController`。对于这两个注解,我在基础篇单独抽了一篇文章来介绍。
112+
`@RestController`是 Spring 4 之后新加的注解,如果在 Spring4 之前开发 RESTful Web 服务的话,你需要使用`@Controller` 并结合`@ResponseBody`注解,也就是说`@Controller` +`@ResponseBody`= `@RestController`。对于这两个注解,我在基础篇单独抽了一篇文章来介绍。
109113

110114
`com.example.helloworld.controller`
111115

@@ -134,9 +138,9 @@ public class HelloWorldController {
134138
server.port=8333
135139
```
136140

137-
### 大功告成,运行项目
141+
## 大功告成,运行项目
138142

139-
运行 `HelloWorldApplication` ,运行成功控制台会打印出一些消息,不要忽略这些消息,它里面会有一些比较有用的信息。
143+
运行 `HelloWorldApplication` ,运行成功控制台会打印出一些消息,不要忽略这些消息,它里面会有一些比较有用的信息。
140144

141145
```
142146
2019年10月03日 09:24:47.757 INFO 26326 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8333 (http) with context path ''
@@ -146,9 +150,6 @@ server.port=8333
146150

147151
浏览器 http://localhost:8333/test/hello 如果你可以在页面正确得到 "Hello World" 的话,说明你已经成功完成了这部分内容。
148152

149-
### 总结
150-
151-
通过本文我们学到了如何新建 Spring Boot 项目、SpringBoot 项目常见的项目结构分析、`@SpringBootApplication` 注解分析,最后实现了 Spring Boot 版的 "Hello World"。
152-
153-
代码地址: [https://github.com/Snailclimb/springboot-guide/tree/master/source-code/start/hello-world](https://github.com/Snailclimb/springboot-guide/tree/master/source-code/start/hello-world)(建议自己手敲一遍!!!)
153+
## 总结
154154

155+
通过本文我们学到了如何新建 Spring Boot 项目、SpringBoot 项目常见的项目结构分析、`@SpringBootApplication` 注解分析,最后实现了 Spring Boot 版的 "Hello World"。

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /