Spring Boot 配置文件(Properties)
程序员文章站
2022-06-13 19:06:20
...
Spring Boot 配置文件(Properties)
properties文件书写格式:
server.port=8081
#配置person的值
person.last-name=张三
person.age=18
person.boss=true
person.birth=2017/12/12
person.map.k1=v1
person.map.k2=v2
person.lists=a,b,c
person.dog.name=dog
person.dog.age=15
配置文件值注入
package com.atguigu.springboot.bean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @author JiangDong
* @date 2020/7/22 - 15:33
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> map;
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 getBoss() {
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> getMap() {
return map;
}
public void setMap(Map<String, Object> map) {
this.map = map;
}
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 +
", map=" + map +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
Dog实体类
package com.atguigu.springboot.bean;
/**
* @author JiangDong
* @date 2020/7/22 - 15:37
*/
public class Dog {
private String name;
private Integer age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}
处理properties配置文件在idea中默认utf-8可能会乱码
运行结果
九个人不能让一个孩子在一个月内出生。
上一篇: STM32实现Airplay音乐播放器
下一篇: spring葵花宝典
推荐阅读
-
自己动手在Spring-Boot上加强国际化功能的示例
-
浅析Spring Boot中的spring-boot-load模块
-
spring-boot-klock-starter V1.1 主体功能重大更新内容介绍
-
Spring Boot + Kotlin整合MyBatis的方法教程
-
Spring Boot集成Quartz注入Spring管理的类的方法
-
myeclipse创建spring配置文件正确却显示红叉该怎么办?
-
【spring-boot 源码解析】spring-boot 依赖管理
-
使用Docker部署 spring-boot maven应用的方法
-
【Swagger】可能是目前最好的 Spring Boot 集成 swagger 的方案
-
在Spring Boot中使用Spring-data-jpa实现分页查询