0

I need to provide timeouts from application.properties file, but at initialization it fails because properties are not yet loaded. What is best practice to get them loaded?

@Configuration
@AllArgsConstructor
@Slf4j
public class Config {

    @Value("${connectionTimeout}") 
    int connectionTimeout;
    @Value("${responseTimeout}") 
    int responseTimeout;

    @Bean
    public ClientHttpConnector getConnector() {
        HttpClient client = HttpClient.create();

        client.option(ChannelOption.CONNECT_TIMEOUT_MILLIS, connectionTimeout)
                .responseTimeout(Duration.ofMillis(responseTimeout));

        return new ReactorClientHttpConnector(client);

    }
    @Bean
    public WebClient webClient() {
        return WebClient.builder().defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
                .clientConnector(getConnector())
                .build();
    }

application.properties from resource folder

connectionTimeout=30000
responseTimeout=30000

As suggested in other similar posts I tried using @ConfigurationProperties, but that didn't work at all. Is there some easier way to get them loaded that I am not aware of?

6
  • You could try passing those values as Bean method arguments: public ClientHttpConnector getConnector(@Value("${connectionTimeout}") int connectionTimeout, @Value("${responseTimeout}") int responseTimeout) { .. }
    – Thorne
    Commented Feb 2, 2023 at 12:12
  • What values do I pass to getConnector() when I create WebClient after this as it now requires those two arguments? Commented Feb 2, 2023 at 12:21
  • Also pass the same values to your webClient() bean and then just pass them down to getConnector()
    – Thorne
    Commented Feb 2, 2023 at 12:24
  • It still appears not to load when it is needed -org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'getConnector' defined in ...config.Config: Unsatisfied dependency expressed through method 'getConnector' parameter 0; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "${connectionTimeout}" Commented Feb 2, 2023 at 12:32
  • What happens in your initial case? Are they 0? Are they really in the application.properties. When is this configuration loaded, how are you loading things. Too many questions.
    – M. Deinum
    Commented Feb 2, 2023 at 13:28

2 Answers 2

1

Try injecting the values via constructor:

public Config(@Value("${connectionTimeout}") int connectionTimeout,
              @Value("${responseTimeout}") int responseTimeout) {
    // assign to fields
}
3
  • I still get same type of exceptions, could it be that properties just can't be loaded at initialization if this spring context is loaded as a module in guice? org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'config': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'int'; nested exception is java.lang.NumberFormatException: For input string: "${connectionTimeout}" Commented Feb 2, 2023 at 12:42
  • It says that it cannot convert String to int, which leads me to believe that you are probably not setting your application.properties correctly. Could you please post your application.properties?
    – Thorne
    Commented Feb 2, 2023 at 13:03
  • Edited, but they are same as every other project I had used before Commented Feb 2, 2023 at 13:08
0

Try using Environment class.

@Configuration
public class Config {

   private final Environment environment;

   @Autowired
   public Config(Environment environment) {
       this.environment = environment;
   }

   @Bean
   public SimpleBean simpleBean() {
       SimpleBean simpleBean = new SimpleBean();
       simpleBean.setConfOne(environment.getProperty("conf.one"));
       simpleBean.setConfTwo(environment.getProperty("conf.two"));
       return simpleBean;
   } 
}

Not the answer you're looking for? Browse other questions tagged or ask your own question.