Spring 启动配置 class 无法在运行时连接 ConfigurationProperties

Spring Boot Configuration class cannot wire ConfigurationProperties at runtime

Java 8 和 Spring 在这里启动 1.5.8。我有以下 application.properties 文件:

logging:
  config: 'logback.groovy'
myapp:
  hystrixTimeoutMillis: 500
  jwt:
    expiry: 86400000
    secret: 12345
  machineId: 12345
spring:
  cache:
    type: none

映射到以下 @ConfigurationProperties POJO:

@ConfigurationProperties(prefix = "myapp")
public class MyAppConfig {
  private Jwt jwt;
  private Long hystrixTimeoutMillis;
  private String machineId;

  public Jwt getJwt() {
    return jwt;
  }

  public void setJwt(Jwt jwt) {
    this.jwt = jwt;
  }

  public Long getHystrixTimeoutMillis() {
    return hystrixTimeoutMillis;
  }

  public void setHystrixTimeoutMillis(Long hystrixTimeoutMillis) {
    this.hystrixTimeoutMillis = hystrixTimeoutMillis;
  }

  public String getMachineId() {
    return machineId;
  }

  public void setMachineId(String machineId) {
    this.machineId = machineId;
  }

  public static class Jwt {
    private Long expiry;
    private String secret;

    public Long getExpiry() {
      return expiry;
    }

    public void setExpiry(Long expiry) {
      this.expiry = expiry;
    }

    public String getSecret() {
      return secret;
    }

    public void setSecret(String secret) {
      this.secret = secret;
    }
  }
}

我有以下 @Configuration(注入器)class:

@Configuration
public class MyAppInjector implements ApplicationContextAware {
  private Logger log = LoggerFactory.getLogger(this.getClass());

  private ApplicationContext applicationContext;

  @Autowired
  private MyAppConfig myAppConfig;

  @Bean
  public AuthService authService(MyAppConfig myAppConfig) {
    return new JwtAuthService(myAppConfig);
  }
}

以及以下 JwtAuthService class:

public class JwtAuthService implements AuthService {
  private static final String BEARER_TOKEN_NAME = "Bearer";

  private Logger log = LoggerFactory.getLogger(this.getClass());

  private MyAppConfig myAppConfig;

  @Autowired
  public JwtAuthService(MyAppConfig myAppConfig) {
    this.myAppConfig = myAppConfig;
  }

  @Override
  public boolean isValidAuthToken(String authToken) {
    return true;
  }
}

启动时出现以下错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field myAppConfig in com.example.myapp.spring.MyAppInjector required a bean of type 'com.example.myapp.spring.MyAppConfig' that could not be found.

Action:

Consider defining a bean of type 'com.example.myapp.spring.MyAppConfig' in your configuration.

为什么我会收到此错误消息? 我在哪里 injecting/configuring 出错了?

Class with @ConfigurationProperties 应该也是bean。您需要将其注释为 @Component 或使用 @Bean 注释在 @Configuration class 中手动注册(而不是尝试在那里自动装配它)

您没有在示例中的任何地方将 MyAppConfig 声明为 bean,@ConfigurationProperties 不会将带注释的 class 设为 bean。您可以将其作为 MyAppInjector 配置的一部分:

@Configuration
public class MyAppInjector {

  @Bean
  public AuthService authService() {
    return new JwtAuthService(myAppConfig());
  }

  @Bean
  public MyAppConfig myAppConfig() {
    return new MyAppConfig();
  }

}