Python 官方文档:入门教程 => 点击学习
目录@ConfigurationProperties注入创建一个新的模板ServerConfig.java编写application.yml编写ConfigurationApplic
此过程就不在这介绍了,在我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>
以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高级配置内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: SpringBoot万字爆肝高级配置
本文链接: https://lsjlt.com/news/164442.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0