此文幾處有引用別人部落格內容,如有侵權表示歉意!3、spring 容器和spring mvc 容器之間的關係:
spring 在整體框架的核心概念中,容器思想是其核心思想。 在乙個專案應用中容器的個數可以是多個,spring中可以包含多個容器,其中容器之間存在層級關係。其中spring容器和spring mvc 容器就是兩個容器,spring容器作為根容器,spring mvc 容器作為子容器,並且父容器可以看到子容器的bean,子容器中不能看到父容器中的bean.這是spring自己定義的規則。
①在 spring 配置xml檔案時 配置元件掃瞄包,該功能掃瞄包下所有的@component註解,並且自動注入到容器中,@service、@controller、@repository繼承自@component。spring配置主要是為了掃瞄@service和@repository註解。配置如下:
在專案中為解決上訴容器之間的關係有如下三種方式:
(推薦)在spring 配置掃瞄包指定@service、@repository所在包的路徑,並在spring mvc 配置掃瞄包的指定@controller所在包的路徑,當然在spring配置掃瞄時也可以加入@controller的掃瞄,但生成了此bean會一直儲存在spring容器中不進行使用,對資源照成浪費。總結:spring mvc容器中必須包含@controller層級bean,否則會請求會包404錯誤
protected void inithandlermethods()
string beannames =(this.detecthandlermethodsinancestorcontexts ?
beanfactoryutils.beannamesfortypeincludingancestors(
for (string beanname : beannames)
}handlermethodsinitialized(gethandlermethods());
}
方法ishandler()會判斷當前的bean是否為controller。
protected
boolean
ishandler(class> beantype)
name="detecthandlermethodsinancestorcontexts">
truevalue>
property>
bean>
3.可以讓spring 容器和spring mvc容器各司其職,spring 容器只負責非@controller的掃瞄註冊,spring mvc 容器只進行@controller相關bean 的掃瞄註冊
--spring容器配置,排除所有@controller的bean
context:component-scan>
--springmvc容器配置,讓其只包括@controller的bean
use-default-filters="false">
context:component-scan>
Spring 父容器與子容器
一 spring容器 父容器 2 service物件 二 springmvc 前端控制器 子容器 controller物件 1.標準的配置是這樣的 controller只在springmvc的配置檔案掃瞄。service等只在spring容器的配置檔案配置掃瞄。2.spring中也可以掃瞄contr...
spring與springMVC父子容器的坑
前言 在整合spring和springmvc這兩個框架中,spring作為父容器 一般用來管理controller層 springmvc作為子容器。為什麼要用到兩個容器呢?因為spring的擴充套件性,所以保留了spring的父容器配置 而且子容器是可以呼叫父容器物件的,反之不行。但是今天遇到乙個問...
Spring學習(三) 初探Spring容器
首先,所有被spring管理的物件都是生存在spring容器 container 中的,spring負責建立物件 裝配物件 配置物件並管理它們的整個生命週期。容器是spring的核心,spring通過依賴注入管理構成spring應用的元件,包括它們之間的關聯,spring自帶了多個容器實現,歸結起來...