1.此项目是对springboot基本注解的一些使用,以及熟悉yarml文件的格式,将application.properties和各种类通过注解进行了绑定,这样十分的方便,而且在application.properties中还可以使用随机值设置,以及参数的间接引用
2.一些注意事项:
//需要添加相应的依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
-
在使用注解的时候,当注解是@value+@Component的时候,不需要在类中写getter和setter,但是他需要对类的每一个属性进行绑定
-
还可以使用的注解组合有:
1.@Componet+@ConfigurationProperties
2.@Component/@Configuration+@EnableConfigurationProperties+@ConfigurationProperties+@PropertySource
//使用value注解
类前写@Component,属性前写@Value("${books.numbers}")
//使用ConfigurationProperties注解
类前写@Component和@ConfigurationProperties(prefix = "myanimals")
注意:其中prefix的值不能大写
//使用自己定义的properties时要在类前写
@Component
@PropertySource("classpath:my.properties")//自己的properties位置以及名字
@EnableConfigurationProperties(Teacher.class)//该类的名字
@ConfigurationProperties(prefix = "teacher")//properties中该写的属性
-
toString是每个这里所用到的所有注解都需要用到的
-
//使用springboot多环境配置时在application.properties下添加 spring.profiles.active=prod //等于什么就看你需要的配置是叫什么名字
-
yarml文件注意嵌套的时候“ :”后面要跟一个空格
3.在类前引入@Validated注解,可以对该类的每一个属性都进行约束,常见有
-
@NotBlank用在String上
-
@NotEmpty用在集合上
-
@NotNull用在基本类型上
-
//需要添加相应的依赖 <!--jsr303 校验--> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency>
4.常用的随机配置
#随机数的配置
randoms.myNmuber=${random.int}//随机整数
randoms.UpNumber=${random.int(99)}//不超过
randoms.UnderNumbers=${random.int[1024,2024]}//区间
randoms.unknown=${random.value}//任意字符串
randoms.uuid=${random.uuid}//uuid
此项目的百度网盘地址如下:
链接:https://pan.baidu.com/s/1GE_G_A5Aaw8mfzyWC3lE0Q
提取码:6666