简体   繁体   English

使用Spring MongoDB模板自定义mongoDB查询

[英]Custom mongoDB query with Spring MongoDB Template

I have this classes: 我有这个课程:

IUserRepository (For CRUD) IUserRepository(对于CRUD)

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

UserRepositoryCustom (For Custom) UserRepositoryCustom(用于自定义)

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

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

And Service for CRUD implementation: 和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);       
    }
}

When I start the server I get this error: 当我启动服务器时,出现此错误:

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

I have the error only when my CRUD interface implements my custom interface too. 仅当我的CRUD接口也实现我的自定义接口时,我才出现错误。 What's wrong? 怎么了?

I think the problem is with the name of the classes and interfaces, when you are using spring data you need to follow some naming rules to be understood correctly by the framework. 我认为问题在于类和接口的名称,当您使用spring数据时,您需要遵循一些命名规则,以使框架正确理解。

This is ok 还行吧

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

this is correct also> 这也是正确的>

public interface UserRepositoryCustom {

but the implementation class has a problem with the name, it should be 但是实现类的名称有问题,应该是

public class IUserRepositoryImpl implements UserRepositoryCustom {

then spring jpa will works and repository will be injected in the Service. 然后spring jpa将起作用,并且存储库将被注入到Service中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM