0

My first time to use Spring-MVC and Mongodb . When I deploy the webapp and start server (Tomcat 8) I'am getting the following error :

[ERROR] [localhost-startStop-1 06:26:09] (ContextLoader.java:initWebApplicationContext:307) Context initialization failed org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.zhwei.Service.UserService com.zhwei.controller.UserController.userservice; 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.zhwei.Repository.UserRepository com.zhwei.Service.UserService.userrepository; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': FactoryBean threw exception on object creation; nested exception is java.lang.IllegalArgumentException: No property user found for type class com.zhwei.domain.User at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:287) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1106) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:517) at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456) at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294) at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225) at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291) at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193) at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585) at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913) at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464) at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:384) at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:283) at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:111) at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4739) at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5159) at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:726) at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:702) at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:697) at org.apache.catalina.startup.HostConfig.deployDirectory(HostConfig.java:1134) at org.apache.catalina.startup.HostConfig$DeployDirectory.run(HostConfig.java:1780) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:471) 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) 

I don't understand why 'No property user found for type class com.zhwei.domain.User'. Any idea?How dose this error occur?

This is the User class:

package com.zhwei.domain; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.DBRef; import org.springframework.data.mongodb.core.mapping.Document; @Document public class User { @Id private String id; private String firstName; private String lastName; private String username; private String password; @DBRef private Role role; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getUsername() { return username; } public void setUsername(String userName) { this.username = userName; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public Role getRole() { return role; } public void setRole(Role role) { this.role = role; } } 

This is the repository :

package com.zhwei.Repository; import org.springframework.data.mongodb.repository.MongoRepository; import com.zhwei.domain.User; public interface UserRepository extends MongoRepository<User,String> { User findByUserName(String username); } 

Here is the UserService :

package com.zhwei.Service; import java.util.List; import java.util.UUID; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import com.zhwei.Repository.RoleRepository; import com.zhwei.Repository.UserRepository; import com.zhwei.domain.User; @Service public class UserService { @Autowired private UserRepository userrepository; @Autowired private RoleRepository roleRepository; // we must save,update or delete both separately since there is no cascading // feature // in Spring Data MongoDB public User create(User user) { user.setId(UUID.randomUUID().toString()); user.getRole().setId(UUID.randomUUID().toString()); roleRepository.save(user.getRole()); return userrepository.save(user); } public User read(User user) { return user; } public List<User> readAll() { return userrepository.findAll(); } public User update(User user) { User existingUser = userrepository.findByUserName(user.getUsername()); if (existingUser == null) { return null; } existingUser.setFirstName(user.getFirstName()); existingUser.setLastName(user.getLastName()); existingUser.setRole(user.getRole()); roleRepository.save(existingUser.getRole()); return userrepository.save(existingUser); } public boolean delete(User user) { User existingUser = userrepository.findByUserName(user.getUsername()); if (existingUser == null) { return false; } roleRepository.delete(existingUser.getRole()); userrepository.delete(existingUser); return true; } } 

1 Answer 1

1

This error is caused by the findByUserName method on the UserRepository class. If what you want is the username property of the User object, the N letter should be lowercase.

findByUserName ---> user.getUser().getName(); <--- No property user found for type class com.zhwei.domain.User findByUsername ---> user.getUsername(); 

Read this doc if you want more information on the method parser.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.