springboot配置文件绑定实现解析
作者:JayMu 时间:2022-06-07 23:32:38
这篇文章主要介绍了springboot配置文件绑定实现解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
先创建一个peron类,然后需要注解configurationProperties(prefix ="person")<br data-filtered="filtered">然后需要加一个@component<br data-filtered="filtered">因为只有在springboot的容器才能提供容器提供的@configurationProperties<br data-filtered="filtered">@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public boolean isBoss() {
return boss;
}
public void setBoss(boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person [lastName=" + lastName + ", age=" + age + ", boss=" + boss + ", birth=" + birth + ", maps="
+ maps + ", lists=" + lists + ", dog=" + dog + "]";
}
}
dog类
public class Dog {
private String Name;
private Integer age;
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Dog [Name=" + Name + ", age=" + age + "]";
}
}
写完后,ide会提示需要在pom.xml中导入组件处理器。
<!-- 配置文件的处理器 ,配置文件进行绑定就会有提示-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
然后创建配置文件,有两种方式,一个时yml文件,另一个时properties
1,application.yml
person:
last-name: zhangsan
age: 24
boss: false
birth: 2017/12/5
maps: {k1: v1,k2: v2}
lists: [lisi, zhangsan]
dog:
Name: xiaohei
age: 4
2.application.properties
中文字,在eclipse中自动转为unicode码
person.age=24
person.last-name=\u5F20\u4E09
person.birth=2000/1/1
person.boss=false
person.maps.k1=value1
person.maps.k2=12
person.dog.name=\u5C0F\u9ED1
person.dog.age=2
在test中使用spring boot的单元测试
@RunWith(SpringRunner.class)
@SpringBootTest
class Helloworld01QuickApplicationTests {
@Autowired
Person person;
@Test
void contextLoads() {
System.out.println(person);
}
}
运行,会看到得到配置文件中的数据
在获取配置文件中注入值得时候,可以使用@value,也可以使用@configurationProperties;
如果只是在逻辑中获取一下配置文件中得值,那么就使用@value
在配置文件注入值得时候也可以校验
在类加入注解@validate
配置文件注入数据校验
@validate
public class person{
private String last-name;
....
}
@PropertySource("classpath:person.properties") :加载指定的配置文件
@ImportResource(“classpath:beans.xml”):导入spring配置文件,让配置文件生效;
springboot推荐给容器增加组件
1.配置类--》spring配置文件
2.使用@bean给容器中增加组件;
配置文件占位符
1.随机数
${random.value}、${random.int}、${random.long}
${random.int(10)}、${random.int[1024,65536]}2.配置文件中找不到属性时的默认值。
${app.name:金毛}来指定找不到属性时的默认值。
profile
1.多个profile文件
Profile是Spring对不同环境提供不同配置功能的支持,可以通过激活、指定参数等方式快速切换环境
一般我们在开发的时候有测试环境,开发环境等。
我们在编写多个配置文件的时候,文件名字是application-(profile).properties/yml(这二种格式的都行)。
默认使用application.properties.
2.yml支持多文档块方式
application.yml
#三个横线属于一个文档块
#激活哪个环境
spring:
profiles:
active: test
#测试环境
---
server:
port: 8081
spring:
profiles: test
#开发环境
---
server:
port: 8082
spring:
profiles: dev
3.激活指定profile
在配置文件中指定spring.profiles.active =dev
springboot配置文件加载位置
这些配置都会加载,然后进行互补配置。
来源:https://www.cnblogs.com/hamish26/p/12171877.html
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Intellij Idea 多模块Maven工程中模块之间无法相互引用问题
![](https://img.aspxhome.com/file/2023/0/59230_0s.jpg)
Java对象不使用时赋值null的意义详解
![](https://img.aspxhome.com/file/2023/4/59884_0s.png)
两种JAVA实现短网址服务算法
IDEA 2021.2 激活教程及启动报错问题解决方法
![](https://img.aspxhome.com/file/2023/5/59345_0s.png)
springmvc如何使用map接收参数
![](https://img.aspxhome.com/file/2023/9/60289_0s.png)
微信小程序微信登录的实现方法详解(JAVA后台)
![](https://img.aspxhome.com/file/2023/0/58370_0s.jpg)
SpringBoot返回Json对象报错(返回对象为空{})
![](https://img.aspxhome.com/file/2023/2/61642_0s.png)
SpringBoot redis分布式缓存实现过程解析
![](https://img.aspxhome.com/file/2023/1/58541_0s.png)
SpringBoot整合WebService的实现示例
![](https://img.aspxhome.com/file/2023/4/60754_0s.png)
java.net.SocketException: Connection reset 解决方法
![](https://img.aspxhome.com/file/2023/6/57416_0s.png)
SpringCloud分布式链路追踪组件Sleuth配置详解
![](https://img.aspxhome.com/file/2023/6/60666_0s.png)
Android应用内悬浮窗Activity的简单实现
![](https://img.aspxhome.com/file/2023/4/84704_0s.png)
java多次嵌套循环查询数据库导致代码中数据处理慢的解决
Java8中对于LocalDateTime的序列化和反序列化问题
java Hibernate多对多映射详解及实例代码
![](https://img.aspxhome.com/file/2023/2/61052_0s.png)
深入理解Java8新特性之接口中的默认方法和静态方法
![](https://img.aspxhome.com/file/2023/9/59759_0s.png)
Java抢红包的红包生成算法
![](https://img.aspxhome.com/file/2023/2/57532_0s.jpg)
Jersey Restful接口如何获取参数的问题
解决Jackson解析嵌套类问题(MismatchedInputException)
![](https://img.aspxhome.com/file/2023/9/57909_0s.png)
SpringBoot中实现分布式的Session共享的详细教程
![](https://img.aspxhome.com/file/2023/6/58416_0s.jpg)