创建bean时出错 - 嵌套异常是java.lang.stackoverflow错误弹簧数据elasticsearch存储库

时间:2014-11-25 10:41:30

标签: spring-mvc elasticsearch spring-data spring-data-elasticsearch

我正在尝试在我的Spring MVC项目中配置Spring Data Elasticsearch。我正在遵循Java配置。以下是我建议的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();
        }
    }

}

我正在尝试将MerchantRepository自动装入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";
    }

}

但自动装配未能给出以下异常:

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

有什么建议吗?我想我在配置中遗漏了一些东西。

2 个答案:

答案 0 :(得分:1)

我有类似的问题。我认为Spring对具有自引用参数化类型的实体有问题。在我的情况下,我已经上课了:

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

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

与您的堆栈跟踪产生相同的堆栈跟踪。我建议你检查你的Merchant类,或者它的子类,或者其中一个字段是否具有参数化类型。

答案 1 :(得分:0)

我得到了同样的错误,但在不同的Web应用程序和不同的bean创建当然(我假设您的应用程序也是一个Web应用程序),我的应用程序是onebusaway / oba。以下是在Windows 7开发机器上的Eclipse Kepler中启动tomcat服务器时出现的错误:

Error creating bean with name 'transitGraphDaoImpl': Invocation of init method failed; 
nested exception is java.lang.StackOverflowError

我相信“由于深度递归,抛出此错误表示应用程序的堆栈已耗尽。”阅读source

解决方案:

  1. 双击Eclipse Kepler IDE的Servers选项卡中的现有tomcat 7服务器,将打开配置窗口
  2. 点击“打开启动配置”
  3. 点击“参数”标签
  4. 粘贴此行(根据您的环境进行相应更改)

    -Dcatalina.base="C:\Program3\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2
    -Dcatalina.home="C:\Program3\tomcat7" 
    -Dwtp.deploy="C:\Program3\workspace\.metadata\.plugins\org.eclipse.wst.server.core\tmp2\wtpwebapps
    -Djava.endorsed.dirs="C:\Program3\tomcat7\endorsed" -Xms256m -Xmx1g -XX:MaxPermSize=2g
    
  5. 单击“应用”和“确定”,然后再次在“Eclipse服务器”选项卡中停止并启动服务器。

  6. 它对我有用。

    注意:

    这里要记住的关键是:

    1. 在我的情况下,我遇到了tomcat的内存问题
    2. 因此我增加了这样的内存大小-Xms256m -Xmx1g -XX:MaxPermSize=2g我有16gig可用。要了解Java内存设置,请阅读this
    3. 希望它有所帮助。