读取内容到List中的一些细节

我有点强迫症(其实是为了开发工具时配置文件更加美观),我有如下的写法:

配置文件

1
2
3
4
5
6
7
8
9

tables:
  - logic-name: t_order
    entity-name: 订单
  - logic-name: t_task
    entity-name: 任务
  - logic-name: t_color_atla
    entity-name: 色卡

解析类:

 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
34
35
36
37
38
39
40
41
42
43
44
45
46

@Data
@Component
public class TableProperties implements ApplicationContextAware {

    private List<Table> tables;


    @Data
    public static class Table {

        /**
         * 表逻辑名称
         */
        private String logicName;

        /**
         * 对应的实体名称
         */
        private String entityName;

        /**
         * 不需要生成的模板
         */
        private List<String> templatesExclude;
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //noinspection unchecked
        tables = (List<Table>) applicationContext.getBean("tables");
        System.out.println("");
    }

    @Configuration
    public static class TablePropertiesConfiguration {

        @Bean
        @ConfigurationProperties(prefix = "tables")
        public List<Table> tables() {
            return new ArrayList<>(0);
        }
    }

}

比常规的写法有点复杂,如果使用常规的写法,我的配置文件如下:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12

# 解析类上@ConfigurationProperties中的参数
PERFIX:
    # 解析类中字段的名称
    tables:
      - logic-name: t_order
          entity-name: 订单
      - logic-name: t_task
          entity-name: 任务
      - logic-name: t_color_atla
          entity-name: 色卡

这种写法会增加我一层配置,我非常的不喜欢,所以我自己开发了上面的非常规的写法。

今天我再使用该方法时,遇到了一些问题,记录如下:

多段名称

我的配置如下:

1
2
3
4
5
6
7
8
9

development-season-config:
  - index: 1
    name: 季节
  - index: 2
    name: 品牌
  - index: 3
    name: 类别

我的正确的解析类如下:

 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
34
35
36
37
38
39
40
41
42
43

package com.sdstc.pdm.server.config;


import com.sdstc.pdm.common.dto.DevelopmentSeasonConfigDTO;
import lombok.Data;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Data
@Component
public class DevelopmentSeasonConfigProperties implements ApplicationContextAware {

    public List<DevelopmentSeasonConfigDTO> developmentSeasonConfig;

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        //noinspection unchecked
        developmentSeasonConfig =
                (List<DevelopmentSeasonConfigDTO>) applicationContext.getBean("developmentSeasonConfig");
    }

    @Configuration
    public static class DevelopmentSeasonConfigPropertiesConfiguration {
        @Bean
        @ConfigurationProperties(prefix = "development-season-config")
        public List<DevelopmentSeasonConfigDTO> developmentSeasonConfig() {
            return new ArrayList<>();
            // return Collections.emptyList();
        }
    }
}

我犯了哪些错误:

  1. ConfigurationProperties注解的方法名写错了,用了copy时的tables,最终导致我在容器里找不到相应的Bean。
  2. 忘记了这套方案的逻辑:是通过Configuration创造出一个Bean,然后再在setApplicationContext中将这个Bean赋值给对应的字段。

Configuration中返回值

如下代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11

@Configuration
public static class DevelopmentSeasonConfigPropertiesConfiguration {
    @Bean
    @ConfigurationProperties(prefix = "development-season-config")
    public List<DevelopmentSeasonConfigDTO> developmentSeasonConfig() {
        return new ArrayList<>();
        // return Collections.emptyList();
    }
}

如果我注释return new ArrayList<>(),取消注释return Collections.emptyList();,那么最终的表现结果和我预期的是不一样的,根本无法成功的获取到配置文件中得配置,而是得到了一个空列表。所以,我还是建议自己使用如下写法,这种写法是学习尚硅谷课程时学到的:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

@Configuration
public static class DevelopmentSeasonConfigPropertiesConfiguration {
    @Bean
    @ConfigurationProperties(prefix = "development-season-config")
    public List<DevelopmentSeasonConfigDTO> developmentSeasonConfig(List<DevelopmentSeasonConfigDTO> developmentSeasonConfigDTOs) {
        return developmentSeasonConfigDTOs;
    }
}