5

我正在尝试在我的 Spring MVC 项目中配置Spring Data Elasticsearch。我正在关注 Java 配置。以下是我在这里建议的 elasticsearch 配置类。

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

}

我正在尝试自动连接MerchantRepositoryMerchantService.

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

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

4

2 回答 2

1

我有类似的问题。我认为 Spring 在具有自引用的参数化类型的实体方面存在问题。就我而言,我有课程:

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

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

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

于 2014-12-22T15:15:12.547 回答
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

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

解决方案:

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

    -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 Servers 选项卡中停止并启动您的服务器。

这个对我有用。

笔记:

这里要记住的关键是:

  1. 就我而言,我对tomcat的内存有疑问
  2. 因此我像这样增加了内存大小,-Xms256m -Xmx1g -XX:MaxPermSize=2g我有 16gig 可用。要了解 Java 内存设置,请阅读

希望能帮助到你。

于 2015-05-28T05:03:38.660 回答