使用@cacheable时,自动装配的组件变为空

时间:2017-07-10 15:05:43

标签: java spring

当我使用@Cacheable时,自动连线字段变为null并在使用时抛出null pointer exception。删除@Cacheable可以正常运行。

有人可以解释这种行为吗?

@Component
public class SailingPacakgesSearch {

/** The sailing mapper. */
@Autowired
private PacakgesSearchResponseMapper sailingMapper;


/** The packages search request mapper. */
@Autowired
private PacakgesSearchRequestMapper pacakgesSearchRequestMapper;

/** The map common params. */
@Autowired
private MapCommonReqResParameters mapCommonParams;


/** The rest template. */
@Autowired
private RestTemplate restTemplate;

/** The Constant logger. */
@Autowired
private  Log logger;

/**
 * Gets the sailing packges list.
 *
 * @param sailingPackageSearchParameter
 *            the sailing package search parameter
 * @return the sailing packges list
 * @throws ApplicationLevelException
 *             the application level exception
 */
public final AvailableSailingPackages getSailingPackgesList(
        final SailingPackageSearchParameter sailingPackageSearchParameter) throws ApplicationLevelException {

    OtaCruiseSailAvailRQ otaSailRequest = pacakgesSearchRequestMapper
            .mapDXPSearchPackageReqtoSeawareSailAvailRequest(sailingPackageSearchParameter);
    AvailableSailingPackages sailingPackages = new AvailableSailingPackages();
    ParameterizedTypeReference<OtaCruiseSailAvailRS> responseType =
            new ParameterizedTypeReference<OtaCruiseSailAvailRS>() {
    };

    HttpHeaders header = SetMessageConverterAndGetHeader();
    HttpEntity<Object> request = new HttpEntity<>(otaSailRequest, header);
    try {
        ResponseEntity<OtaCruiseSailAvailRS> responseEntity = restTemplate.exchange(otaCruiseSailAvailUrl,
                HttpMethod.POST, request, responseType);
        OtaCruiseSailAvailRS sailingPackageSeaware = responseEntity.getBody();
        sailingPackages.setPackages(sailingMapper.mapSeawareSailingRestoDXPSailingRes(sailingPackageSeaware,
                otaSailRequest.getGuestCounts(), otaSailRequest));
        return sailingPackages;
    } catch (Exception e) {
        handleError(request, otaCruiseSailAvailUrl);
        logger.write(e);
    }
    return sailingPackages;
}


/**
 * Gets the cruise itinerary list.
 *
 * @param selectedSailing
 *            the selected sailing
 * @return the cruise itinerary list
 */

@Cacheable(value = "itinerary", key = "#voyageID", cacheManager = "springCM")
public final Future<ResponseEntity<OtaCruiseItineraryDescRS>> getCruiseItinerary(
        final SelectedSailing selectedSailing, String voyageID) {
    OtaCruiseItineraryDescRQ itineraryRequest = new OtaCruiseItineraryDescRQ();
    itineraryRequest.setSelectedSailing(setSelectedSailing(selectedSailing));
    itineraryRequest.setPos(mapCommonParams.setPOSAgencyDetails());
    itineraryRequest.setVersion(BigDecimal.valueOf(1.0));
    final String uri = otaCruiseItineraryAvailUrl;
    ParameterizedTypeReference<OtaCruiseItineraryDescRS> responseType = 
            new ParameterizedTypeReference<OtaCruiseItineraryDescRS>() {
    };
    AsyncRestTemplate restTemplate = new AsyncRestTemplate();
    List<HttpMessageConverter<?>> messageConverters = new ArrayList<>();
    Jaxb2RootElementHttpMessageConverter jaxbMessageConverter = new Jaxb2RootElementHttpMessageConverter();
    List<MediaType> mediaTypes = new ArrayList<>();
    mediaTypes.add(MediaType.APPLICATION_XML);
    jaxbMessageConverter.setSupportedMediaTypes(mediaTypes);
    messageConverters.add(jaxbMessageConverter);
    restTemplate.setMessageConverters(messageConverters);
    HttpHeaders header = new HttpHeaders();
    header.setContentType(MediaType.APPLICATION_XML);
    HttpEntity<Object> request = new HttpEntity<>(itineraryRequest, header);
    return restTemplate.exchange(uri, HttpMethod.POST, request, responseType);
}

0 个答案:

没有答案