Servlet.init() for servlet [springmvc] threw exception The server encountered an unexpected condition that prevented it from fulfilling the request.
5.异常显示:
刚学了maven的分模块开发,上手试了一个小案例,结果被狠狠的上了一课。
Message
Servlet.init() for servlet [springmvc] threw exception
The server encountered an unexpected condition that prevented it from fulfilling the request.
Root Cause
Error creating bean with name 'itemController': Unsatisfied dependency expressed through field 'itemService';
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'itemServiceImpl': Unsatisfied dependency expressed through field 'itemMapper';
nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.itheima.ssm.dao.ItemMapper'
available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
相信大家也看出来了,第一是 itemController 不能依赖到 itemService ,第二是 itemServiceImol 不能依赖到 itemMapper,废话不多说,上解决方案。
先检查一下自己的service层、controller层是否添加了注解
//service层
如果加了注解还报错,那可能是配置文件中的包扫描出错了
<!--applicationContext-dao.xml 配置文件-->
<!--配置数据源信息,使用druid连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>
<!--配置spring整合mybatis框架的SQLSessionFactoryBean-->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<!--扫描pojo包,为实体类创建别名-->
<property name="typeAliasesPackage" value="com.itheima.ssm.pojo"/>
</bean>
<!--mapper扫描器,用于产生代理对象-->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="com.itheima.ssm.dao"/>
</bean>
<!--applicationContenxt-service.xml配置文件-->
<!--配置扫描器,扫描Service-->
<context:component-scan base-package="com.itheima.ssm.service"/>
<!--事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"/>
</bean>
<!--事物注解驱动-->
<tx:annotation-driven transaction-manager="transactionManager"/>
<!--springmvc.xml配置文件-->
<!--配置扫描器,扫描Controller-->
<context:component-scan base-package="com.itheima.ssm.controller"/>
<!--视图解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>3.到这里基本就没什么大问题了,但bug可不会这么简单的放过你,因为我还是执行不了。于是就百度啊百度,终于让我找到 了。因为分模块开发的原因,每个模块是相互独立的,但又彼此依赖,配置文件也是如此,所以写完对应层的配置文件后需要在依赖的文件中导入被依赖的文件。
<!--service层配置文件中导入dao层配置文件解决依赖问题-->
<import resource="applicationContext-dao.xml"/>
<!--controlle层配置文件中导入service层配置文件,结局依赖不到问题-->
<import resource="applicationContext-service.xml"/>
4.到这问题就基本解决了,但是悲催的我还有一个bug
Error creating bean with name 'dataSource' defined in class path resource [applicationContext-dao.xml]: Initialization of bean failed;
nested exception is org.springframework.beans.ConversionNotSupportedException: Failed to convert property value of type 'java.lang.String' to required type 'java.sql.Driver' for property 'driver';
nested exception is java.lang.IllegalStateException: Cannot convert value of type 'java.lang.String' to required type 'java.sql.Driver' for property 'driver': no matching editors or conversion strategy found由于不细心导致的数据源错误
因为用的是阿里的druid数据源,所以drive应该写为driveClassName
<!--配置数据源信息,使用druid连接池-->
<bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
//错误写法
<property name="driver" value="com.mysql.jdbc.Driver"/>
//正确写法
<property name="driveClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/ssm"/>
<property name="username" value="root"/>
<property name="password" value="root"/>
</bean>如还是有错的话,那检查一下url路径中的数据库名是否拼写错误,别问我怎么知道的,因为我踩坑了.....
小提示:子模块相互依赖可以使用dependcy标签导入模块名即可,如controll层依赖service层中的配置文件
<dependencies>
<dependency>
<groupId>com.itheima</groupId>
<artifactId>maven_service</artifactId>
<version>1.0-SNAPSHOT</version>
</dependency>
</dependencies>

浙公网安备 33010602011771号