# springboot实验一
**Repository Path**: chainsawman/springboot-experiment-1
## Basic Information
- **Project Name**: springboot实验一
- **Description**: springboot
- **Primary Language**: Java
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-10-11
- **Last Updated**: 2024-12-01
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
| 实验名称: | 使用Spring Boot构建应用程序 | 实验序号: | 一 |
|-------|---------------------|-------|--------------|
| 姓名: | 陈志聪 | 学号: | 201841413203 |
| 班级: | 18网工2班 | 实验地点: | 线上 |
| 实验日期: | 2020/10/4 | 指导老师: | 黎志雄 |
| 成绩: | | 同组同学: | 无 |
- **一、 实验目的**
1. 掌握使用IntelliJ IDEA创建Spring Boot应用程序的方法;
1. 了解spring-boot-starter-parent的配置内容;
1. 掌握如何利用Starter扩展Spring Boot应用程序的功能;
1. 掌握如何配置Starter;
1. 掌握如何通过属性文件定制Spring Boot应用程序的初始化参数;
1. 掌握使用Spring Boot编写简单的单元测试;
1. 了解Spring Boot应用程序的Fat Jar文件;
1. 掌握Markdown轻量级标记语言编写README.md文件。
- **二、 实验环境**
1. JDK 1.8或更高版本
1. Maven 3.6+
1. IntelliJ IDEA
**- 三、实验任务**
1. 通过IntelliJ IDEA的Spring Initializr向导创建Spring Boot项目;

1. **添加两个功能模块:spring MVC、lombok;**

1. 添加阿里云镜像仓库作为项目maven仓库;
修改maven目录下conf文件夹中的setting.xml文件
```java
G:/IDEA/aliyun
nexus-aliyun
*
Nexus aliyun
http://maven.aliyun.com/nexus/content/groups/public
jdk-1.8
true
1.8
1.8
1.8
1.8
```
4. 解释项目pom.xml文件中主要标签的意义;
groupId:被继承的父项目的全球唯一标识符
parent:父项目的坐标
artifactId:被继承的父项目的构件标识符
version:被继承的父项目的版本
modelVersion:声明项目描述符遵循哪一个POM模型版本
build:构建项目需要的信息
plugins:使用的插件列表
plugin:plugin元素包含描述插件所需要的信息
profile:根据环境参数或命令行参数激活某个构建处理
1. 配置jetty或undertow作为Spring Boot应用程序的默认Servlet容器;
在pom.xml文件dependencies当中添加
```java
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-tomcat
org.springframework.boot
spring-boot-starter-undertow
2.3.4.RELEASE
```
7. 修改Spring Boot应用程序启动时的Banner;

1. 添加一个简单的Spring Mvc控制器组件
```java
package cn.edu.dgut.css.sai.createspringbootprojectdemo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestController {
@RequestMapping("hello")
public String index()
{
return "你好,Spring Boot!";
}
}
```

9. 定义一个CommandLineRunner的Bean
```java
@Bean("Makima")
CommandLineRunner init(){
return args -> {
System.out.println("这是CommanderLineRunner!");
};
}
```

10. 编写一个简单的单完测试
```java
package cn.edu.dgut.css.sai.createspringbootprojectdemo;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@SpringBootTest
@AutoConfigureMockMvc
class CreateSpringBootProjectDemoApplicationTests {
@Autowired @Qualifier("Makima")
CommandLineRunner commandLineRunner;
@Autowired
cn.edu.dgut.css.sai.createspringbootprojectdemo.TestController testController;
@Test
void contextLoads(@Autowired ApplicationContext ac, @Autowired MockMvc mockMvc) throws Exception {
Assertions.assertNotNull(ac);
Object Makima=ac.getBean("Makima");
Assertions.assertNotNull(Makima);
Assertions.assertNotNull(mockMvc);
mockMvc.perform(MockMvcRequestBuilders.get("/hello"))
.andExpect(status().isOk())
.andDo(print());
Assertions.assertNotNull(this.commandLineRunner);
Assertions.assertNotNull(this.testController);
}
}
```

11. 使用IntelliJ IDEA的HTTP Client工具测试控制器端口;

12. 在命令行中使用spring官方提供的mave插件 spring-boot 运行Spring Boot应用程序,并把嵌入式Servlet容器的默认端口8080改为9090;

13. 在属性文件中配置Spring Boot应用程序以debug模式运行。

14. 在命令行中编译、打包Spring Boot应用程序。

15. 在命令行中使用java命令运行Spring Boot应用程序的Jar文件。

16. 在命令行中使用java命令运行Spring Boot应用程序的Jar文件,带参数改变嵌入式Servlet容器的默认端口8080改为9090。
```java
java -jar create-spring-boot-project-demo-0.0.1-SNAPSHOT.jar --debug --server.port=7070
```

**- 实验总结**
** pom.xml很重要**