- 首先配置maven,找到maven目录下conf里面的settings.xml添加一些镜像
- 新建maven项目,在pom.xml文件中添加依赖,保存,然后会联网下载包
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15<!--springboot 的版本仲裁中心 -->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
<relativePath /> <!-- lookup parent from repository ,从仓库中寻找parent-->
</parent>
<!-- 启动器,Starter里面包含各种功能,要用那个就调用哪个 -->
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies> - 新建主配置类
1 |
|
注意:由于springbootApplication中有@EnableAutoConfiguration的@Import(AutoConfigurationImportSelector.class)
方法,主要将主配置类(就是@SpringBootApplication修饰的类)所在包及下面所有子包里面的组件扫描到spring容器。
这就要求我们把主配置类放到外面,否则容器会找不到controller注解之类的
- 新建controller
1 |
|
@ResponseBody和@Controller可以合并,用@RestController
5.运行配置类中的main方法
在浏览器中输入 localhost:8080/hello,页面会显示 hello world,则成功运行