gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 02:53:10 27 4
gpt4 key购买 nike

我正在尝试配置 Spring Data Elasticsearch在我的 Spring MVC 项目中。我正在关注 Java 配置。以下是我建议的 elasticsearch 配置类 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 Autowiring 到 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";
}

}

但 Autowiring 失败给出以下异常:

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

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

最佳答案

我遇到了类似的问题。我认为 Spring 对于具有自引用参数化类型的实体存在问题。就我而言,我有类(class):

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

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

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

关于spring-mvc - 创建 bean 时出错 - 嵌套异常是 java.lang.stackoverflow 错误 spring 数据 elasticsearch 存储库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27124456/

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