SpringBoot萬字爆肝高級配置

@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高級配置內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: