我们不快乐的原因之一,是不知道如何安静地待在房间里,心平气和地与自己相处。——亦舒
例如我们需要进行一些自定义配置写到配置文件中
可以使用@ConfigurationProperties
注解
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.ruben.pojo;
import com.ruben.enumeration.GenderEnum; import lombok.Data; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component;
import java.util.List; import java.util.Map;
@Data @Component @ConfigurationProperties(prefix = "ruben") public class RubenProperties { private Integer number; private String avatar; private GenderEnum gender; private List<String> hobby; private Map<String, Object> introduce; }
|
这里枚举GenderEnum
1 2 3 4 5 6 7 8 9 10 11
| @Getter @AllArgsConstructor public enum GenderEnum {
FEMALE("女", 0), MALE("男", 1);
private final String name; private final Integer code;
}
|
然后yml
中对应配置写法
1 2 3 4 5 6
| ruben: number: 4444 avatar: /imgs/oss/2020-06-01/head.jpg gender: male hobby: ["游戏","动漫","编程"] introduce: {"food": "blood","programLanguage": "java"}
|
我们编写一个测试类测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
| package com.ruben;
import com.ruben.pojo.RubenProperties; import lombok.extern.slf4j.Slf4j; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest;
import javax.annotation.Resource; import java.util.Map;
@Slf4j @SpringBootTest public class PropertiesDemo {
@Resource private RubenProperties rubenProperties;
@Test public void test() { log.info(rubenProperties.toString()); }
}
|
运行结果