一、报错信息
org.springframework.dao.InvalidDataAccessApiUsageException: 查询用户名不能为空! should not be empty; nested exception is java.lang.IllegalArgumentException: 查询用户名不能为空!should not be empty
at org.springframework.orm.jpa.EntityManagerFactoryUtils.convertJpaAccessExceptionIfPossible(EntityManagerFactoryUtils.java:373)
at org.springframework.orm.jpa.vendor.HibernateJpaDialect.translateExceptionIfPossible(HibernateJpaDialect.java:255)
at org.springframework.orm.jpa.AbstractEntityManagerFactoryBean.translateExceptionIfPossible(AbstractEntityManagerFactoryBean.java:527)
at org.springframework.dao.support.ChainedPersistenceExceptionTranslator.translateExceptionIfPossible(ChainedPersistenceExceptionTranslator.java:61)
at org.springframework.dao.support.DataAccessUtils.translateIfNecessary(DataAccessUtils.java:242)
at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:153)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
at com.jusdaglobal.imservice.infrastructure.persistence.dialog.impl.UserDialogRepositoryImpl$$EnhancerBySpringCGLIB$$34ed6c18.getUserDialogByPage(<generated>)
at com.jusdaglobal.imservice.application.service.impl.DialogServiceImpl.getUserDialogByPage(DialogServiceImpl.java:113)
at com.jusdaglobal.imservice.application.service.impl.DialogServiceImpl$$FastClassBySpringCGLIB$$ff048e66.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:684)
at com.jusdaglobal.imservice.application.service.impl.DialogServiceImpl$$EnhancerBySpringCGLIB$$b540c68f.getUserDialogByPage(<generated>)
at com.jusdaglobal.imservice.interfaces.api.rest.controller.dialog.DialogController.getUserDialogByPage$original$lCly1inl(DialogController.java:55)
at com.jusdaglobal.imservice.interfaces.api.rest.controller.dialog.DialogController.getUserDialogByPage$original$lCly1inl$accessor$PBV7l7IZ(DialogController.java)
at com.jusdaglobal.imservice.interfaces.api.rest.controller.dialog.DialogController$auxiliary$0OlFwrfa.call(Unknown Source)
at org.apache.skywalking.apm.agent.core.plugin.interceptor.enhance.InstMethodsInter.intercept(InstMethodsInter.java:93)
at com.jusdaglobal.imservice.interfaces.api.rest.controller.dialog.DialogController.getUserDialogByPage(DialogController.java)
at com.jusdaglobal.imservice.interfaces.api.rest.controller.dialog.DialogController$$FastClassBySpringCGLIB$$d5b314ba.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.validation.beanvalidation.MethodValidationInterceptor.invoke(MethodValidationInterceptor.java:119)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.adapter.MethodBeforeAdviceInterceptor.invoke(MethodBeforeAdviceInterceptor.java:56)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
......
二、问题描述
看来上面的报错是不是觉得有点奇怪,是不是发现错误提示语句重复了。开始我也挺纳闷的,代码里面只是做一个查询参数的断言处理,然后统一异常处理类将错误提示语句提示给前端,这下好了,返回的提示语句,用户看起来就更奇怪了。仔细观察这两个错误,发现这个两个错误其中一个是断言里面的错误信息,另一个错误却是spring事务所报的错,但代码里并没有加入任何事务,我就奇怪了。经过几番折腾与验证,终于发现我在类上错误的使用了@Repositroy注解,因为该注解的作用不只是将类识别为Bean,同时它还能将所标注的类中抛出的数据访问异常封装为 Spring 的数据访问异常类型。将@Repositroy注解修改为@Component问题自然就解决了
三、总结
这看上去这是个很简单的问题,对整体代码也不会造成大的bug,但却反映出了对spring框架的掌握程度不够,没有深入的去理解这个框架。以前也只是会用就行,并没有仔细去研究它的底层是怎么实现。借此这个错误,鞭策自己去深入的学习理解spring框架。