2014-02-09 17 views
1

我有這個類:定製MongoDB的查詢與Spring MongoDB的模板

IUserRepository(對於CRUD)

@Transactional 
public interface IUserRepository extends MongoRepository<User, String>, 
    UserRepositoryCustom { 
} 

UserRepositoryCustom(對於自定義)

public interface UserRepositoryCustom { 
public User getUserByEmail(String email); 
public User getUserByEmailAndPassword(String email, String password); 
public List<User> getOnlineUsers(); 
} 

UserRepositoryImpl(自定義IMPL)

public class UserRepositoryImpl { 

    @Autowired 
    private MongoTemplate mongoTemplate; 

    public User getUserByEmail(String email) { 
     Query searchUserQuery = new Query(Criteria.where("email").is(email)); 
     return mongoTemplate.findOne(searchUserQuery, User.class); 
    } 

    public User getUserByEmailAndPassword(String email, String password) { 
     Query searchUserQuery = new Query(Criteria.where("email").is(email) 
       .andOperator(Criteria.where("password").is(password))); 
     return mongoTemplate.findOne(searchUserQuery, User.class); 
    } 

    public List<User> getOnlineUsers() { 
     Query searchUserQuery = new Query(Criteria.where("online").is(true)); 
     return mongoTemplate.find(searchUserQuery, User.class); 
    } 
} 

和服務CRUD實現:

@Repository("userService") 
@Transactional 
public class UserService { 

    @Autowired 
    private IUserRepository userRepository; 

    public List<User> getAll() { 
     return userRepository.findAll(); 
    } 

    public User getUser(String deviceRegistrationID){ 
     return userRepository.findOne(deviceRegistrationID); 
    } 

    public void addUser(User user){ 
     userRepository.save(user); 
    } 

    public User editUser(User user){ 

     User findUser = getUser(user.getDeviceRegistrationID()); 
     findUser.setLatitude(user.getLatitude()); 
     findUser.setLongitude(user.getLongitude()); 
     findUser.setOnline(true); 

     return userRepository.save(findUser);  
    } 
} 

當我開始我得到這個錯誤服務器:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'messageController': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.extendsme.service.IUserRepository com.extendsme.service.UserService.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IUserRepository': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property get found for type com.extendsme.model.User 
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:306) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:628) 
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:932) 
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:479) 
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:389) 
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:294) 
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:112) 
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4961) 
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5455) 
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) 
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) 
    at java.util.concurrent.FutureTask.run(FutureTask.java:262) 
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145) 
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615) 
    at java.lang.Thread.run(Thread.java:744) 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.extendsme.service.IUserRepository com.extendsme.service.UserService.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IUserRepository': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property get found for type com.extendsme.model.User 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:289) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1146) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:519) 
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:458) 
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:296) 
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:223) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:293) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:198) 
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.autowireResource(CommonAnnotationBeanPostProcessor.java:444) 
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.getResource(CommonAnnotationBeanPostProcessor.java:418) 
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor$ResourceElement.getResourceToInject(CommonAnnotationBeanPostProcessor.java:546) 
    at org.springframework.beans.factory.annotation.InjectionMetadata$InjectedElement.inject(InjectionMetadata.java:150) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    at org.springframework.context.annotation.CommonAnnotationBeanPostProcessor.postProcessPropertyValues(CommonAnnotationBeanPostProcessor.java:303) 
    ... 22 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.extendsme.service.IUserRepository com.extendsme.service.UserService.userRepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IUserRepository': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property get found for type com.extendsme.model.User 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:517) 
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:286) 
    ... 35 more 
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'IUserRepository': FactoryBean threw exception on object creation; nested exception is org.springframework.data.mapping.PropertyReferenceException: No property get found for type com.extendsme.model.User 
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:149) 
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.getObjectFromFactoryBean(FactoryBeanRegistrySupport.java:102) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getObjectForBeanInstance(AbstractBeanFactory.java:1468) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:307) 
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:912) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:855) 
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770) 
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:489) 
    ... 37 more 
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property get found for type com.extendsme.model.User 
    at org.springframework.data.mapping.PropertyPath.<init>(PropertyPath.java:74) 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:325) 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:351) 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:351) 
    at org.springframework.data.mapping.PropertyPath.create(PropertyPath.java:305) 
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:269) 
    at org.springframework.data.mapping.PropertyPath.from(PropertyPath.java:240) 
    at org.springframework.data.repository.query.parser.Part.<init>(Part.java:75) 
    at org.springframework.data.repository.query.parser.PartTree$OrPart.<init>(PartTree.java:189) 
    at org.springframework.data.repository.query.parser.PartTree$Predicate.buildTree(PartTree.java:279) 
    at org.springframework.data.repository.query.parser.PartTree$Predicate.<init>(PartTree.java:259) 
    at org.springframework.data.repository.query.parser.PartTree.<init>(PartTree.java:68) 
    at org.springframework.data.mongodb.repository.query.PartTreeMongoQuery.<init>(PartTreeMongoQuery.java:47) 
    at org.springframework.data.mongodb.repository.support.MongoRepositoryFactory$MongoQueryLookupStrategy.resolveQuery(MongoRepositoryFactory.java:128) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport$QueryExecutorMethodInterceptor.<init>(RepositoryFactorySupport.java:304) 
    at org.springframework.data.repository.core.support.RepositoryFactorySupport.getRepository(RepositoryFactorySupport.java:161) 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:162) 
    at org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport.getObject(RepositoryFactoryBeanSupport.java:44) 
    at org.springframework.beans.factory.support.FactoryBeanRegistrySupport.doGetObjectFromFactoryBean(FactoryBeanRegistrySupport.java:142) 
    ... 45 more 

我有錯誤,只有當我的CRUD接口實現我的自定義界面了。 有什麼問題?

回答

2

我認爲問題是類和接口的名稱,當你使用彈簧數據時,你需要遵循一些命名規則,以便框架能夠正確理解。

這是確定

public interface IUserRepository extends MongoRepository<User, String>, UserRepositoryCustom { 

這是正確的也是>

public interface UserRepositoryCustom { 

但實現類有名稱的問題,它應該是

public class IUserRepositoryImpl implements UserRepositoryCustom { 

然後春天JPA將工作和存儲庫將被注入服務。

+0

多數民衆贊成在此,謝謝 – colymore