spring boot启动报出IllegalArgumentException异常
org.springframework.beans.factory.UnsatisfiedDependencyException:
Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userRepository';
nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Invocation of init method failed;
nested exception is java.lang.IllegalArgumentException: Failed to create query for method public abstractcom.liuzeyu.po.Usercom.liuzeyu.dao.UserRepository.findUsernameAndPassword(java.lang.String,java.lang.String)! No property findUsername found for type User!
这个异常重点再最后一个异常 java.lang.IllegalArgumentException
谷歌一下最后一个异常
Failed to create query for method public abstract com.liuzeyu.po.User com.liuzeyu.dao.UserRepository.findUsernameAndPassword(java.lang.String,java.lang.String)! No property findUsername found for type User!
翻译: ----->
无法为方法公共抽象com.liuzeyu.po.User com.liuzeyu.dao.UserRepository.findUsernameAndPassword(java.lang.String,java.lang.String)创建查询! 找不到类型为User的属性findUsername!
琢磨一个晚上无果,在群里请教大佬才得以解决:原来是错在了方法名
findUsernameAndPassword,要符合JPA方法名称规范,详情见:
https://www.cnblogs.com/amberbar/p/10261599.html
原因是:
方法的名称要遵循 findBy + 属性名(首字母大写) + 查询条件(首字母大写 Is Equals)
findByNameLike(String name)
findByName(String name)
findByNameAndAge(String name, Integer age)
findByNameOrAddress(String name) 等...
最后将findUsernameAndPassword改为findByUsernameAndPassword就可以正常运行了。