Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error creating bean - nested exception is java.lang.stackoverflow error spring data elasticsearch repository

I am trying to configure Spring Data Elasticsearch in my Spring MVC project.I am following Java configuration. Following is my config class for elasticsearch as suggested here.

public class ESConfig {

    @Configuration
    @EnableElasticsearchRepositories(basePackages="com/estore/es/repository")   
    static class ElasticConfig{
        @Bean
        public ElasticsearchOperations elasticsearchTemplate() {            
            return new ElasticsearchTemplate(nodeBuilder().local(true).node().client());
        }

        @Bean
        public NodeBuilder nodeBuilder(){
            final NodeBuilder nodeBuilder = NodeBuilder.nodeBuilder();
            return nodeBuilder;     
        }

        @Bean
        public Node node(){
            return nodeBuilder().node();        
        }

        @Bean
        public Client client(){
            return node().client();
        }
    }

}

I am trying to autowire MerchantRepository into MerchantService.

MerchantRepository.java

public interface MerchantRepository extends ElasticsearchRepository<Merchant, Integer> {

}

MerchantService.java

@Transactional
@Service
public class MerchantService {

    @Autowired
    private MerchantDao merchantDao;

    @Autowired
    private MerchantRepository merchantRepository;

    public String addMerchant(Merchant merchant){   
        merchantRepository.save(merchant);      
        return "success";
    }

}

But Autowiring fails giving following exception:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'merchantService' defined in file [D:\Hemraj-Dev\apache-tomcat-7.0.57\webapps\estore\WEB-INF\classes\com\estore\service\MerchantService.class]: Unsatisfied dependency expressed through constructor argument with index 0 of type [com.estore.es.repository.MerchantRepository]: : Error creating bean with name 'merchantRepository': Invocation of init method failed; nested exception is java.lang.StackOverflowError; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'merchantRepository': Invocation of init method failed; nested exception is java.lang.StackOverflowError
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:751) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:185) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1133) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1036) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:505) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1081) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1006) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:527) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    ... 24 common frames omitted
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'merchantRepository': Invocation of init method failed; nested exception is java.lang.StackOverflowError
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1568) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:540) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:229) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1081) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1006) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:904) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.resolveAutowiredArgument(ConstructorResolver.java:815) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:743) ~[spring-beans-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    ... 37 common frames omitted
Caused by: java.lang.StackOverflowError: null
    at sun.reflect.generics.reflectiveObjects.WildcardTypeImpl.hashCode(WildcardTypeImpl.java:230) ~[na:1.7.0_71]
    at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:330) ~[spring-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.core.ResolvableType.hashCode(ResolvableType.java:811) ~[spring-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.util.ConcurrentReferenceHashMap.getHash(ConcurrentReferenceHashMap.java:214) ~[spring-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.util.ConcurrentReferenceHashMap.getReference(ConcurrentReferenceHashMap.java:246) ~[spring-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.util.ConcurrentReferenceHashMap.get(ConcurrentReferenceHashMap.java:226) ~[spring-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.core.ResolvableType.forType(ResolvableType.java:1207) ~[spring-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.core.ResolvableType.forType(ResolvableType.java:1177) ~[spring-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.core.GenericTypeResolver.resolveType(GenericTypeResolver.java:262) ~[spring-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.data.util.TypeDiscoverer.resolveType(TypeDiscoverer.java:140) ~[spring-data-commons-1.8.4.RELEASE.jar:na]
    at org.springframework.data.util.TypeDiscoverer.createInfo(TypeDiscoverer.java:97) ~[spring-data-commons-1.8.4.RELEASE.jar:na]
    at org.springframework.data.util.ParentTypeAwareTypeInformation.createInfo(ParentTypeAwareTypeInformation.java:70) ~[spring-data-commons-1.8.4.RELEASE.jar:na]
    at org.springframework.data.util.ParameterizedTypeInformation.isResolvedCompletely(ParameterizedTypeInformation.java:207) ~[spring-data-commons-1.8.4.RELEASE.jar:na]
    at org.springframework.data.util.ParameterizedTypeInformation.hashCode(ParameterizedTypeInformation.java:183) ~[spring-data-commons-1.8.4.RELEASE.jar:na]
    at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:330) ~[spring-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.data.util.ParentTypeAwareTypeInformation.hashCode(ParentTypeAwareTypeInformation.java:98) ~[spring-data-commons-1.8.4.RELEASE.jar:na]
    at org.springframework.data.util.ParameterizedTypeInformation.hashCode(ParameterizedTypeInformation.java:183) ~[spring-data-commons-1.8.4.RELEASE.jar:na]
    at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:330) ~[spring-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.data.util.ParentTypeAwareTypeInformation.hashCode(ParentTypeAwareTypeInformation.java:98) ~[spring-data-commons-1.8.4.RELEASE.jar:na]
    at org.springframework.data.util.ParameterizedTypeInformation.hashCode(ParameterizedTypeInformation.java:183) ~[spring-data-commons-1.8.4.RELEASE.jar:na]
    at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:330) ~[spring-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.data.util.ParentTypeAwareTypeInformation.hashCode(ParentTypeAwareTypeInformation.java:98) ~[spring-data-commons-1.8.4.RELEASE.jar:na]
    at org.springframework.data.util.ParameterizedTypeInformation.hashCode(ParameterizedTypeInformation.java:183) ~[spring-data-commons-1.8.4.RELEASE.jar:na]
    at org.springframework.util.ObjectUtils.nullSafeHashCode(ObjectUtils.java:330) ~[spring-core-4.1.1.RELEASE.jar:4.1.1.RELEASE]
    at org.springframework.data.util.ParentTypeAwareTypeInformation.hashCode(ParentTypeAwareTypeInformation.java:98) ~[spring-data-commons-1.8.4.RELEASE.jar:na]
    at org.springframework.data.util.ParameterizedTypeInformation.hashCode(ParameterizedTypeInformation.java:183) ~[spring-data-commons-1.8.4.RELEASE.jar:na]

-------
-------same exception repeated as it is recusrsively making call to each other-------

Any suggestion? I guess I am missing something in configuration.

like image 748
hemu Avatar asked Nov 10 '22 22:11

hemu


1 Answers

I've got similar problem. I think that Spring has problem with entities with parameterized types which are self referencing. In my case, I've got classes:

public class Input<V> {
    private Pipe<V> pipe;
    ...
}

and

public class Pipe<V> {
    private Input<V> input;
    ...
}

which produce same stacktrace as yours. I suggest you to check if your Merchant class, or it's subclass, or one of fields is or has parameterized type.

like image 63
Woland Avatar answered Nov 15 '22 10:11

Woland