Spring boot @Value аннотация, возвращающая ноль - PullRequest
0 голосов
/ 23 октября 2019

У меня есть класс java, показанный ниже, который создает шаблон отдыха с таймаутами подключения и чтения, а также создает шаблон повторов, который выполняет повторные попытки всякий раз, когда происходит соединение и тайм-аут чтения. Я читаю значения из файла application.properties, но по какой-то причине я получаю нулевые значения для считываемых значений. Я понятия не имею, что можно сделать, чтобы это исправить. Будем благодарны за любые советы по этому вопросу.

public class Retry {

@Value("${read.Timeout.InMilliSeconds:-1}")
private Integer readTimeoutInMilliSeconds;

@Value("${connect.Timeout.InMilliSeconds:-1}")
private Integer connectTimeoutInMilliSeconds;

@Value("${backOff.Period.InMilliSeconds:-1}")
private Integer backOffPeriodInMilliSeconds;

@Value("${max.Attempts:-1}")
private Integer maxAttempts;

@Bean
public RestTemplate restTemplate() {

    RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());
    return restTemplate;
}

private HttpComponentsClientHttpRequestFactory getClientHttpRequestFactory() {

    HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setReadTimeout(readTimeoutInMilliSeconds);
    requestFactory.setConnectTimeout(connectTimeoutInMilliSeconds);
    return requestFactory;
}

@Bean
public RetryTemplate retryTemplate() {

    Map<Class<? extends Throwable>, Boolean> retryableExpressions = new HashMap<>();

    // connection and read timeouts
    retryableExpressions.put(ResourceAccessException.class, true);

    // 404
    retryableExpressions.put(RestClientException.class, false);

    SimpleRetryPolicy simpleRetryPolicy = new SimpleRetryPolicy(maxAttempts, retryableExpressions);

    FixedBackOffPolicy backOffPolicy = new FixedBackOffPolicy();
    backOffPolicy.setBackOffPeriod(backOffPeriodInMilliSeconds);

    RetryTemplate retryTemplate = new RetryTemplate();
    retryTemplate.setRetryPolicy(simpleRetryPolicy);
    retryTemplate.setBackOffPolicy(backOffPolicy);

    return retryTemplate;
}

@Bean
public RetryRestTemplate retryRestTemplate() {
    return new RetryRestTemplate(
            restTemplate(),
            retryTemplate());
   }
}

application.properties

read.Timeout.InMilliSeconds=10000
connect.Timeout.InMilliSeconds=10000
backOff.PeriodInMilliSeconds=10000
max.Attempts=5

трассировка стека

Caused by: java.lang.NullPointerException: null
at com.beans.Retry.getClientHttpRequestFactory(Retry.java:43)
at com.beans.Retry.restTemplate(Retry.java:36)
at com.beans.Services.retryRestTemplate(Services.java:82)
at com.beans.Services$$EnhancerBySpringCGLIB$$819e8a9c.CGLIB$retryRestTemplate$3(<generated>)
at com.beans.Services$$EnhancerBySpringCGLIB$$819e8a9c$$FastClassBySpringCGLIB$$65931da6.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:244)
at org.springframework.context.annotation.ConfigurationClassEnhancer$BeanMethodInterceptor.intercept(ConfigurationClassEnhancer.java:363)
at com.beans.Services$$EnhancerBySpringCGLIB$$819e8a9c.retryRestTemplate(<generated>)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:154)
... 213 common frames omitted

Процесс завершен с кодом выхода 1

RetryRestTemplate

public class RetryRestTemplate {

private RestTemplate restTemplate;
private RetryTemplate retryTemplate;

public RetryRestTemplate(RestTemplate restTemplate, RetryTemplate retryTemplate) {
    this.restTemplate = this.restTemplate;
    this.retryTemplate = this.retryTemplate;
}

public ResponseEntity getForEntity(URI uri, Class c) {
    return retryTemplate.execute(retryContext -> {
        System.out.println("Check");
        return restTemplate.getForEntity(uri, c);
    });
}

public ResponseEntity exchange(String url, HttpMethod get, HttpEntity headers, Class c) {
    return retryTemplate.execute(retryContext -> {
        return restTemplate.exchange(url, get, headers, c);
    });
}

public <T extends Object> ResponseEntity<T> postForEntity(String apiUrl, HttpEntity<Object> entityRequest, Class<T> responseClass) {
    return retryTemplate.execute(retryContext -> {
        return restTemplate.postForEntity(apiUrl, entityRequest, responseClass);
    });
}

}

Ответы [ 4 ]

1 голос
/ 23 октября 2019

Класс Retry возможно не управляется Spring. Добавление @Configuration на Retry class.

@Configuration
public class Retry {
}
0 голосов
/ 23 октября 2019

2 Вещи:
1) Как говорили другие, добавьте конфигурацию
2) Проверьте, используете ли вы Retry как Autowired в любом классе, для которого создается объект. Если в экземпляре объекта используется какой-либо класс bean-компонента, он возвращает ноль.

0 голосов
/ 23 октября 2019

Помимо аннотации @Value, есть другой способ получить значение свойства. С помощью экземпляра интерфейса Environment.

Выполните следующие действия:

  1. Добавьте @Component аннотацию к Retry.java
  2. Импорт и автопроводEnvironment в вашем Retry.java

    import org.springframework.core.env.Environment
    @Autowired
    private Environment env;
    
  3. Получить стоимость имущества от env. Например,

    env.getProperty("read.Timeout.InMilliSeconds","-1");

Существует также несколько других вариантов стоимости имущества. Вы можете проверить это на этом .

0 голосов
/ 23 октября 2019

Попробуйте переместить @Value(...) в соответствующие методы в качестве входных параметров следующим образом:

private HttpComponentsClientHttpRequestFactory getClientHttpRequestFactory(
    @Value("${read.Timeout.InMilliSeconds:-1}") Integer readTimeoutInMilliSeconds,
    @Value("${connect.Timeout.InMilliSeconds:-1}") Integer connectTimeoutInMilliSeconds) {
    ...
}

@Bean
public RetryTemplate retryTemplate(
    @Value("${backOff.Period.InMilliSeconds:-1}") Integer backOffPeriodInMilliSeconds,
    @Value("${max.Attempts:-1}") Integer maxAttempts) {
    ...
}

Или вы также можете попытаться создать конструктор для Retry и использовать @Value для аргументов конструктора:

private Integer readTimeoutInMilliSeconds;
private Integer connectTimeoutInMilliSeconds;
private Integer backOffPeriodInMilliSeconds;
private Integer maxAttempts;

Retry(@Value("${read.Timeout.InMilliSeconds:-1}") Integer readTimeoutInMilliSeconds,
      @Value("${connect.Timeout.InMilliSeconds:-1}") Integer connectTimeoutInMilliSeconds,
      @Value("${backOff.Period.InMilliSeconds:-1}") Integer backOffPeriodInMilliSeconds,
      @Value("${max.Attempts:-1}") Integer maxAttempts) {
    this.readTimeoutInMilliSeconds = readTimeoutInMilliSeconds;
    this.connectTimeoutInMilliSeconds = connectTimeoutInMilliSeconds;
    this.backOffPeriodInMilliSeconds = backOffPeriodInMilliSeconds;
    this.maxAttempts = maxAttempts;
}
...