背景及问题
今天在学习有关maven分模块构建工程时,将简单的spring+springmvc+mybatis工程拆分为web模块,service模块,dao模块,以及domain模块。其中service和dao模块有各自的spring配置文件。web模块有对应的spring-mvc配置文件。
完成相应模块的常规代码编写后,访问对应资源,执行控制层代码时遇到报错,无法创建service层的bean实例:
org.springframework.context.support.AbstractApplicationContext.refresh Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘itemController’: Unsatisfied dependency expressed through field ‘itemService’; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type ‘cn.yansw.service.ItemService’ available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
注解使用没有问题,对应的spring配置文件是正确的,组件扫描没有问题。
解决方案
经检查测试,通过修改web模块的web.xml配置中Spring配置文件的路径得以解决:
原路径:classpath:applicationContext*.xml
修改后路径:classpath*:applicationContext*.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
<!--指定Spring配置文件位置-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext*.xml</param-value>
</context-param>
<!--配置Spring框架启动时使用的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!--配置SpringMVC的前端控制器-->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--指定springmvc配置文件位置-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
</web-app>
若classpath后缺少*,则无法跨模块取得对应的spring配置文件。也就造成无法创建bean实例,从而出现报错。
项目结构
- domain模块封装从数据库查询得到的实体类
- dao模块实现mybatis配置查询数据库
- service模块通过调用对应的dao模块完成业务逻辑
- web模块包括前端的数据提交、数据展示页面,以及对应的控制层代码
总结与反思
在分模块构建工程时,需要注意是否需要跨模块读取配置文件,在需要的位置classpath后要加*号。