為什么Spring和SpringMVC要單獨(dú)做包掃描工作

bingo

SpringMVC作為IOC容器使用和Spring作為IOC容器有什么區(qū)別呢。為什么有的項(xiàng)目中分別配置了Spring和SpringMVC的包掃描配置?

  • 其實(shí)注冊(cè)了兩個(gè)容器,Spring作為父容器,SpringMVC作為子容器。SpringMVC容器內(nèi)的Bean可以訪問父容器的Bean,即:SpringMVC內(nèi)的Bean可以使用@Autowire注入Spring容器管理的Bean,反之不行。
  • 一般Spring負(fù)責(zé)除去@Controller之外的Bean管理工作,SpringMVC負(fù)責(zé)@Controller的Bean管理工作。
  • 理由:Spring的擴(kuò)展性,如果要是項(xiàng)目需要加入Struts等可以整合進(jìn)來,便于擴(kuò)展框架。如果要是為了快,為了方便開發(fā),完全可以用SpringMVC框架,再有如果存在事務(wù)管理,springMVC未整合事物控制則可能出現(xiàn)事物無效情況。

Spring作為IOC容器的配置

web.xml配置Spring監(jiān)聽器

<!--加載Spring的配置文件到上下文中去,可以不配置默認(rèn)是classpath下的applicationContext.xml-->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        classpath:applicationContext.xml
    </param-value>
</context-param>
<!-- Spring監(jiān)聽器,監(jiān)聽servletContext啟動(dòng) -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

applicationContext配置

<!-- 自動(dòng)掃描指定包,過濾@Controller,則Spring容器不包含@Controller注解的Bean -->
<context:component-scan base-package="cn.orcish">
    <!-- 掃描時(shí)跳過 @Controller 注解的JAVA類(控制器) -->
    <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>

SpringMVC作為IOC容器的配置

web.xml配置SpringMVC servlet

 <servlet>
    <servlet-name>orcish</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:orcish-servlet.xml</param-value>
    </init-param>
    <!-- 容器初始化加載此servlet -->
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>orcish</servlet-name>
    <!--
        一般有以下寫法:
        *.do    攔截固定格式的請(qǐng)求
        /       rest風(fēng)格的寫法:攔截所有資源,需要針對(duì)靜態(tài)資源做單獨(dú)處理
        /*      錯(cuò)誤寫法:會(huì)在處理完請(qǐng)求后攔截jsp導(dǎo)致404錯(cuò)誤
     -->
    <url-pattern>/</url-pattern>
</servlet-mapping>

dispatcher-servlet(orcish-servlet.xml)配置

<!-- 使用基于注解的控制器,spring會(huì)自動(dòng)掃描base-package下的子包和類,如果掃描到會(huì)把這些類注冊(cè)為bean-->
<!--<context:component-scan base-package="cn.orcish"/>-->
<context:component-scan base-package="cn.orcish">
    <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!--<context:annotation-config/>-->
<!--json支持-->
<mvc:annotation-driven/>

<!--  配置處理映射器和處理器適配器 在Spring4.0后,不配置,會(huì)默認(rèn)加載-->
 <!--<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping"/>-->
<!-- <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"/> -->
<!--  會(huì)自動(dòng)注冊(cè)RequestMappingHandlerMapping與RequestMappingHandlerAdapter兩個(gè)Bean  -->
<!--<mvc:annotation-driven/>-->
<!-- 配置視圖解析器,經(jīng)過視圖解析器后,視圖的的完成路徑為[prefix]+返回的視圖字符串+[suffix] -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/WEB-INF/view/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容