SpringMvc自動裝配@Controller無效

lonecloud發表於2017-04-09

1.問題原因:SpringMvc驅動器沒有掃描該Controller層

雖然配置了

1     <!-- 啟用spring mvc 註解 -->
2     <context:annotation-config />
3 
4     <!-- 設定使用註解的類所在的jar包 -->
5     <context:component-scan base-package="cn.lonecloud.controller"></context:component-scan>

但是由於沒有啟用mvc驅動器導致其自動裝配失敗

解決辦法:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">

    <mvc:annotation-driven />
    <mvc:default-servlet-handler />
    <!-- 啟用spring mvc 註解 -->
    <context:annotation-config />

    <!-- 設定使用註解的類所在的jar包 -->
    <context:component-scan base-package="cn.lonecloud.controller"></context:component-scan>
</beans>

新增

<mvc:annotation-driven />
    <mvc:default-servlet-handler />和mvc的schema檔案即可開啟驅動

相關文章