gpt4 book ai didi

java - 使用 Spring Data ElasticSearch 时出现空指针异常

转载 作者:行者123 更新时间:2023-11-29 08:57:04 25 4
gpt4 key购买 nike

我尝试使用自定义存储库方法的功能(如在 spring data jpa 中)。

我在为 spring 数据 Elasticsearch 配置的包中有存储库接口(interface):

public interface TestrunSetRepository extends ElasticsearchRepository<TestrunSetDocument, String>, TestrunSetRepositoryCustom {

}

我有 TestrunSetRepositoryCustom 的实现

public class TestrunSetRepositoryCustomImpl implements
TestrunSetRepositoryCustom {

private ElasticsearchTemplate elasticsearchTemplate;

public void setElasticsearchTemplate(ElasticsearchTemplate elasticsearchTemplate) {
this.elasticsearchTemplate = elasticsearchTemplate;
}

@Override
public List<String> getUniqueCategoriesDe() {
return null;
}
}

应用程序上下文.xml:

<elasticsearch:repositories base-package="some.repositories.es" />
<elasticsearch:transport-client id="esClient"
cluster-nodes="#{@'some.elasticsearch.cluster.nodes'}"
cluster-name="#{@'some.elasticsearch.cluster.name'}"/>
<bean id="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
<constructor-arg name="client" ref="esClient"/>
</bean>
<bean id="testrunSetRepositoryCustom"
class="some.repositories.es.impl.TestrunSetRepositoryCustomImpl">
<property name="elasticsearchTemplate" ref="elasticsearchTemplate"/>
</bean>

但是在初始化 spring 上下文时我得到了这个异常:

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'testrunSetRepository': FactoryBean threw exception on object creation; nested exception is java.lang.NullPointerException
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1454)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:249)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:270)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
at org.springframework.beans.factory.support.BeanDefinitionValueResolver.resolveReference(BeanDefinitionValueResolver.java:323)
... 63 more
Caused by: java.lang.NullPointerException
at org.springframework.data.elasticsearch.repository.support.MappingElasticsearchEntityInformation.<init>(MappingElasticsearchEntityInformation.java:53)
at org.springframework.data.elasticsearch.repository.support.MappingElasticsearchEntityInformation.<init>(MappingElasticsearchEntityInformation.java:49)
at org.springframework.data.elasticsearch.repository.support.ElasticsearchEntityInformationCreatorImpl.getEntityInformation(ElasticsearchEntityInformationCreatorImpl.java:46)
at org.springframework.data.elasticsearch.repository.query.ElasticsearchQueryMethod.<init>(ElasticsearchQueryMethod.java:42)
at org.springframework.data.elasticsearch.repository.support.ElasticsearchRepositoryFactory$ElasticsearchQueryLookupStrategy.resolveQuery(ElasticsearchRepositoryFactory.java:108)
at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:290)
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:158)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:162)
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:44)
at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142)

最佳答案

自定义类的名称应为“存储库接口(interface)名称”+ Impl,并且该类应实现您的自定义接口(interface)(相同的自定义接口(interface)应由您的存储库接口(interface)扩展)

例子:

//repository interface
public interface ComplexElasticsearchRepository extends ElasticsearchRepository<SampleEntity, String>, ComplexElasticsearchRepositoryCustom {

}

//custom implementation
public class ComplexElasticsearchRepositoryImpl implements ComplexElasticsearchRepositoryCustom {
public String doSomethingSpecial() {
return "2+2=4";
}
}

//custome interface
public interface ComplexElasticsearchRepositoryCustom {
public String doSomethingSpecial();
}

关于java - 使用 Spring Data ElasticSearch 时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19704812/

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