Spring Boot Configuration类无法在运行时连接ConfigurationProperties

时间:2019-02-27 17:57:44

标签: java spring-boot spring-config

Java 8和Spring Boot 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(注入器)类:

@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类:

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.

为什么会出现此错误?我在哪里错误地注入/配置东西?

2 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:1)

在示例中,您没有将MyAppConfig声明为bean,@ConfigurationProperties并没有使带注释的类成为bean。您可以将其作为MyAppInjector配置的一部分:

@Configuration
public class MyAppInjector {

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

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

}