我第一次使用Spring-MVC和Mongodb。 当我部署webapp并启动服务器(Tomcat 8)时,我收到以下错误:
[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)
我不明白为什么没有为类型类com.zhwei.domain.User'找到属性用户。有什么想法?这个错误发生的剂量是多少?
这是User类:
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;
}
}
这是存储库:
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);
}
这是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;
}
}
答案 0 :(得分:0)
此错误是由findByUserName
类上的UserRepository
方法引起的。如果你想要的是User对象的username属性,那么N字母应该是小写的。
findByUserName ---> user.getUser().getName(); <--- No property user found for type class com.zhwei.domain.User
findByUsername ---> user.getUsername();
如果您想了解有关方法解析器的更多信息,请阅读此doc。