SpringBoot万字爆肝高级配置

2022-11-13 12:11:14 配置 高级 万字

@ConfigurationProperties注入

创建一个新的模板

此过程就不在这介绍了,在我SpringBoot专栏里有详细过程。

⭐⭐⭐注意的是加入所需的支持这什么也不勾选。

ServerConfig.java

package com.jkj.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@ConfigurationProperties(prefix = "servers")
@Component
@Data
public class ServerConfig {
    private String ipAddress;
    private int port;
    private long timeout;
}

⭐⭐⭐注意:

@Data

为当前实体类在编译期设置对应的get/set,toString方法,hashCode方法,equals方法等。

需要导入lomok的依赖:

<dependency>
			<groupId>org.projectlombok</groupId>
			<artifactId>lombok</artifactId>
		</dependency>

编写application.yml

servers:
  ipAddress: 168.225.0.15
  port: 1234
  timeout: -1

编写ConfigurationApplication.java

@SpringBootApplication
public class ConfigurationApplication {
	public static void main(String[] args) {
		ConfigurableApplicationContext cac = SpringApplication.run(ConfigurationApplication.class, args);
		ServerConfig bean = cac.getBean(ServerConfig.class);
		System.out.println(bean); 
	}
}

运行结果:ServerConfig(ipAddress=168.225.0.15, port=123456, timeout=-1)

给第三方Bean绑定属性

方式一

?编写ConfigurationApplication.java

@SpringBootApplication
public class ConfigurationApplication {
    @Bean
	public DruidDataSource dataSource(){
		DruidDataSource ds = new DruidDataSource();
		ds.setDriverClassName("com.Mysql.jdbc.Driver");
		return ds;
	}
	public static void main(String[] args) {
		ConfigurableApplicationContext cac = SpringApplication.run(ConfigurationApplication.class, args);
		ServerConfig bean = cac.getBean(ServerConfig.class);
		System.out.println(bean); 
		DruidDataSource ds = cac.getBean(DruidDataSource.class);
		System.out.println(ds);
	}
}

运行结果:com.mysql.jdbc.Driver

方式二

使用@ConfigurationProperties为第三方bean绑定属性

?编写application.yml

servers:
  ipAddress: 168.225.0.15
  port: 1234
  timeout: -1
dataSource:
  driverClassName: com.mysql.jdbc.Driver

?编写ConfigurationApplication.java

@SpringBootApplication
public class ConfigurationApplication {
    @Bean
    @ConfigurationProperties(prefix = "datasource")
	public DruidDataSource dataSource(){
		DruidDataSource ds = new DruidDataSource();
		return ds;
	}
	public static void main(String[] args) {
		ConfigurableApplicationContext cac = SpringApplication.run(ConfigurationApplication.class, args);
		ServerConfig bean = cac.getBean(ServerConfig.class);
		System.out.println(bean); 
		DruidDataSource ds = cac.getBean(DruidDataSource.class);
		System.out.println(ds.getDriverClassName()); 
	}
}

运行结果:com.mysql.jdbc.Driver

@EnableconfigurationProperties

@EnableConfigurationProperties注解可以将使用@ConfigurationProperties注解对应的类加入Spring容器

?ConfigurationApplication.java

@SpringBootApplication
@EnableConfigurationProperties(ServerConfig.class)
public class ConfigurationApplication {
    @Bean
    @ConfigurationProperties(prefix = "datasource")
	public DruidDataSource dataSource(){
		DruidDataSource ds = new DruidDataSource();
		return ds;
	}
	public static void main(String[] args) {
		ConfigurableApplicationContext cac = SpringApplication.run(ConfigurationApplication.class, args);
		ServerConfig bean = cac.getBean(ServerConfig.class);
		System.out.println(bean); 
		DruidDataSource ds = cac.getBean(DruidDataSource.class);
		System.out.println(ds.getDriverClassName()); 
	}
}

?ServerConfig.java

