87.【SpringBoot-01】(三)

简介: 87.【SpringBoot-01】

配置yaml语句: 包含对象 数组 Map集合 List数组

# 外对象
person:
# 基本属性
  name: jsxs
  age: 18
  happy: No
  birth: 2001/12/01
#键值对
  maps: {k1: v1,k2: v2}
#数组
  lists:
    - code
    - music
    - girls
# 内嵌对象
  dog:
    name: 旺财
    age: 15

测试

package com.example.springboot01hello;
import com.example.springboot01hello.pojo.Dog;
import com.example.springboot01hello.pojo.people;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource;
@SpringBootTest
class SpringBoot01HelloApplicationTests {
@Resource   //进行自动装配的操作,也就是我们前面的
    private Dog dog;
@Resource
private people people;
    @Test
    void contextLoads() {
        System.out.println(dog);
        System.out.println(people);
    }
}

(3).使用properties进行配置和读取配置属性(SPEL)
package com.example.springboot01hello.pojo;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Component
//javaConfig 绑定我么配置文件的值,可以采用这些方式
// 加载指定的配置文件
@PropertySource(value = "classpath:jsxs.properties")
public class people {
//  SPEL表达式读取配置文件的值
 @Value("${name}")
    private String name;
    @Value("${age}")
    private Integer age;
    @Value("${happy}")
    private Boolean happy;
    @Value("${birth}")
    private Date birth;   //这里的日期格式,一定要是xxxx/xx/xx否则报错
    private Map<String,Object> maps;
    private List<Object> lists;
    private Dog dog;
    public people(String name, Integer age, Boolean happy, Date birth, Map<String, Object> maps, List<Object> lists, Dog dog) {
        this.name = name;
        this.age = age;
        this.happy = happy;
        this.birth = birth;
        this.maps = maps;
        this.lists = lists;
        this.dog = dog;
    }
    public people() {
    }
    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;
    }
    public Boolean getHappy() {
        return happy;
    }
    public void setHappy(Boolean happy) {
        this.happy = happy;
    }
    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 "people{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", happy=" + happy +
                ", birth=" + birth +
                ", maps=" + maps +
                ", lists=" + lists +
                ", dog=" + dog +
                '}';
    }
}

利用properties进行配置

通过EL表达式进行获取数据。${}

(4).yaml的SPEL占位符

${}

# 外对象
person:
# 基本属性
  name: jsxs${random.uuid}
  age: ${random.int}
  happy: false
  birth: 2001/12/01
  hello: sdsdsd
#键值对
  maps: {k1: v1,k2: v2}
#数组
  lists:
    - code
    - music
    - girls
# 内嵌对象
  dog:
    # 假如说person这个yaml类中不存在hello,那么就会输出hello00_旺财
    # 假如说存在: 就会输出存在的hello的值
    name: ${person.hello:hello00}_旺财
    age: 15

4.yaml与properties的区别

⭐@ConfigurationProperties只需要写一次,value需要每个字段都要加上

⭐松散绑定: 意思就是我的yaml中写的last-name,这个和lastName是一样的,-后面跟着的字母默认是大写的,这就是松散绑定。

⭐JR303数据校验,这个就是我们可以在字段是增加一层过滤器验证,可以保证数据的合法性

⭐复杂类型封装,yml中可以封装对象,使用@Value就不支持。

结论

配置yaml和配置properties都可以获取到值 , 但是强烈推荐 yaml

如果我们在某个业务中,只需要获取配置文件中的某个值,可以使用一下 @value;

如果说,我们专门编写了一个JavaBean来和配置文件进行一一映射,就直接@configurationProperties,不要犹豫!


相关文章
|
7月前
|
负载均衡 监控 Dubbo
91.【SpringBoot-03】(二)
91.【SpringBoot-03】
42 0
|
3月前
|
JSON Java 数据安全/隐私保护
Springboot 之 HandlerMethodReturnValueHandler 运用
Springboot 之 HandlerMethodReturnValueHandler 运用
39 0
|
6月前
|
监控 Dubbo Java
|
7月前
|
Java 测试技术 容器
87.【SpringBoot-01】(六)
87.【SpringBoot-01】
40 0
|
7月前
89.【SpringBoot-02】(七)
89.【SpringBoot-02】
24 0
|
8月前
|
XML 运维 安全
springboot
springboot
82 0
|
10月前
|
XML 安全 Java
|
10月前
|
前端开发 Java 测试技术
SpringBoot相关知识
SpringBoot相关知识
32 0
|
监控 Java
SpringBoot
springBoot
128 0
|
监控 前端开发 Java
什么是 SpringBoot?为什么要用 SpringBoot?
什么是 SpringBoot?为什么要用 SpringBoot,2、Spring Boot 的核心注解是哪个?它主要由哪几个注解组成的?,3、运行 Spring Boot 有哪几种方式?,4、如何理解 Spring Boot 中的 Starters?,5、如何在 Spring Boot 启动的时候运行一些特定的代码?,6、Spring Boot 需要独立的容器运行吗?,7、Spring Boot 中的监视器是什么?,8、如何使用 Spring Boot 实现异常处理?,9、你如何理解 Spring Boot 中的 Starters?,10、springboot 常用的 starter 有哪些.