java - 如何实例化封装对象?

标签 java spring-boot sap-commerce-cloud

我目前正在使用 spring。我创建了一个restClient,它使用基于另一个模块的服务。我正在尝试实例化此restClient,但总是收到空指针异常。经过调查,似乎封装的属性(对象)没有实例化,它们的嵌套也没有封装。

这是成员变量、我的类、构造函数和我尝试调用的方法。

private OAuth2RestTemplate oAuth2RestTemplate;
private ConsumedDestinationModel consumedDestinationModel;
@Autowired
private DestinationService<ConsumedDestinationModel> destinationService ;

public DefaultSacRestClient()
{
    consumedDestinationModel = getSacConsumedDestinationModel(getDestinationService().getAllConsumedDestinations());
    oAuth2RestTemplate = configureOAuth2Credentials(getConsumedDestinationModel());
}


@Override
public List<Story> fetchStories() {
    List<Story> stories = new ArrayList<>();

    String endpoint = getConsumedDestinationModel().getUrl() + STORIES_URL;

    stories.add(oAuth2RestTemplate.getForObject(endpoint, Story.class));
    return stories;
}

在我的构造函数中,两种方法 getSacConsumedDestinationModel() & configureOAuth2Credentials() 返回一个对象。

但是方法 getSacConsumedDestinationModel() 使用未实例化的destinationService。我不明白为什么destinationService成员变量没有实例化。

这是仅与我正在处理的模块相对应的应用程序上下文。此应用程序上下文附加到应用程序的全局应用程序上下文

<bean id="defaultSacRestClient" class="de.hybris.platform.sacintegrationbackoffice.client.impl.DefaultSacRestClient">
    <property name="destinationService" ref="destinationService"/>
</bean>

<bean id="destinationService" class="de.hybris.platform.apiregistryservices.services.impl.DefaultDestinationService">
    <property name="destinationDao" ref="destinationDao"/>
</bean>
<bean id="destinationDao" class="de.hybris.platform.apiregistryservices.dao.impl.DefaultDestinationDao">
    <property name="flexibleSearchService" ref="defaultFlexibleSearchService"/>
    <property name="modelService" ref="defaultModelService"/>
</bean>

是否是bean配置问题?我错过了什么吗?我肯定做错了什么。

最佳答案

两个可能的问题:

  1. 该类是否用 @Component@Service 注释?
  2. 有些对象是使用构造函数实例化的,有些则不是。

尝试

@Service
public class DefaultSacRestClient {
    private final OAuth2RestTemplate oAuth2RestTemplate;
    private final ConsumedDestinationModel consumedDestinationModel;
    private final DestinationService<ConsumedDestinationModel> destinationService ;

    public DefaultSacRestClient(DestinationService<ConsumedDestinationModel> destinationService) {
        this.destinationService = destinationService;
        this.consumedDestinationModel = getSacConsumedDestinationModel(destinationService.getAllConsumedDestinations());
        this.oAuth2RestTemplate = configureOAuth2Credentials(getConsumedDestinationModel());
    }
    ...
}

关于java - 如何实例化封装对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57496847/

相关文章:

caching - 如何在 SAP Commerce (hybris) 中创建新的缓存区域

java - 如何将 CharSequence 与字符串进行比较 - Android

java - 如何使用 Spring 启动执行器?官方步骤不起作用

java - 最快次要GC的收集器

java - 如何在 Spring Boot 中注入(inject) Clock.getInstance()?

java - 使用 Wicket 无状态页面时,Bean 未正确注入(inject)另一个 Bean

java - 向现有产品类型添加新属性 - SAP Hybris e-Commerce

java - 如何证明一个Applet是安全的?

java - Spring Boot 宽松绑定(bind)不起作用

java - 带参数的灵活搜索返回空值