package com.jkj.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
@ConfigurationProperties(prefix = "servers")
@Data
public class ServerConfig {
    private String ipAddress;
    private int port;
    private long timeout;
}

⭐⭐⭐注意:@EnableConfigurationProperties@Component不能同时使用

解除使用@ConfigurationProperties注释警告

加入依赖:

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
		</dependency>

宽松绑定

  • 宽松绑定就是configrationProperties注入的书写规范并不严格,大小写,下划线、中划线、springboot都可以忽略。
  • @ConfigurationProperties绑定属性支持属性名宽松绑定。

以ipAddress为例:

?驼峰模式

 ipAddress: 168.225.0.15

?下划线模式

ip_address: 168.225.0.15

?中划线模式

IP_ADD_R-E-SS: 168.225.0.15

?常量模式

IP_ADDRESS: 168.225.0.15

?大写模式

 IPADDRESS: 168.225.0.15

?烤肉串模式(推荐使用)

  ip-address: 168.225.0.15

宽松绑定不支持注解@Value引用单个属性的方式

?测试

package com.jkj;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot06ConfigurationApplicationTests {
    @Value("${servers.ipAddress}")
	private String msg;
	@Test
	void contextLoads() {
		System.out.println(msg); 
	}
}

报错信息:Could not resolve placeholder 'servers.ipAddress' in value "${servers.ipAddress}"

常量计量单位绑定

SpringBoot支持jdk8提供的时间与空间计量单位

package com.jkj.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.util.unit.DataSize;
import org.springframework.util.unit.DataUnit;
import java.time.Duration;
import java.time.temporal.ChronoUnit;
@ConfigurationProperties(prefix = "servers")
@Data
public class ServerConfig {
    private String ipAddress;
    private int port;
    private long timeout;
    @DurationUnit(ChronoUnit.DAYS)
    private Duration serverTimeOut;
    @DataSizeUnit(DataUnit.TERABYTES)
    private DataSize dataSize;
}

?常用计量单位 ChronoUnit

DataUnit

数据校验

?添加了SR303规范坐标与Hibernate校验框架对应坐标

	<dependency>
			<groupId>javax.validation</groupId>
			<artifactId>validation-api</artifactId>
		</dependency>

?使用hibernate框架提供的校验器做实现类

<dependency>
			<groupId>org.hibernate.validator</groupId>
			<artifactId>hibernate-validator</artifactId>
		</dependency>

?对Bean开启校验功能

@ConfigurationProperties(prefix = "servers")
@Validated
@Data
public class ServerConfig {
    private String ipAddress;
    private int port;
    private long timeout;
}

?设置校验规则

@ConfigurationProperties(prefix = "servers")
@Validated
@Data
public class ServerConfig {
    private String ipAddress;
    @Max(value = 9999,message = "最大值不能超过9999")
    @Min(value = 1111,message = "最小值不能超过1111")
    private int port;
    private long timeout;
}

?yml文件中port设置为1234,测试结果如下

进制数据转换规则

案例模拟

?编写application.yml

servers:
  ipAddress: 168.225.0.15
  port: 1234
  timeout: -1
dataSource:
  driverClassName: com.mysql.jdbc.Driver
  passWord: 0123

?编写ConfigureApplication,java

package com.jkj;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
@SpringBootTest
class Springboot06ConfigurationApplicationTests {
    @Value("${dataSource.Password}")
	private Password psw;
	@Test
	void contextLoads() {
		System.out.println(psw); 
	}
}

测试结果是个八进制的83。

⭐⭐⭐注意:根据字面值表达方式,String类型的需要使用双引号包裹。

?重新编写application.yml

servers:
  ipAddress: 168.225.0.15
  port: 1234
  timeout: -1
dataSource:
  driverClassName: com.mysql.jdbc.Driver
  password: "0123"

运行结果:

到此这篇关于SpringBoot万字爆肝高级配置的文章就介绍到这了,更多相关SpringBoot高级配置内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!

相关文章