您好,登錄后才能下訂單哦!
本文小編為大家詳細介紹“SpringBoot高級配置實例分析”,內容詳細,步驟清晰,細節處理妥當,希望這篇“SpringBoot高級配置實例分析”文章能幫助大家解決疑惑,下面跟著小編的思路慢慢深入,一起來學習新知識吧。
注意的是加入所需的支持這什么也不勾選。
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>
servers:
ipAddress: 168.225.0.15
port: 1234
timeout: -1
@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)
編寫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注解可以將使用@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
不能同時使用
加入依賴:
<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高級配置實例分析”文章已經介紹完畢,想要掌握這篇文章的知識點還需要大家自己動手實踐使用過才能領會,如果想了解更多相關內容的文章,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。