1

我正在尝试读取属性文件中的值。为什么我在输出中得到值:[]?

abc.env[0].envname=test
abc.env[0].grant_type=password

abc.env[1].envname=dev
abc.env[1].grant_type=password

下面是我要执行的课程

@SpringBootApplication
public class AbcApplication implements CommandLineRunner{

    private static Logger LOG = LoggerFactory.getLogger(AbcApplication.class);

    @Autowired
    ReadAbcApplicationProperties readAbcApplicationProperties;
    
    public static void main(String[] args) {
        SpringApplication.run(AbcApplication.class, args);
    }
    
    @Override
    public void run(String... args) {
        LOG.info("values: {}", readAbcApplicationProperties.getAbcSources());
    }

}
@Component
@ConfigurationProperties(prefix = "abc")
public class ReadAbcApplicationProperties {

    private List<AbcProperties> abcSources = new ArrayList<AbcProperties>();

    @Autowired
    ReadAbcApplicationProperties readAbcApplicationProperties;

    public List<AbcProperties> getAbcSources() {
        return abcSources;
    }

    public void setAbcSources(List<AbcProperties> abcSources) {
        this.abcSources = abcSources;
    }
    
}
public class AbcProperties {

    private String envname;
    private String tokenGrantType;

    public String getEnvname() {
        return envname;
    }
    public void setEnvname(String envname) {
        this.envname = envname;
    }
    public String getTokenGrantType() {
        return tokenGrantType;
    }
}

有人可以帮我解决缺失的部分吗?

4

2 回答 2

2

您的代码中有一些不一致的地方..

  • ReadAbcApplicationProperties- 为什么你注入自己?

     @Autowired
     ReadAbcApplicationProperties readAbcApplicationProperties;
    

    必须删除此行。

  • 字段与属性文件中private String tokenGrantType的不一致abc.env[0].grant_type

  • private List<AbcProperties> abcSources与属性文件中的abc.env[0]and abc.env[1]and ...不一致abc.env[X]

课程应该是:

AbcProperties

public class AbcProperties {

  private String envname;
  private String grantType; //to match `grant_type` in .properties file
  
  //getters setters
}

ReadAbcApplicationProperties

@ConfigurationProperties(prefix = "abc")
public class ReadAbcApplicationProperties {

    private List<AbcProperties> env = new ArrayList<>(); //to match abc.env[X] in properties file

    // getters setters
}

我在 spring-boot 示例项目中尝试了上述方法并且它有效 在此处输入图像描述

于 2021-10-13T15:44:52.897 回答
1

您在属性文件和相应的 bean 类中有不同的属性名称AbcProperties。见tokenGrantTypegrant_type

理想的配置是:

很抱歉使用 yml 文件而不是属性,但它的行为都是一样的。它只yml会给我们更多的可读性。


myapp: # this can be your prefix
  abc:
    - 
      envname: test
      grant_type: password
    - 
      envname: dev
      grant_type: password

将“myapp”作为前缀(顶级键),可以让您在单个类中添加任意数量的属性,如下面的“ConfigPropertiesBinder”。

@Data // If not, include getter and setters, with cons.
@RefreshScope
@Configuration
@ConfigurationProperties(prefix = "myapp")
public class ConfigPropertiesBinder {

    private List<AbcProperties> abc; // Should match with myapp.abc

  // Other properties
    
}
  @Data
  public class AbcProperties{
   private String envname; // Match with myapp.abc[x].envname
   private String grantType; // '-' and '_' converts to camelcase
  }

看看有没有帮助。

于 2021-10-13T16:10:56.560 回答