博客主页:不会压弯的小飞侠
✨欢迎关注:点赞收藏⭐留言✒
✨系列专栏: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
编写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
不能同时使用
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
</dependency>
以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}"
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校验框架对应坐标
<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;
}
编写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"