2015-04-01 100 views
0

我試圖將我的Spring Boot應用程序連接到mongo db follwoing this 教程。這非常簡單,我有一個ScanResults類,當我收到一個post請求時,我試圖將它保存在一個db中。 我在自動裝配上遇到以下錯誤。春季啓動Autowired Mongo存儲庫不能正常工作

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'application': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.gandharv.iiitd.ScanResultsRepository com.gandharv.iiitd.Application.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scanResultsRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.model.MappingException: Could not lookup mapping metadata for domain class java.lang.Object! 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:292) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1185) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482) 
at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:120) 
at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691) 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:320) 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:952) 
at org.springframework.boot.SpringApplication.run(SpringApplication.java:941) 
at com.gandharv.iiitd.Application.main(Application.java:21) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
at java.lang.reflect.Method.invoke(Method.java:606) 
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134) 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: public com.gandharv.iiitd.ScanResultsRepository com.gandharv.iiitd.Application.repository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scanResultsRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.model.MappingException: Could not lookup mapping metadata for domain class java.lang.Object! 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:508) 
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) 
... 21 more 

Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'scanResultsRepository': Invocation of init method failed; nested exception is org.springframework.data.mapping.model.MappingException: Could not lookup mapping metadata for domain class java.lang.Object! 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1553) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:539) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:302) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:1017) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:960) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:858) 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:480) 
... 23 more 
Caused by: org.springframework.data.mapping.model.MappingException: Could not lookup mapping metadata for domain class java.lang.Object! 
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getEntityInformation(MongoRepositoryFactory.java:144) 
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory.getTargetRepository(MongoRepositoryFactory.java:83) 
at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:158) 
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.initAndReturn(RepositoryFactoryBeanSupport.java:224) 
at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.afterPropertiesSet(RepositoryFactoryBeanSupport.java:210) 
at org.springframework.data.mongodb.repository.support.MongoRepositoryFactoryBean.afterPropertiesSet(MongoRepositoryFactoryBean.java:108) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.invokeInitMethods(AbstractAutowireCapableBeanFactory.java:1612) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1549) 
... 33 more 

ScanResults.java

public class ScanResults { 

@Id 
private String id; 

public ScanResults() {} 

public String getLongitude() { 
    return longitude; 
} 

public String getLatitude() { 
    return latitude; 
} 

public ArrayList<WifiScans> getWifiscan() { 
    return wifiscan; 
} 

public void setLatitude(String latitude) { 
    this.latitude = latitude; 
} 

public void setLongitude(String longitude) { 
    this.longitude = longitude; 
} 

public void setWifiscan(ArrayList<WifiScans> wifiscan) { 
    this.wifiscan = wifiscan; 
} 

private String latitude; 
private String longitude; 
public ArrayList<WifiScans> wifiscan = new ArrayList<WifiScans>(); 




} 

倉庫接口

package com.gandharv.iiitd; /** 
* Created by GandharvK on 4/1/2015. 
*/ 

import com.gandharv.iiitd.ScanResults; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import org.springframework.data.mongodb.repository.MongoRepository; 
import org.springframework.data.mongodb.repository.config.EnableMongoRepositories; 
import org.springframework.stereotype.Repository; 

public interface ScanResultsRepository extends MongoRepository { 

ScanResults find(WifiScans obj); 
} 

Application.java

package com.gandharv.iiitd; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; 
import org.springframework.context.annotation.ComponentScan; 
import org.springframework.context.annotation.Configuration; 
import com.gandharv.iiitd.ScanResultsRepository; 
/** 
* Created by GandharvK on 3/27/2015. 
*/ 
@Configuration 
@EnableAutoConfiguration 
@ComponentScan 
public class Application { 

    @Autowired 
    public ScanResultsRepository repository; 

    public static void main(String[] args) { 
     SpringApplication.run(Application.class, args); 
    } 
} 

的pom.xml

<parent> 
    <groupId>org.springframework.boot</groupId> 
    <artifactId>spring-boot-starter-parent</artifactId> 
    <version>1.1.5.RELEASE</version> 
</parent> 

<groupId>com.gandharvkapoor</groupId> 
<artifactId>spring-wifi-scan</artifactId> 
<version>1.0-SNAPSHOT</version> 

<dependencies> 
    <dependency> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-web</artifactId> 
    </dependency> 
    <!-- Spring Data MongoDB--> 
    <dependency> 
     <groupId>org.springframework.data</groupId> 
     <artifactId>spring-data-mongodb</artifactId> 
    </dependency> 
</dependencies> 

<build> 
    <plugins> 
     <!-- Spring Boot Maven Support --> 
     <plugin> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-maven-plugin</artifactId> 
     </plugin> 
    </plugins> 
</build> 

回答

0

好吧,我在倉庫註釋掉find方法解決它。我想它是強制實施接口的所有方法。

1

您不必實施存儲庫方法。這在幕後完成。問題是,你有沒有提供一個模板參數到倉庫:

public interface ScanResultsRepository extends MongoRepository<ScanResults, WifiScans> { 
} 

如果WifiScans是一個序列化的對象,是ScanResults對象的ID,你甚至不必指定find方法。您可以在其他許多方法中使用ScanResults findOne(WifiScans w)List<ScanResults> findAll(WifiScans w)方法(請參閱MongoRepository和CrudRepository接口)