# 使用Spring Boot构建应用程序
**Repository Path**: xie-hong/experiment-one
## Basic Information
- **Project Name**: 使用Spring Boot构建应用程序
- **Description**: 实验1:使用Spring Boot构建应用程序
- **Primary Language**: Unknown
- **License**: Not specified
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2020-10-07
- **Last Updated**: 2021-10-27
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
# 实验一 :使用Spring Boot构建应用程序
班级:网络工程1班 姓名:谢泓泓 学号:201841413134
实验日期:10/8
指导老师:黎志雄
#### 一、 实验目的
1、 掌握使用IntelliJ IDEA创建Spring Boot应用程序的方法;
2、 了解spring-boot-starter-parent的配置内容;
3、 掌握如何利用Starter扩展Spring Boot应用程序的功能;
4、 掌握如何配置Starter;
5、 掌握如何通过属性文件定制Spring Boot应用程序的初始化参数;
6、 掌握使用Spring Boot编写简单的单元测试;
7、 了解Spring Boot应用程序的Fat Jar文件;
8、 掌握Markdown轻量级标记语言编写README.md文件。
#### 二、 实验环境
1、 JDK 1.8或更高版本
2、 Maven 3.6+
3、 IntelliJ IDEA
#### 三、 实验任务
1、 通过IntelliJ IDEA的Spring Initializr向导创建Spring Boot项目;

2、 添加两个功能模块:spring MVC、lombok;

3、 添加阿里云镜像仓库作为项目maven仓库;
————通过修改settings.xml来添加
```java
nexus-aliyun
*
Nexus aliyun
http://maven.aliyun.com/nexus/content/groups/public
```
4、 解释项目pom.xml文件中主要标签的意义;


5、 配置jetty或undertow作为Spring Boot应用程序的默认Servlet容器;
*先把嵌入式的tomact库去除:
```java
org.springframework.boot
spring-boot-starter-tomcat
```
*配置undertow:
```java
org.springframework.boot
spring-boot-starter-undertow
2.3.4.RELEASE
```
6、 修改Spring Boot应用程序启动时的Banner;

7、 添加一个简单的Spring Mvc控制器组件,用于测试,如下图:
```java
@RestController
static class HelloWorldController{
@RequestMapping("hello")
String index() {
return"你好,spring Boot!";
}
}
```

8、定义一个CommandLineRunner的Bean,用于检查Spring Boot应用程序启动完成后在Spring IoC容器中注册的所有Bean。
```java
@Bean("initer")
CommandLineRunner init(ApplicationContext ac){
return args -> {
//自定义run方法
System.out.println("由Spring Boot注册的所有Bean:");
String[] beanNames=ac.getBeanDefinitionNames();
Arrays.sort(beanNames);
for(String beanName:beanNames){
System.out.println(beanName);
}
};
}
```

9、编写一个简单的单完测试。
```java
package cn.edu.dgut.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("initer")
CommandLineRunner initer;
@Test
void contextLoads(@Autowired ApplicationContext ac,@Autowired MockMvc mockMvc) throws Exception {
Assertions.assertNotNull(ac);
Object initer=ac.getBean("initer");
Assertions.assertNotNull(initer);
Assertions.assertNotNull(mockMvc);
mockMvc.perform(MockMvcRequestBuilders.get("/hello"))
.andExpect(status().isOk())
.andDo(print());
Assertions.assertNotNull(this.initer);
}
}
```

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


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

*修改端口


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

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

14-15、 在命令行中使用java命令运行Spring Boot应用程序的Jar文件;
在命令行中使用java命令运行Spring Boot应用程序的Jar文件,带参数改变嵌入式Servlet容器的默认端口8080改为9090。

#### 四、实验总结:
通过此次实验,我基本掌握使用IntelliJ IDEA创建Spring Boot应用程序的方法;了解了pom.xml文件的配置以及spring-boot-starter-parent的配置内容,在实验的进行过程中,懂得了使用springboot编写简单的文件测试,为以后学习打下基础;最后还掌握了怎么用Markdown轻量级标记语言编写README.md文件。