发表于: 2021-04-19 23:22:24
2 1264
今天完成的事情:
解决Service启动报错
明天计划的事情:
上传代码到服务器运行
深度思考
遇到的问题:
一直找不到userService的bean,下面是报错信息
报错:
2021-04-19 22:06:00 CST WARN org.springframework.context.support.AbstractApplicationContext558refresh - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.kbk.service.UserService' available: expected single matching bean but found 2: UserServiceA,UserServiceB
2021-04-19 22:06:00 CST ERROR org.springframework.web.context.ContextLoader313initWebApplicationContext - Context initialization failed org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userController': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.kbk.service.UserService' available: expected single matching bean but found 2: UserServiceA,UserServiceB
==============
==============
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.web.servlet.handler.MappedInterceptor#1': Cannot create inner bean 'com.kbk.util.InterceptUtil#0' of type [com.kbk.util.InterceptUtil] while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.kbk.util.InterceptUtil#0': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.kbk.service.UserService' available: expected single matching bean but found 2: UserServiceA,UserServiceB
=============
==========
1 原因分析:
bean不能创建不能自动装载一定出在配置问题上
2 解决步骤
a 检查 dao层(@param可不写) service层(@Server) controller层(@Controller)有没有加注解 如果没有加上一般就Ok了 如果加了 请看b步骤
b 检查 添加了注解dao层service层controller层对应的包有没有添加进针对这些注解的包扫描中 如果没有 加上就Ok(在spring的配置文件中检查)
(a)打开spring-context.xml配置文件
(b)找到class为org.mybatis.spring.mapper.MapperScannerConfigurer的bean
(c)在name为basePackage的property
(d)在对应的value下添加你的包就行了
如果上述这些步骤还没有解决的话,可以在spring-context.xml配置文件中加入如下语句:
这些都是对的,还是无法找到userService,怀疑是和拆分出去的ServiceA和ServiceB发生了冲突,最后不使用Bean导入, 直接: UserService userService = null;
收获:
导入AOP依赖解决:
<!-- 基于AspectJ的aop依赖 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>${aopalliance.version}</version>
</dependency>
<!-- spring-aop -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${org.springframework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>${org.springframework.version}</version>
</dependency>
出现新的报错:
2021-04-19 13:45:04 CST ERROR com.kbk.domain.ServerDo31main - 报错信息Error creating bean with name 'org.springframework.remoting.rmi.RmiServiceExporter#0' defined in class path resource [spring/spring-mybatis.xml]: Error setting property values; nested exception is org.springframework.beans.PropertyBatchUpdateException; nested PropertyAccessExceptions (1) are:
PropertyAccessException 1: org.springframework.beans.MethodInvocationException: Property 'serviceInterface' threw exception; nested exception is java.lang.IllegalArgumentException: 'serviceInterface' must be an interface
这里不应该写实现类,需要写接口
又恢复了之前的报错,发现是ServerDo 打包的时候并没有启动,手动启动,再来运行,成功连接,登录成功
=================
=================
评论