spring boot 读取配置文件(application.yml)中的属性值111

在spring boot中,简单几步,读取配置文件(application.yml)中各种不同类型的属性值:

1、引入依赖:

[html] view plain copy

  1. <!-- 支持 @ConfigurationProperties 注解 -->
  2. <dependency>
  3. <groupId>org.springframework.boot</groupId>
  4. <artifactId>spring-boot-configuration-processor</artifactId>
  5. <optional>true</optional>
  6. </dependency>

2、配置文件(application.yml)中配置各个属性的值:

[plain] view plain copy

  1. myProps: #自定义的属性和值
  2. simpleProp: simplePropValue
  3. arrayProps: 1,2,3,4,5
  4. listProp1:
  5. - name: abc
  6. value: abcValue
  7. - name: efg
  8. value: efgValue
  9. listProp2:
  10. - config2Value1
  11. - config2Vavlue2
  12. mapProps:
  13. key1: value1
  14. key2: value2

3、创建一个bean来接收配置信息:

[java] view plain copy

  1. @Component
  2. @ConfigurationProperties(prefix="myProps") //接收application.yml中的myProps下面的属性
  3. public class MyProps {
  4. private String simpleProp;
  5. private String[] arrayProps;
  6. private List<Map<String, String>> listProp1 = new ArrayList<>(); //接收prop1里面的属性值
  7. private List<String> listProp2 = new ArrayList<>(); //接收prop2里面的属性值
  8. private Map<String, String> mapProps = new HashMap<>(); //接收prop1里面的属性值
  9. public String getSimpleProp() {
  10. return simpleProp;
  11. }
  12. //String类型的一定需要setter来接收属性值;maps, collections, 和 arrays 不需要
  13. public void setSimpleProp(String simpleProp) {
  14. this.simpleProp = simpleProp;
  15. }
  16. public List<Map<String, String>> getListProp1() {
  17. return listProp1;
  18. }
  19. public List<String> getListProp2() {
  20. return listProp2;
  21. }
  22. public String[] getArrayProps() {
  23. return arrayProps;
  24. }
  25. public void setArrayProps(String[] arrayProps) {
  26. this.arrayProps = arrayProps;
  27. }
  28. public Map<String, String> getMapProps() {
  29. return mapProps;
  30. }
  31. public void setMapProps(Map<String, String> mapProps) {
  32. this.mapProps = mapProps;
  33. }
  34. }

启动后,这个bean里面的属性就会自动接收配置的值了。

4、单元测试用例:

[java] view plain copy

  1. @Autowired
  2. private MyProps myProps;
  3. @Test
  4. public void propsTest() throws JsonProcessingException {
  5. System.out.println("simpleProp: " + myProps.getSimpleProp());
  6. System.out.println("arrayProps: " + objectMapper.writeValueAsString(myProps.getArrayProps()));
  7. System.out.println("listProp1: " + objectMapper.writeValueAsString(myProps.getListProp1()));
  8. System.out.println("listProp2: " + objectMapper.writeValueAsString(myProps.getListProp2()));
  9. System.out.println("mapProps: " + objectMapper.writeValueAsString(myProps.getMapProps()));
  10. }

测试结果:

[plain] view plain copy

  1. simpleProp: simplePropValue
  2. arrayProps: ["1","2","3","4","5"]
  3. listProp1: [{"name":"abc","value":"abcValue"},{"name":"efg","value":"efgValue"}]
  4. listProp2: ["config2Value1","config2Vavlue2"]
  5. mapProps: {"key1":"value1","key2":"value2"}

源代码参考:https://github.com/xujijun/my-spring-boot

时间: 2024-08-07 21:17:05

spring boot 读取配置文件(application.yml)中的属性值111的相关文章

IDEA开发spring boot应用时 application.yml 或 application.properties 自定义属性提示

在使用spring boot开发过程中,经常会定义一些应用自己的属性,直接写到application配置文件中使用@Value注解进行使用,这样使用也没有什么问题.不过我认为更优雅的方式是定义自己的属性类统一管理,这样在idea中,既能自动提示,又能对配置进行分类管理,显得有条不紊,下面是具体的配置步骤. 第一步:添加依赖(分为maven和gradle两种方式) 1.1 如果你使用的是maven 增加依赖 <dependency> <groupId>org.springframew

Spring Boot之配置文件application.properties

Spring Boot默认的配置文件为application.properties,放在src/main/resources目录下或者类路径的/config目录下,作为Spring Boot的全局配置文件,对一些默认的配置进行修改. 接下来使用例子展示Spring Boot配置文件的用法: 首先在src/main/resources下新建application.properties文件,内容如下 author.name=微儿博客 author.sex=男 创建controller类 ackage

【spring boot】配置文件 application.properties 属性解析

1.JPA命名策略 spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.DefaultNamingStrategy 有两种值可以配置分别为: 第一:org.hibernate.cfg.DefaultNamingStrategy 第二:org.hibernate.cfg.ImprovedNamingStrategy DefaultNamingStrategy这个直接映射,不会做过多的处理(前提没有设置@Table,@Column等属性的

spring boot 读取配置文件信息

1.读取application.properties @Component @ConfigurationProperties(prefix="images.product.column") public class ColumnImageConfig { private String path; public String getPath() { return path; } public void setPath(String path) { this.path = path; }

Spring Boot 学习之路二 配置文件 application.yml

一.创建配置文件 如图所示,我们在resources文件夹中新建配置文件application.yml 结构图 二.一些基本配置 server: port: 8090 //配置端口 session-timeout: 30 tomcat.max-threads: 0 tomcat.uri-encoding: UTF-8 spring: datasource: //数据库配置 url : jdbc:mysql://localhost:3306/newbirds username : root pas

企业分布式微服务云SpringCloud SpringBoot mybatis (十九)Spring Boot 自定义配置文件

上面介绍的是我们都把配置文件写到application.yml中.有时我们不愿意把配置都写到application配置文件中,这时需要我们自定义配置文件,比如test.properties: com.forezp.name=forezp com.forezp.age=12 怎么将这个配置文件信息赋予给一个javabean呢? @Configuration @PropertySource(value = "classpath:test.properties") @Configuratio

SpringBoot中logback.xml使用application.yml中属性

教你如何使用 springProfile 与 springProperty 让你的logback.xml 配置显得更有逼格,当别人还在苦苦挣扎弄logback-{profile}.xml的时候 你一个文件就搞定了- - springProfile 该 <springProfile> 标签允许我们更加灵活配置文件,可选地包含或排除配置部分.元素中的任何位置均支持轮廓部分.使用该name属性指定哪个配置文件接受配置.可以使用逗号分隔列表指定多个配置文件. <springProfile nam

Spring boot 通用配置文件模板

001 # ===================================================================002 # COMMON SPRING BOOT PROPERTIES003 #004 # This sample file is provided as a guideline. Do NOT copy it in its005 # entirety to your own application.               ^^^006 # ==

将source类中的属性值赋给target类中对应的属性

/** * 对象的属性值拷贝 * <p> * 将source对象中的属性值赋值到target对象中的属性,属性名一样,类型一样 * <p> * example: * <p> * source: * <p> * String name; * String address; * Integer age; * Date birthday; * <p> * target: * String name; * String address; * String