Itheima-Springboot2【二刷】

1. Springboot parent和starter区别

  • parent :开发Springboot项目需要继承spring-boot-starter-parent,其中定义了若干个依赖管理(坐标版本号 ),避免依赖版本冲突
  • starter :开发Springboot项目需要导入坐标时通常导入对应的starter,每个starter根据功能不同,通常包含多个依赖坐标简化配置

2. 配置文件加载优先级

  • .properties > .yml > .yaml
  • 不同配置文件中相同的配置按照加载优先级相互覆盖,不同配置文件中不同配置全部保留。

3. 配置文件属性提示消失解决方案

4. 读取配置文件属性数据

  • 读取单一属性属性:@Value
java 复制代码
@Value("${server.port}")
private String port;
  • 读取全部属性数据:Environment
java 复制代码
@Autowired
private Environment env; 
...
sout(env.getProperty("server.port"));
  • 自定义对象封装指定数据(目前常用

5. 整合第三方技术

  1. 导入相关依赖(starter)
  2. 配置yml

6. 工程打包与运行

  • mvn clean -> mvn package
  • pom里应包含打包插件spring-boot-maven-plugin
  • java -jar xxxx.jar [(临时属性:)--server.port=8080 --xx=xx] 运行jar文件。

  • linux系统打包运行暂时跳过!

7. 配置文件4级分类


多层级配置文件间的属性采用叠加并覆盖的形式作用于程序。

8. 多环境开发

  • 单文件

  • 多文件(yml) :主文件中设置公共属性,环境分类文件中设置冲突属性

  • 多文件(properties)

  • 多环境分组管理

9. 热部署

  • 手工启动热部署

(1)导入依赖 devtools

java 复制代码
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifcatId>spring-boot-devtools</artifactId>
</dependency>

(2)激活热部署(restart):build project (Ctrl + F9)

  • 自动启动热部署


    激活方式:IDEA失去焦点5s后启动热部署。

10. @ConfigurationProperties 第三方Bean属性绑定

java 复制代码
@Bean
@ConfigurationProperties(prefix="datasource")
public DruidDataSource datasource(){
	DruidDataSource ds = new DruidDataSource();
	return ds;
}
  • 宽松绑定属性名 可以宽松绑定,但绑定前缀名prefix)必须仅能使用纯小写字母、数字、下划线作为合法字符;

  • Bean的属性校验

    (1)导入JSR303和Hibernate校验框架依赖;

    (2)使用@Validated注解启用校验功能;

    (3)使用具体的校验规则规范数据校验格式。

11. web环境模拟测试 webEnvironment =

(1)设置测试端口

java 复制代码
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.DEFINED_PORT)
public class WebTest {
	@Test
	void test(){
		xxxxx;
	}
}

(2)模拟测试启动

(3)模拟测试匹配

java 复制代码
@Test
void testGetById(@Autowired MockMvc mvc) throws Exception {
	MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/books");
	ResultActions action = mvc.perform(builder);

	// 测试响应状态
	StatusResultMatchers status =  MockMvcResultMatchers.status();
	ResultMatcher ok = status.isOk();
	action.andExpect(ok);

	// 测试响应头
	StatusResultMatchers header=  MockMvcResultMatchers.header();
	ResultMatcher contentType = header.string("Content_Type", "application/json");
	action.andExpect(contentType);

	// 测试响应体,json
	StatusResultMatchers content=  MockMvcResultMatchers.content();
	ResultMatcher result= content.json("{\"id\":1,\"name\":\"wyw\"}");
	action.andExpect(result);
}
相关推荐
计算机学姐15 分钟前
基于微信小程序的调查问卷管理系统
java·vue.js·spring boot·mysql·微信小程序·小程序·mybatis
2401_857622667 小时前
SpringBoot框架下校园资料库的构建与优化
spring boot·后端·php
2402_857589367 小时前
“衣依”服装销售平台:Spring Boot框架的设计与实现
java·spring boot·后端
哎呦没9 小时前
大学生就业招聘:Spring Boot系统的架构分析
java·spring boot·后端
编程、小哥哥9 小时前
netty之Netty与SpringBoot整合
java·spring boot·spring
IT学长编程10 小时前
计算机毕业设计 玩具租赁系统的设计与实现 Java实战项目 附源码+文档+视频讲解
java·spring boot·毕业设计·课程设计·毕业论文·计算机毕业设计选题·玩具租赁系统
杨哥带你写代码10 小时前
足球青训俱乐部管理:Spring Boot技术驱动
java·spring boot·后端
A尘埃11 小时前
SpringBoot的数据访问
java·spring boot·后端
yang-230711 小时前
端口冲突的解决方案以及SpringBoot自动检测可用端口demo
java·spring boot·后端
代码之光_198011 小时前
SpringBoot校园资料分享平台:设计与实现
java·spring boot·后端