我正在尝试在我的 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();
}
}
}
我正在尝试自动连接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-------
有什么建议吗?我想我在配置中遗漏了一些东西。