Spring启动-@组件类中返回null的@Value

Spring启动-@组件类中返回null的@Value,spring,spring-boot,spring-mvc,spring-profiles,spring-autoconfiguration,Spring,Spring Boot,Spring Mvc,Spring Profiles,Spring Autoconfiguration,这是一个简单的类,我试图用@value映射错误消息 我没有在属性文件中添加相应的值,因为我已经设置了默认值 @Slf4j @Component public class RestTemplateResponseErrorHandler implements ResponseErrorHandler { @Value("${invalid.password:test}") private String invalidPassword;

这是一个简单的类,我试图用@value映射错误消息

我没有在属性文件中添加相应的值,因为我已经设置了默认值

@Slf4j
@Component
public class RestTemplateResponseErrorHandler
        implements ResponseErrorHandler {

    @Value("${invalid.password:test}")
    private String invalidPassword;


    @Override
    public boolean hasError(ClientHttpResponse httpResponse)
            throws IOException {

        return (
                httpResponse.getStatusCode().series() == CLIENT_ERROR
                        || httpResponse.getStatusCode().series() == SERVER_ERROR);
    }

    @Override
    public void handleError(ClientHttpResponse httpResponse)
            throws IOException {

        if (httpResponse.getStatusCode()
                .series() == SERVER_ERROR) {
            log.info("RestTemplateResponseErrorHandler --- SERVER_ERROR");
            // handle SERVER_ERROR
        } else if (httpResponse.getStatusCode()
                .series() == CLIENT_ERROR) {
            log.info("RestTemplateResponseErrorHandler --- CLIENT_ERROR");

            if (httpResponse.getStatusCode() == HttpStatus.BAD_REQUEST)
                throw new UnAuthorisedRequestException(invalidPassword);


        }
    }
}
这里抛出新的UnauthorizedRequestException(invalidPassword);返回空值

不确定该类有什么问题,因为它被标记为@Component

@Configuration
public class CommonBeanConfig {

    @Bean
    public RestTemplate restTemplate(SSLSocketFactory sslSocketFactory) {
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(new SSLConnectionSocketFactory(sslSocketFactory, new NoopHostnameVerifier()))
                .build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(requestFactory));
        restTemplate.setInterceptors(Collections.singletonList(new ClientLoggingInterceptor()));
        restTemplate.setErrorHandler(new RestTemplateResponseErrorHandler());
        return restTemplate;
    }

}

该类作为异常处理程序添加

RestTemplateResponseErrorHandler
,在您的示例中,它不是由spring管理的-您可以“手动”创建它。因此spring不能对它无法管理的对象使用
@Value

请尝试以下操作:

@Configuration
public class CommonBeanConfig {

    @Bean
    public RestTemplateResponseErrorHandler restTemplateErrorHandler() {
      return new RestTemplateErrorHandler();
    }
    @Bean
    public RestTemplate restTemplate(SSLSocketFactory sslSocketFactory) {
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(new SSLConnectionSocketFactory(sslSocketFactory, new NoopHostnameVerifier()))
                .build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(requestFactory));
        restTemplate.setInterceptors(Collections.singletonList(new ClientLoggingInterceptor()));
        restTemplate.setErrorHandler(restTemplateErrorHandler()); // or inject the parameter into the method public RestTemplate restTemplate(SSLSocketFactory sslSocketFactory, RestTemplateErrorHandler restTemplateErrorHandler) 
        return restTemplate;
    }

}

注意,因为您已经在使用java配置,所以不必放置@Component 在
RestTemplateErrorHandler
类上

如果您确实需要使用
@Component
并希望通过组件扫描找到并实例化它,请不要在java配置中为错误处理程序创建bean,而使用我在
restTemplate
的注释中提到的方法:

@Bean
public RestTemplate restTemplate(SSLSocketFactory sslSocketFactory, RestTemplateErrorHandler restTemplateErrorHandler) {
   ...
}


RestTemplateResponseErrorHandler
在您的示例中不是由spring管理的-您可以“手动”创建它。因此spring不能对它无法管理的对象使用
@Value

请尝试以下操作:

@Configuration
public class CommonBeanConfig {

    @Bean
    public RestTemplateResponseErrorHandler restTemplateErrorHandler() {
      return new RestTemplateErrorHandler();
    }
    @Bean
    public RestTemplate restTemplate(SSLSocketFactory sslSocketFactory) {
        CloseableHttpClient httpClient = HttpClients.custom()
                .setSSLSocketFactory(new SSLConnectionSocketFactory(sslSocketFactory, new NoopHostnameVerifier()))
                .build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        RestTemplate restTemplate = new RestTemplate(new BufferingClientHttpRequestFactory(requestFactory));
        restTemplate.setInterceptors(Collections.singletonList(new ClientLoggingInterceptor()));
        restTemplate.setErrorHandler(restTemplateErrorHandler()); // or inject the parameter into the method public RestTemplate restTemplate(SSLSocketFactory sslSocketFactory, RestTemplateErrorHandler restTemplateErrorHandler) 
        return restTemplate;
    }

}

注意,因为您已经在使用java配置,所以不必放置@Component 在
RestTemplateErrorHandler
类上

如果您确实需要使用
@Component
并希望通过组件扫描找到并实例化它,请不要在java配置中为错误处理程序创建bean,而使用我在
restTemplate
的注释中提到的方法:

@Bean
public RestTemplate restTemplate(SSLSocketFactory sslSocketFactory, RestTemplateErrorHandler restTemplateErrorHandler) {
   ...
}


请同时发布配置文件(.properties,.yaml)。可能是这样的:如果您正在使用lombok,则有可能是您导入了lombok的
@Value
,而不是
spring
检查了您导入的包。请发布配置文件(.properties,.yaml)同样。可能是这样的:如果您正在使用lombok,则有可能您已经导入了lombok的
@Value
,而不是
spring
检查您导入的包。