SpringMvc自動裝配@Controller無效


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文件即可開啟驅動


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM