org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘itemController1’: Unsatisfied dependency expressed through field ‘itemService’;
各模块直接的项目依赖确定没有问题,但是没有办法创建itemController1的bean对象,在往下翻发现造成原因是 itemService创建有问题
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.itheima.service.ItemService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
找了好久发现是自己进行分层模块开发后,service层有自己的配置文件,controller层也有自己的配置文件,在家controller层的web.xml中,配置加载配置文件路径时出了问题
原因1:此种配置情况只能加载到controller层中resource中的配置文件,所以service在容器中才会出现NoSuchBeanDefinitionException的问题,因为根本没有加载其配置文件也就没有在容器创建ItemService的Bean,无法进行注入。
原因2:后续打包后,各模块的配置文件全部都存在于各自模块形成的jar包中,此时classpath:只会读取类加载路径下的配置文件,而没有办法读取在jar包中的配置文件
解决方案:
class*:详细解读连接
使用classpath*: 除了能够加载classess文件下的配置文件,还会扫描jar包,并加载其中的配置文件
该项目用tomcat部署后,可以发现controller模块打包后,会有其他三个模块的jar存在,各自模块的配置文件就在各自的jar包中