本文小编为大家详细介绍“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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。