一、简单获取配置文件的数据
application.yml配置文件
servers:
ipAddress: 127.0.0.1
port: 1234
timeout: -1
?创建配置文件的对应实体类
package com.gg.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* @Author: {LZG}
* @ClassName: ServerConfig
* @Description: TODO
* @Date: 2022/4/21 22:59
**/
@Component
@Data
@ConfigurationProperties(prefix = "servers")
public class ServerConfig {
private String ipAddress;
private int port;
private long timeout;
}
?作用:@ConfigurationProperties:快速、方便的将配置文件中的自定义属性注入到某个Bean对象的多个对应属性中去。
说明:@ConfigurationProperties(prefix = "servers"):将以servers开头的属性注入到Bean对象的对应属性
springboot启动类
package com.gg;
import com.gg.config.ServerConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
@SpringBootApplication
public class Springboot02UseConfigApplication {
public static void main(String[] args) {
// 获取容器对象
ConfigurableApplicationContext context = SpringApplication.run(Springboot02UseConfigApplication.class, args);
// 从容器中获得Bean对象
ServerConfig bean = context.getBean(ServerConfig.class);
System.out.println(bean);
}
}
运行结果
二、第三方Bean属性绑定
application.yml配置文件
servers:
ipAddress: 127.0.0.1
port: 1234
timeout: -1
datasource:
driver-class-name: com.mysql.jdbc.Driver1111
启动类
package com.gg;
import com.alibaba.druid.pool.DruidDataSource;
import com.gg.config.ServerConfig;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
@SpringBootApplication
public class Springboot02UseConfigApplication {
@Bean
@ConfigurationProperties(prefix = "datasource")
public DruidDataSource druidDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
public static void main(String[] args) {
// 获取容器对象
ConfigurableApplicationContext context = SpringApplication.run(Springboot02UseConfigApplication.class, args);
// 从容器中获得Bean对象
ServerConfig bean = context.getBean(ServerConfig.class);
System.out.println(bean);
DruidDataSource source = context.getBean(DruidDataSource.class);
System.out.println(source.getDriverClassName());
}
}
测试结果
?三、松散绑定
@ConfigurationProperties:支持松散绑定
?@Value:不支持松散绑定
配置文件中对应的属性必须和@Value的属性相同
测试:
配置文件中属性
测试类中的的代码?
?测试结果
改变以下配置文件中的书写格式其他代码不变
执行结果:执行失败
四、常用计量单位
JDK8提供的时间和空间计量单位
1、简单介绍
实体类
package com.gg.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import org.springframework.util.unit.DataSize;
import java.time.Duration;
/**
* @Author: {LZG}
* @ClassName: ServerConfig
* @Description: TODO
* @Date: 2022/4/21 22:59
**/
@Component
@Data
@ConfigurationProperties(prefix = "servers")
public class ServerConfig {
private String ipAddress;
private int port;
private long timeout;
private Duration serverTimeOut;
private DataSize dataSize;
}
配置文件
servers:
ipAddress: 127.0.0.1
port: 1234
timeout: -1
serverTimeOut: 3
dataSize: 10
datasource:
driverClassName: com.mysql.jdbc.Driver1111
执行结果
默认单位是:秒
默认单位:Byte
?2、使用注解设置单位
?此时实体类
package com.gg.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.convert.DataSizeUnit;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.stereotype.Component;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
/**
* @Author: {LZG}
* @ClassName: ServerConfig
* @Description: TODO
* @Date: 2022/4/21 22:59
**/
@Component
@Data
@ConfigurationProperties(prefix = "servers")
public class ServerConfig {
private String ipAddress;
private int port;
private long timeout;
@DurationUnit(ChronoUnit.HOURS)
private Duration serverTimeOut;
@DataSizeUnit(DataUnit.MEGABYTES)
private DataSize dataSize;
}
执行结果
?
?五、数据校验
?开启数据校验功能有助于系统安全性,J2EE规范中JSR303规定了一组有关数据校验的API
?使用JSR303需要导入坐标依赖
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.2.0.Final</version>
</dependency>
配置文件属性
实体类
package com.gg.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.convert.DataSizeUnit;
import org.springframework.boot.convert.DurationUnit;
import org.springframework.stereotype.Component;
import org.springframework.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import org.springframework.validation.annotation.Validated;
import javax.validation.constraints.Max;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
/**
* @Author: {LZG}
* @ClassName: ServerConfig
* @Description: TODO
* @Date: 2022/4/21 22:59
**/
@Component
@Data
@ConfigurationProperties(prefix = "servers")
// 开启对于当前bean的属性注入校验
@Validated
public class ServerConfig {
private String ipAddress;
@Max(value = 8888,message = "最大值不能超过8888")
private int port;
private long timeout;
@DurationUnit(ChronoUnit.HOURS)
private Duration serverTimeOut;
@DataSizeUnit(DataUnit.MEGABYTES)
private DataSize dataSize;
}
?测试结果
?说明:@Validated引入Spring框架支持的数据校验规则
相关的校验规则如下:
?
?
|