1:Spirng Boot 常用的两种配置文件
application.properties或者 application.yml 对 Spring Boot 程序进行简单的配置。
2:什么是 YML(YAML)?yml 配置的优势在哪里 ? 缺点呢
yml 是一种人类可读的数据序列化语言。它通常用于配置文件。
与属性文件相比,yml 文件就更加结构化,而且更少混淆。
缺点:不支持 @PropertySource 注解导入自定义的 yml 配置(后面有详细介绍)
配置格式对比.png
3:Spring Boot 常用的读取配置文件的方法有哪些?
#yml配置文件
wuhan2020: 2020年初武汉爆发了新型冠状病毒,但我相信一切都会过去,中国加油!
my-profile:
name: 我是名称
email: koushuangbwcx@163.com
library:
location: 湖北武汉加油中国加油
books:
- name: 时间的秩序
description: 为什么我们记得过去,而非未来?时间“流逝”意味着什么?是我们存在于时间之内,还是时间存在于我们之中?卡洛·罗韦利用诗意的文字,邀请我们思考这一亘古难题——时间的本质。
- name: 了不起的我
description: 如何养成一个新习惯?如何让心智变得更成熟?如何拥有高质量的关系? 如何走出人生的艰难时刻?
方法一:
通过@ConfigurationProperties读取并与 bean 绑定
@Component
@ConfigurationProperties(prefix = "library")
@Data
class LibraryProperties {
private String location;
private List<Book> books;
static class Book {
String name;
String description;
}
}
方法二:
如果LibraryProperties 类没有加 @Component 注解。
我们在我们要使用LibraryProperties 的地方使用
@EnableConfigurationProperties注册我们的配置 bean:
@EnableConfigurationProperties(LibraryProperties.class)
@Configuration
@EnableConfigurationProperties(LibraryProperties.class)
public class LibraryConfiguration {
@Bean
public String dataSource(LibraryPropertiesl libraryProperties) {
return libraryProperties.getName();
}
}
方法3:
@Value("${property}")
需要注意的是 @value这种方式是不被推荐的
@Compoent
@Data
public void Value(){
@Value("${wuhan2020}")
String wuhan2020;
@Value("${my-profile.name}")
String name;
}
另外注意:
@ConfigurationProperties只能读取全局配置文件
@PropertySource读取指定的 properties 文件
当然也可以读默认配置文件,但不能读取yml配置文件
源码中的createPropertySource设置仅能读取 properties(除非重写)
@Component
@PropertySource("classpath:website.properties")
@Data
public class WebsiteProperies {
}
4:Spring Boot 加载配置文件的优先级是怎么样的?
5:常用的 Bean 映射工具有哪些?
MapStruct、ModelMapper、Dozer、Orika、JMapper 是 5 种常用的 Bean 映射工具。
综合日常使用情况和相关测试数据,其中 MapStruct、ModelMapper 这两个 Bean 映射框架是最佳选择。
6: Spring Boot 如何做请求参数校验?
如果spring-boot版本小于2.3,spring-boot-starter-web包含Hibernate Validator
否则需要在pom包中进行引入:
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
Hibernate Validator包含了jakarta.validation(javax.validation.constraints)
(JSR)(全称Java Specification Requests )Java规范请求
可使用以下常用注解:
@NotNull 被注释的元素必须不为 null
@Min(value) 被注释的元素必须是一个数字,其值必须大于等于指定的最小值
@Max(value) 被注释的元素必须是一个数字,其值必须小于等于指定的最大值
@Size(max=, min=) 被注释的元素的大小必须在指定的范围内
@Pattern(regex=,flag=) 被注释的元素必须符合指定的正则表达式
@NotEmpty 被注释的字符串的必须非空
@NotBlank(message =) 验证字符串非 null,且长度必须大于 0
@Email 被注释的元素必须是电子邮箱地址
@Length(min=,max=) 被注释的字符串的大小必须在指定的范围内
@Range(min=,max=,message=) 被注释的元素必须在合适的范围内
注意: 可以使用 (message = "我是自定义信息")
使用教程:
@Data
public class Person {
@NotNull(message = "classId 不能为空")
private String classId;
@Pattern(regexp = "((^Man$|^Woman$|^UGM$))",
message = "sex 值不在可选范围")
@NotNull(message = "sex 不能为空")
private String sex;
@Email(message = "email 格式不正确")
@NotNull(message = "email 不能为空")
private String email;
}
//验证请求体(RequestBody
@RestController(Controller + ResponseBody)
@RequestMapping("/requestBody")
public class PersonController {
@PostMapping("/person")
public String getPerson(@RequestBody @Valid Person person) {
return "Success"
}
}
@RestController
@RequestMapping("/Param")
@Validated
public class PersonController {
//使用占位符
@GetMapping("/person/{id}")
public String getPersonByID(
@Valid @PathVariable("id")
@Max(value = 5,message = "超过 id 的范围了")
Integer id) {
return "Success"
}
//没使用占位符
@GetMapping("/person")
public String getPersonByName(
@Valid @RequestParam("name")
@Size(max = 5,message = "超过 name 的范围了")
String name) {
return "Success"
}
}
7: Spring Boot 中如何实现定时任务 ?
//在SpringBoot的启动类中加入注解@EnableScheduling 注解
//(作用是发现注解 @Scheduled 的任务并在后台执行该任务)
@Component
@Slf4j
public class ScheduledTasks {
/**
* fixedRate:固定速率执行。每5秒执行一次。
*/
@Scheduled(fixedRate = 5000)
public void reportCurrentTimeWithFixedRate() {
log.info("Fixed Rate Task : The time is now {}", dateFormat.format(new Date()));
}
}
Schedule格式: 秒 分 时 日 月 年
0/5 * * * * ? (每5秒执行一次)
0 0/10 * * * ? (每10分钟执行一次)
注意验证Scheduled网址:@Scheduled