1. 程式人生 > 其它 >Servlet.init() for servlet [springmvc] threw exception The server encountered an unexpected condition that prevented it from fulfilling the request.

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,廢話不多說,上解決方案。

  1. 先檢查一下自己的service層、controller層是否添加了註解

    //service層
    @Service
    public class ItemServiceImpl implements ItemService {
    @Autowired
    private ItemMapper itemMapper;

    @Override
    public Item findById(int id) {
    return itemMapper.findById(id);
    }
    }

    //controller層
    @Controller
    @RequestMapping("/item")
    public class ItemController {
    @Autowired
    private ItemService itemService;

    @RequestMapping("/showItem/{id}")
    public String findById(@PathVariable("id")int id,Model model){
    Item item = itemService.findById(id);
    model.addAttribute("item",item);
    return "item";
    }
    }
  2. 如果加了註解還報錯,那可能是配置檔案中的包掃描出錯了

    <!--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>