我正在使用hibernate-validator-5.2.2.Final jar进行spring验证。当我在类路径中添加validation-api-1.1.0.Final jar文件时,会发生以下异常:
WARNING: Exception encountered during context initialization - cancelling refresh attempt
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userDetailsService': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.database.dao.UsersDAO com.layers.configuration.MyUsersDetailsService.userDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userdao': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private org.hibernate.SessionFactory com.database.dao.UserDaoImplementation.sessionFactory; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sessionFactory' defined in com.layers.configuration.SpringConfiguration: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.hibernate.SessionFactory]: Factory method 'sessionFactory' threw exception; nested exception is java.lang.NoClassDefFoundError: javax/validation/ValidatorFactory
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:835)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537)
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4727)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5167)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1408)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1398)
at java.util.concurrent.FutureTask.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
我的模特课是:
public class Users implements java.io.Serializable {
/**
*
*/
private static final long serialVersionUID = -8605158460377236476L;
private int UId;
@Size(min=3, max=15)
@NotNull
private String userName;
@NotNull
@Size(min = 4, max = 15)
private String userPassword;
@NotNull
@Size(min=3, max=20)
private String firstName;
@NotNull
@Size(min=3, max=20)
private String lastName;
@JsonIgnore
private List<UsersRoles> usersRoleNames;
public Users() {
}
public Users(int UId) {
this.UId = UId;
}
public Users(int UId, String userName, String userPassword, String firstName, String lastName,
List<UsersRoles> usersRoleNames) {
this.UId = UId;
this.userName = userName;
this.userPassword = userPassword;
this.firstName = firstName;
this.lastName = lastName;
this.usersRoleNames = usersRoleNames;
}
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
@Column(name = "u_id", unique = true, nullable = false)
public int getUId() {
return this.UId;
}
public void setUId(int UId) {
this.UId = UId;
}
@Column(name = "user_name")
public String getUserName() {
return this.userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
@Column(name = "user_password")
public String getUserPassword() {
return this.userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
}
@Column(name = "first_name")
public String getFirstName() {
return this.firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
@Column(name = "last_name")
public String getLastName() {
return this.lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
@OneToMany(fetch = FetchType.LAZY, mappedBy = "usersTable")
public List<UsersRoles> getUsersRoleNames() {
return this.usersRoleNames;
}
public void setUsersRoleNames(List<UsersRoles> usersRoleNames) {
this.usersRoleNames = usersRoleNames;
}
}
我的控制器:
@RequestMapping(value = "/saveuser", method = RequestMethod.POST)
public @ResponseBody String Save(/*@Valid Users user, BindingResult result,*/HttpServletRequest request, Model model,
@RequestParam(required = false) Boolean reverseBeforeSave) {
if (result.hasErrors() || user.getUserName() == null || user.getUserPassword() == null
|| user.getUId() == 0 || user.getFirstName() == null || user.getLastName() == null) {
return "error";
}
else
{
String userName = request.getParameter("username");
String password = request.getParameter("password");
String firstName = request.getParameter("first_name");
String lastName = request.getParameter("last_name");
System.out.println("save method");
String[] roleNames = request.getParameterValues("roles");
userService.saveUserandRoles(userName, password, firstName, lastName, roleNames);
return "success";
//}
}
请让我知道问题是什么以及如何解决,我已经阅读了有关此问题的各种教程和错误,但无法理解。
答案 0 :(得分:0)
在 pom.xml
中添加以下依赖项docker-compose up