??博客主页:??
欢迎关注:??点赞??收藏留言
系列专栏:??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的依赖:
org.projectlombok
lombok
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
??编写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
??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
不能同时使用
加入依赖:
org.springframework.boot
spring-boot-configuration-processor
以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
??测试:
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;
}
??添加了SR303规范坐标与Hibernate校验框架对应坐标
javax.validation
validation-api
??使用hibernate框架提供的校验器做实现类
org.hibernate.validator
hibernate-validator
??对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"
运行结果:
先自我介绍一下,小编13年上师交大毕业,曾经在小公司待过,去过华为OPPO等大厂,18年进入阿里,直到现在。深知大多数初中级java工程师,想要升技能,往往是需要自己摸索成长或是报班学习,但对于培训机构动则近万元的学费,着实压力不小。自己不成体系的自学效率很低又漫长,而且容易碰到天花板技术停止不前。因此我收集了一份《java开发全套学习资料》送给大家,初衷也很简单,就是希望帮助到想自学又不知道该从何学起的朋友,同时减轻大家的负担。添加下方名片,即可获取全套学习资料哦