使用Spring Data ElasticSearch时出现Nullpointer异常

时间:2013-10-31 11:06:37

标签: java spring spring-data

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

我在包中配置了存储库接口,用于弹簧数据弹性搜索:

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

的applicationContext.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)

1 个答案:

答案 0 :(得分:0)

自定义类的名称应为&#34;存储库接口名称&#34; + Impl和该类应该实现您的自定义接口(相同的自定义接口应该由您的存储库接口扩展)

示例:

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