springboot的yml配置文件使用示例

以普通javabean为例进行说明:
1.Person.java:
package com.example.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 jack
  • @create 2019-07-06 11:51
    */

/**

  • 将文件中配置的每一个属性的值映射到这个组件中,@ConfigurationProperties
  • prefix = “person”:组件中所有的属性进行映射

*/

@Component
@ConfigurationProperties(prefix = “person”)
public class Person {
private String name ;
private Integer age ;
private Boolean boss ;
private Date birth ;
private Map<String,Object> maps ;
private List list ;
private Dog dog ;

public Person() {
}

@Override
public String toString() {
    return "Person{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", boss=" + boss +
            ", birth=" + birth +
            ", maps=" + maps +
            ", list=" + list +
            ", dog=" + dog +
            '}';
}

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 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> getMaps() {
    return maps;
}

public void setMaps(Map<String, Object> maps) {
    this.maps = maps;
}

public List<Object> getList() {
    return list;
}

public void setList(List<Object> list) {
    this.list = list;
}

public Dog getDog() {
    return dog;
}

public void setDog(Dog dog) {
    this.dog = dog;
}

public Person(String name, Integer age, Boolean boss, Date birth, Map<String, Object> maps, List<Object> list, Dog dog) {
    this.name = name;
    this.age = age;
    this.boss = boss;
    this.birth = birth;
    this.maps = maps;
    this.list = list;
    this.dog = dog;
}

}
需要注意添加不带参的构造函数
2.在pom.xml文件中添加 依赖:

org.springframework.boot
spring-boot-configuration-processor
true


3.新建application.yml文件,并注入属性:
server:
port: 8081
person:
name: zhangsan
age: 18
boss: false
birth: 2019/02/08
maps: {k1: v1,k2: v2}
list:
- lisi
- zhaoliu
dog:
name: 小狗
age: 18
注意格式:1.组件和属性之间需要回车到下一行并且属性和组件之间不能同一列起,必须用空格区分,属性名和属性值得书写格式:属性名:(空格)属性值
map键值之间也相同书写格式,如果是数组或者list集合或者Set集合,那么必须以-开头,书写格式:-(空格)数组或者集合元素
springboot测试类:
package com.example;

import com.example.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**

  • springboot单元测试,可以在测试期间很方便的类似编码一样自动注入
    */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SpringBootConfigApplicationTests {
    @Autowired
    Person person ;
    @Test
    public void contextLoads() {
    System.out.println(person);
    }

}

要在 Spring Boot 中使用 Redis 集群,需要在 `application.yml` 或 `application.properties` 文件中配置 Redis 集群的节点信息。下面是一个示例 `application.yml` 文件: ```yaml spring: redis: cluster: nodes: - host: 192.168.0.1 port: 6379 - host: 192.168.0.2 port: 6379 - host: 192.168.0.3 port: 6379 ``` 在这个示例中,Redis 集群由三个节点组成,它们的 IP 地址和端口号分别为 `192.168.0.1:6379`、`192.168.0.2:6379` 和 `192.168.0.3:6379`。 另外,如果 Redis 集群中启用了密码认证,还需要在配置文件中设置密码: ```yaml spring: redis: cluster: nodes: - host: 192.168.0.1 port: 6379 - host: 192.168.0.2 port: 6379 - host: 192.168.0.3 port: 6379 password: yourpassword ``` 此外,还可以配置 Redis 连接池的一些参数,例如最大连接数、最大空闲连接数、连接超时时间等: ```yaml spring: redis: cluster: nodes: - host: 192.168.0.1 port: 6379 - host: 192.168.0.2 port: 6379 - host: 192.168.0.3 port: 6379 password: yourpassword pool: max-active: 8 max-idle: 8 min-idle: 0 max-wait: -1ms ``` 这里的参数含义如下: - `max-active`:连接池中最大的活跃连接数,默认值为 8。 - `max-idle`:连接池中最大的空闲连接数,默认值为 8。 - `min-idle`:连接池中最小的空闲连接数,默认值为 0。 - `max-wait`:从连接池中获取连接的最大等待时间,默认值为 -1ms,表示永不超时。如果超时了,则会抛出 `RedisConnectionFailureException` 异常。 注意:以上配置只适用于使用 Redis 集群的情况,在单机 Redis 中,应该使用 `spring.redis.host` 和 `spring.redis.port` 配置主机地址和端口号。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值