spring注意事項

1.spring容器與springMVC容器衝突(spring version 4.0.2.RELEASE) php

        SpringMVC容器是Spring容器的一個子容器,它一樣可以初始化實體類。因爲SpringMVC容器的初始化是在Spring容器初始化以後,因此它會替換Spring中已經存在的類,這樣可能會致使衝突web

        (1)只在applicationContext.xml中配置以下spring

<context:component-scan base-package="com.pz" />

                啓動正常,可是任何請求都不會被攔截,簡而言之就是@Controller失效express

        (2)只在spring-servlet.xml中配置上述配置app

                啓動正常,請求也正常,可是事物失效,也就是不能進行回滾code

        (3)在applicationContext.xml和spring-servlet.xml中都配置上述信息component

                啓動正常,請求正常,也是事物失效,不能進行回滾xml

         所以將action單獨配置在一個包中,若是已有項目沒法改變,則用如下配置事務

<!--spring容器 -->
<context:component-scan base-package="com.pz">
	<!-- 不掃描@Controller -->
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>

<!-- springMVC容器 -->
<context:component-scan base-package="com.pz.web">
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>

結論:在spring-servlet.xml中只須要掃描全部帶@Controller註解的類,在applicationContext中能夠掃描全部其餘帶有註解的類(也能夠過濾掉帶@Controller註解的類)。get

引用:由於spring的context是父子容器,因此會產生衝突,Controller會進步前輩行掃描裝配,而此時的Service尚未進行事務的增強處理懲罰,得到的將是原樣的Service(沒有通過事務增強處理懲罰,故而沒有事務處理懲罰才能) ,最後纔是applicationContext.xml中的掃描設備進行事務處理懲罰

相關文章
相關標籤/搜索