Spring Boot Configuration class cannot wire ConfigurationProperties at runtime

hotmeatballsoup :

Java 8 and Spring Boot 1.5.8 here. I have the following application.properties file:

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

Which maps to the following @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;
    }
  }
}

And I have the following @Configuration (injector) 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);
  }
}

And the following 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;
  }
}

At startup I get the following error:

***************************
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.

Why am I getting this error? Where am I injecting/configuring things incorrectly?

Karol Dowbecki :

You are not declaring MyAppConfig as a bean anywhere in your example, @ConfigurationProperties doesn't make annotated class a bean. You can do it as part of MyAppInjector configuration:

@Configuration
public class MyAppInjector {

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

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

} 

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Cannot load configuration class error spring boot

How to mock class with @ConfigurationProperties in Spring Boot

@ConfigurationProperties Spring Boot Configuration Annotation Processor not found in classpath

@ConfigurationProperties Spring Boot Configuration Annotation Processor not found in classpath

How to configure for Spring Boot Configuration Annotation Processor using @ConfigurationProperties on IntelliJ?

Spring boot @ConfigurationProperties not loaded

Spring Boot not loading @ConfigurationProperties

Spring Boot - nesting ConfigurationProperties

Spring Boot ConfigurationProperties issues

Spring boot @ConfigurationProperties not working

Kotlin & Spring Boot @ConfigurationProperties

Spring Boot @ConfigurationProperties

Spring Wire a Static Class

How to wire authentication with Spring Boot

Kotlin Spring boot @ConfigurationProperties for list

ConfigurationProperties outside of spring-boot

Spring Boot 1.5 validated ConfigurationProperties

Spring Boot - @Configuration class is null in spring component

How to use services within a singleton-class Spring Boot (wire up different services with a "usual" class)

Cannot load configuration class: net.guides.springboot2.crud.Application Error in JPA spring boot

Spring multiple @ConfigurationProperties with same class

Multiple configuration tree to one class in spring boot

Spring Boot: Configuration Class is simply ignored and not loaded

How disable scanning @Configuration class in Spring boot

Spring Boot: substitute @Configuration class with another

spring-boot external configuration entire class

Spring Boot how to create Auto configuration class

Spring boot Autowired not working in Configuration class

spring-configuration-metadata.json file is not generated in IntelliJ Idea for Kotlin @ConfigurationProperties class