一.新建项目
官网
- 官方文档 https://docs.spring.io/spring-boot/docs/
- 官方地址 https://spring.io/
- 官方中文地址 https://www.kancloud.cn/george96/java-springboot/613741
参考以上中或英文文档 「11.开发你的第一个Spring Boot应用」
a. 官网直接下载初始项目
- start.Spring.io 来生成压缩包,解压导入即可
b. idea直接生成对应项目
- new project-> spring Initializr
c. Spring boot 启动文字banner小工具
- 具体方法在官网banner 应用,23.2 自定义banner
- banner 生成网站 https://www.bootschool.net/ascii
d.setting.xml 文件更改
- 镜像地址,可选阿里云的,下载快
- 代码仓库地址
-
Linux 中运行jar包的命令 java -jar
f.springBoot 项目pom依赖
二.配置文件
1.yaml 中读入配置文件中的配置@ConfigurationProperties 与 @Value 的区别
yml 文件中,如下配置了一个person 实体
person:
last_name: zhangsan
age: 12
sex: 男
likes:
- book
- movie
- girl
启动注入java 实体Person的时候
//@ConfigurationProperties(prefix = "person")
@Component
//@Validated
@Data
public class Person {
// @Value("${person.name}")
// @Email
private String lastName;
@Value("#{1+2}")
private Integer age;
private String sex;
private List<String> likes;
}
a. 使用@ConfigurationProperties(prefix = "person") 时 ,注解在类上边,系统批量自动注入;使用@Value("${person.name}") 时,单个字段逐一注解;
b.松散绑定是指 在使用@ConfigurationProperties(prefix = "person") 时,可以将配置文件中last_name的值映射进类Person 中的lastName中;
c.SpEL,表达式的支持,@Value 是支持的;
d.数据校验,类配合@Validated 元素中注解要检验的类型,如@Email来进行值的校验 ;
2.配置文件的优先级
- classpath 一般就是以 resources 为根的
- file 是以当前项目目录为根的,就是src 之前的那个目录
3.多环境文件配置
配置多个环境文件 如: application-dev.yaml ; application-test.yaml,可以通过在配置文件application.yaml中来指定处于激活状态的环境文件;可以认为前两个是第三个的补充
spring:
profiles:
active: test