父级缺少嵌套的@ConfigurationProperties class

Nested @ConfigurationProperties missing from parent class

我有一个有点复杂的应用程序配置,我试图将其映射到 POJO,但它不起作用。我查看了示例并认为这是正确的方法。

application.properties:

app.activeEnv=dev
debug=true
logging.level.com.nationwide.ess.contentcollector=DEBUG

app.nas1Root=/Users/userid/devl/ICC_DEV
app.nas2Root=ohlewnas0130.nwie.net

task.triggerCron=* * * * * *

task.job[0].name=CCC
task.job[0].monitor[0]=${app.nas1Root}/Claims/CCC/index
task.job[0].monitor[1]=${app.nas2Root}/Folder/CCC/index
task.job[0].processed=${app.nas2Root}/Processed/CCC_Claims
task.job[0].errors=${app.nas1Root}/Errors/CCC_Claims
task.job[0].triggerTimes[0]=15:55
task.job[0].triggerTimes[1]=16:00

task.job[1].name=CIF
task.job[1].monitor[0]=${app.nas1Root}/Policy/CIF_OV
task.job[1].processed=${app.nas1Root}/Processed/CIF_Dist
task.job[1].errors=${nas1Root}/Errors/CIF_Dist
task.job[1].triggerTimes[0]=12:00

ApplicationProperties.java:

package com.nationwide.ess.contentcollector.config;

import java.util.List;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;

import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@NoArgsConstructor
@ConfigurationProperties(prefix="app")
public class ApplicationProperties {
  private String activeEnv;
  private String nas1Root;
  private String nas2Root;

  @Bean
  @ConfigurationProperties(prefix = "task")
    public static Task task() {
    return new Task();
  }
  
  @Data
  @NoArgsConstructor
  public static class Task {
    private String triggerCron;
    private List<Job> job;

    @Data
    @NoArgsConstructor
    public static class Job {
      private String name;

      private List<String> monitor;

      private String processed;

      private String errors;

      private List<String> triggerTimes;
    }
  }
}

ContentCollectorApplication.java:

package com.nationwide.ess.contentcollector;

import com.nationwide.ess.contentcollector.config.ApplicationProperties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.scheduling.annotation.EnableScheduling;

@SpringBootApplication
@EnableScheduling
@EnableConfigurationProperties(ApplicationProperties.class)
public class ContentCollectorApplication {
  static final Logger log = LoggerFactory.getLogger(SpringApplication.class);

  public static void main(String[] args) {
    SpringApplication.run(ContentCollectorApplication.class, args);
  }

}

但是当它运行时,ApplicationProperties 只有“app”级别的值(没有任务和 task.job)。

Task class 移动到它自己的文件中,然后每当您需要这些属性时,您只需声明对 TaskProperties Spring-managed 的​​依赖bean 以便 Spring 注入它(假设它依赖于另一个 Spring-managed bean):

@Data
@NoArgsConstructor
@ConfigurationProperties(prefix = "task")
public class TaskProperties {
  private String triggerCron;
  private List<Job> job;

  @Data
  @NoArgsConstructor
  public static class Job {
    private String name;
    private List<String> monitor;
    private String processed;
    private String errors;
    private List<String> triggerTimes;
  }
}

因为我不太确定静态嵌套Job class,如果前面的不行,那就试试把Job移到一个自己的文件里如下:

@Data
@NoArgsConstructor
@ConfigurationProperties(prefix = "task")
public class TaskProperties {
  private String triggerCron;
  private List<Job> job;
}
@Data
@NoArgsConstructor
public static class Job {
  private String name;
  private List<String> monitor;
  private String processed;
  private String errors;
  private List<String> triggerTimes;
}