Question

I have this classes:

IUserRepository (For CRUD)

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

UserRepositoryCustom (For Custom)

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

UserRepositoryImpl (Custom 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:

@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. What's wrong?

Was it helpful?

Solution

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.

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top