SpringMVC整合,出現註解註解沒有起作用

在spring的applicationContext.xml中配置問題

正確的配置:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">
    <!--啓動註解-->
    <context:annotation-config />
    <!--  base-package 註解所在的包根據自己的需要劃分註解類使用的範圍  -->
    <context:component-scan base-package="main.com.talkweb">
        <!--不再管理Controller 註解  因爲它單獨給mvc-servler.xml去管理-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

</beans>

明明有啓動註解,但有些註解還是沒起作用

<!--啓動註解-->
    <context:annotation-config />

後來發現原因是:下面這句作與springMVC的controller註解的排除關注時,把掃描註解的返回侷限了
原來 base-package=”main.com.talkweb.Controller”:

!--  base-package 註解所在的包根據自己的需要劃分註解類使用的範圍  -->
    <context:component-scan base-package="main.com.talkweb.Controller">
        <!--不再管理Controller 註解  因爲它單獨給mvc-servler.xml去管理-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

現在的包範圍 base-package=“main.com.talkweb”:

!--  base-package 註解所在的包根據自己的需要劃分註解類使用的範圍  -->
    <context:component-scan base-package="main.com.talkweb">
        <!--不再管理Controller 註解  因爲它單獨給mvc-servler.xml去管理-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
    </context:component-scan>

剛學習會出現各種各樣的問題,分享出來相互學習

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章