gpt4 book ai didi

java - 通过构造函数创建 bean 时出错 Bean 实例化失败

转载 作者:行者123 更新时间:2023-11-30 05:32:49 24 4
gpt4 key购买 nike

通过构造函数实例化 Bean 失败;嵌套异常是org.springframework.beans.BeanInstantiationException:无法实例化构造函数抛出异常;嵌套异常是java.lang.NullPointerException

此类中抛出异常:

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 {

@Autowired
public APIAccessConfig apiAccessConfig;
@Autowired
public HotelSearchImpl hotelSearchImpl;

public TravelportCredentials travelportCredentials= apiAccessConfig.getAPIAccess();

public HotelSearchServicePortType hotelShop;
public HotelService hotelShopService;

@Autowired
public HotelSearchConnector() {
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);
}
}

并且根据堆栈跟踪,该类在第 25 行中被引用

package com.GDSWebService.tripnomics_GDS_integration.config.travelport;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;

import com.GDSWebService.tripnomics_GDS_integration.modal.TravelportCredentials;

@Configuration
@PropertySource("classpath:application.properties")
public class APIAccessConfig {

private TravelportCredentials tpc= new TravelportCredentials();

@Value("${GDS.travelport.endpoint}")
private String endpoint;
@Value("${GDS.travelport.username}")
private String username;
@Value("${GDS.travelport.password}")
private String password;
@Value("${GDS.travelport.targetbranch}")
private String targetBranch;


public TravelportCredentials getAPIAccess() {
// tpc= new TravelportCredentials();

tpc.setEndpoint(endpoint);
tpc.setPassword(password);
tpc.setTargetBranch(targetBranch);
tpc.setUsername(username);

return tpc;
}

}

完整的堆栈跟踪:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2019-07-25 15:47:09.528 ERROR 7820 --- [ restartedMain] o.s.boot.SpringApplication : Application run failed

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hotelSearchConnector' defined in file [C:\Users\TravelSpends\Documents\Tripnomics\tripnomics-GDS-integration\target\classes\com\GDSWebService\tripnomics_GDS_integration\connector\travelport\HotelSearchConnector.class]: Bean instantiation via constructor failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.GDSWebService.tripnomics_GDS_integration.connector.travelport.HotelSearchConnector]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:304) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:171) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1341) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1187) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:555) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:515) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:320) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:318) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:849) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:877) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:549) ~[spring-context-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:142) ~[spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:775) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:397) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:316) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1260) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1248) [spring-boot-2.1.4.RELEASE.jar:2.1.4.RELEASE]
at com.GDSWebService.tripnomics_GDS_integration.App.main(App.java:26) [classes/:na]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_201]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_201]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_201]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_201]
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49) [spring-boot-devtools-2.1.4.RELEASE.jar:2.1.4.RELEASE]
Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.GDSWebService.tripnomics_GDS_integration.connector.travelport.HotelSearchConnector]: Constructor threw exception; nested exception is java.lang.NullPointerException
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:184) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:117) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
at org.springframework.beans.factory.support.ConstructorResolver.instantiate(ConstructorResolver.java:300) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
... 24 common frames omitted
Caused by: java.lang.NullPointerException: null
at com.GDSWebService.tripnomics_GDS_integration.connector.travelport.HotelSearchConnector.<init>(HotelSearchConnector.java:25) ~[classes/:na]
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_201]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) ~[na:1.8.0_201]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) ~[na:1.8.0_201]
at java.lang.reflect.Constructor.newInstance(Constructor.java:423) ~[na:1.8.0_201]
at org.springframework.beans.BeanUtils.instantiateClass(BeanUtils.java:172) ~[spring-beans-5.1.6.RELEASE.jar:5.1.6.RELEASE]
... 26 common frames omitted

BaseHotelSearchRsp.java

package com.travelport.schema.hotel_v48_0;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.bind.annotation.XmlType;
import com.travelport.schema.common_v48_0.BaseSearchRsp;
import com.travelport.schema.common_v48_0.HostToken;
import com.travelport.schema.common_v48_0.MarketingInformation;


