We Are Going To Discuss About Error creating bean Bean instantiation via constructor failed. So lets Start this Java Article.
Error creating bean Bean instantiation via constructor failed
- Error creating bean Bean instantiation via constructor failed
According to the stacktrace, it seems that the construction of your object fails because of a NullPointerException on line 25:
public TravelportCredentials travelportCredentials= apiAccessConfig.getAPIAccess();
- Error creating bean Bean instantiation via constructor failed
According to the stacktrace, it seems that the construction of your object fails because of a NullPointerException on line 25:
public TravelportCredentials travelportCredentials= apiAccessConfig.getAPIAccess();
Solution 1
According to the stacktrace, it seems that the construction of your object fails because of a NullPointerException on line 25:
public TravelportCredentials travelportCredentials= apiAccessConfig.getAPIAccess();
The reason why you get this exception is that apiAccessConfig
is used before it gets autowired since the field initializations are executed in a phase before Spring processes the @Autowired annotations.
To ensure that apiAccessConfig is initialized before it is used, you can write your HotelSearchConnector class as follows:
package com.GDSWebService.tripnomics_GDS_integration.connector.travelport;
import javax.xml.ws.BindingProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.GDSWebService.tripnomics_GDS_integration.config.travelport.APIAccessConfig;
import com.GDSWebService.tripnomics_GDS_integration.modal.TravelportCredentials;
import com.GDSWebService.tripnomics_GDS_integration.service.HotelSearchImpl;
import com.GDSWebService.tripnomics_GDS_integration.utils.BindingUtil;
import com.travelport.schema.hotel_v48_0.BaseHotelSearchRsp;
import com.travelport.schema.hotel_v48_0.HotelSearchAvailabilityReq;
import com.travelport.service.hotel_v48_0.HotelSearchServicePortType;
import com.travelport.service.hotel_v48_0.HotelService;
@Component
public class HotelSearchConnector {
public APIAccessConfig apiAccessConfig;
public HotelSearchImpl hotelSearchImpl;
public TravelportCredentials travelportCredentials;
public HotelSearchServicePortType hotelShop;
public HotelService hotelShopService;
@Autowired
public HotelSearchConnector(APIAccessConfig apiAccessConfig, HotelSearchImpl hotelSearchImpl) {
this.apiAccessConfig = apiAccessConfig;
this.hotelSearchImpl = hotelSearchImpl;
this.travelportCredentials= apiAccessConfig.getAPIAccess();
this.hotelShopService= new com.travelport.service.hotel_v48_0.HotelService();
this.hotelShop= hotelShopService.getHotelSearchServicePort();
}
public void hotelSearch(HotelSearchAvailabilityReq hsaReq) {
BaseHotelSearchRsp hsaRsp= new BaseHotelSearchRsp();
BindingUtil.addParametersToProvider((BindingProvider)hotelShop, travelportCredentials);
hotelSearchImpl.hotelLowFareSearch(hsaReq, hsaRsp, hotelShop);
}
}
Notice that in my example I’ve replaced field injection with constructor injection.
Original Author Marko Previsic Of This Content
Conclusion
So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.