/**
*
* Base hotel Search Response
*
*
* <p>Java class for BaseHotelSearchRsp complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType name="BaseHotelSearchRsp"&gt;
* &lt;complexContent&gt;
* &lt;extension base="{http://www.travelport.com/schema/common_v48_0}BaseSearchRsp"&gt;
* &lt;sequence&gt;
* &lt;element name="ReferencePoint" type="{http://www.travelport.com/schema/hotel_v48_0}typeHotelReferencePoint" minOccurs="0"/&gt;
* &lt;element ref="{http://www.travelport.com/schema/hotel_v48_0}HotelSearchResult" maxOccurs="999" minOccurs="0"/&gt;
* &lt;element ref="{http://www.travelport.com/schema/common_v48_0}MarketingInformation" minOccurs="0"/&gt;
* &lt;element ref="{http://www.travelport.com/schema/common_v48_0}HostToken" minOccurs="0"/&gt;
* &lt;/sequence&gt;
* &lt;attribute name="AddressSearchQuality" type="{http://www.w3.org/2001/XMLSchema}int" /&gt;
* &lt;/extension&gt;
* &lt;/complexContent&gt;
* &lt;/complexType&gt;
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "BaseHotelSearchRsp", propOrder = {
"referencePoint",
"hotelSearchResult",
"marketingInformation",
"hostToken"
})
@XmlSeeAlso({
RetrieveHotelSearchAvailabilityRsp.class,
HotelSearchAvailabilityAsynchRsp.class
})
public class BaseHotelSearchRsp
extends BaseSearchRsp
{

@XmlElement(name = "ReferencePoint")
protected TypeHotelReferencePoint referencePoint;
@XmlElement(name = "HotelSearchResult")
protected List<HotelSearchResult> hotelSearchResult;
@XmlElement(name = "MarketingInformation", namespace = "http://www.travelport.com/schema/common_v48_0")
protected MarketingInformation marketingInformation;
@XmlElement(name = "HostToken", namespace = "http://www.travelport.com/schema/common_v48_0")
protected HostToken hostToken;
@XmlAttribute(name = "AddressSearchQuality")
protected Integer addressSearchQuality;

/**
* Gets the value of the referencePoint property.
*
* @return
* possible object is
* {@link TypeHotelReferencePoint }
*
*/
public TypeHotelReferencePoint getReferencePoint() {
return referencePoint;
}

/**
* Sets the value of the referencePoint property.
*
* @param value
* allowed object is
* {@link TypeHotelReferencePoint }
*
*/
public void setReferencePoint(TypeHotelReferencePoint value) {
this.referencePoint = value;
}

/**
* Gets the value of the hotelSearchResult property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the hotelSearchResult property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getHotelSearchResult().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link HotelSearchResult }
*
*
*/
public List<HotelSearchResult> getHotelSearchResult() {
if (hotelSearchResult == null) {
hotelSearchResult = new ArrayList<HotelSearchResult>();
}
return this.hotelSearchResult;
}

/**
* Gets the value of the marketingInformation property.
*
* @return
* possible object is
* {@link MarketingInformation }
*
*/
public MarketingInformation getMarketingInformation() {
return marketingInformation;
}

/**
* Sets the value of the marketingInformation property.
*
* @param value
* allowed object is
* {@link MarketingInformation }
*
*/
public void setMarketingInformation(MarketingInformation value) {
this.marketingInformation = value;
}

/**
* Gets the value of the hostToken property.
*
* @return
* possible object is
* {@link HostToken }
*
*/
public HostToken getHostToken() {
return hostToken;
}

/**
* Sets the value of the hostToken property.
*
* @param value
* allowed object is
* {@link HostToken }
*
*/
public void setHostToken(HostToken value) {
this.hostToken = value;
}

/**
* Gets the value of the addressSearchQuality property.
*
* @return
* possible object is
* {@link Integer }
*
*/
public Integer getAddressSearchQuality() {
return addressSearchQuality;
}

/**
* Sets the value of the addressSearchQuality property.
*
* @param value
* allowed object is
* {@link Integer }
*
*/
public void setAddressSearchQuality(Integer value) {
this.addressSearchQuality = value;
}

}

请帮助大家,我是 Spring Boot 的新手

最佳答案

根据堆栈跟踪,对象的构造似乎因第 25 行的 NullPointerException 而失败:

public TravelportCredentials travelportCredentials= apiAccessConfig.getAPIAccess();

出现此异常的原因是 apiAccessConfig 在 Autowiring 之前使用,因为字段初始化是在 Spring 处理 @Autowired 注解之前的一个阶段执行的。

为了确保apiAccessConfig在使用之前被初始化,您可以按如下方式编写HotelSearchConnector类:

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);
}
}

请注意,在我的示例中,我已替换 field injectionconstructor injection .

关于java - 通过构造函数创建 bean 时出错 Bean 实例化失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57200189/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com