銆銆Spring2.0涓柊澧炰簡(jiǎn)涓涓敤鏉ヨ〃紺築ean鑼冨洿鐨凷cope鎺ュ彛
銆銆public聽interface聽Scope聽{
銆銆Object聽get(String聽name,聽ObjectFactory聽objectFactory);//鏍規(guī)嵁鍚嶇О鍙?qiáng)鍒涘缓宸ュ巶寰楀堫C竴涓狟ean瀹炰緥
銆銆Object聽remove(String聽name);//鍒犻櫎涓涓寚瀹氬悕縐扮殑Bean
銆銆}
銆銆鍦ㄥ鍣–onfigurableBeanFactory鎺ュ彛涓畾涔変簡(jiǎn)Bean宸ュ巶鏈夊叧Scope娉ㄥ唽鐨勭浉鍏蟲柟娉曪紝浣垮緱鍙線Bean宸ュ巶涓姞鍏ユ柊綾誨瀷鐨凚ean銆?br />銆銆public聽interface聽ConfigurableBeanFactory聽extends聽HierarchicalBeanFactory,
銆銆void聽registerScope(String聽scopeName,聽Scope聽scope);//寰Bean宸ュ巶涓坊鍔犱竴涓柊鐨勮寖鍥?榛樿鍙湁涓ょ鑼冨洿錛歴ingleton鍙?qiáng)prototype)
銆銆void聽destroyScopedBean(String聽beanName);//閿姣丅聽ean宸ュ巶涓寖鍥碆ean
銆銆}
銆銆鍦ˋbstractFactoryBean鐨刧etBean鏂規(guī)硶涓疄鐜頒簡(jiǎn)瀵圭壒瀹歋cope聽Bean鏀寔錛屾牳蹇?jī)浠g爜鎽樿锛?xì)
聽聽聽聽String聽scopeName聽=聽mergedBeanDefinition.getScope();//鍙栧緱褰撳墠Bean鐨勮寖鍥達(dá)紝涔熷嵆鍦ㄥ畾涔変腑鐨剆cope=鈥漴equest鈥濈殑閮ㄥ垎銆?/span>
聽聽聽聽Scope聽scope聽=聽(Scope)聽this.scopes.get(scopeName);//寰楀埌Bean宸ュ巶涓殑鑼冨洿澶勭悊鍣?/span>
聽聽聽聽if聽(scope聽==聽null)聽{
聽聽聽聽聽throw聽new聽I(yíng)llegalStateException("No聽Scope聽registered聽for聽scope聽'"聽+聽scopeName聽+聽"'");
聽聽聽聽}
聽聽聽聽try聽{
//浣跨敤scope.get(beanName,ObjectFactory)浠庢寚瀹氱殑鑼冨洿涓緱鍒版垨鍒涘緩Bean瀹炰緥
聽聽聽聽聽Object聽scopedInstance聽=聽scope.get(beanName,聽new聽ObjectFactory()聽{
聽聽聽聽聽聽public聽Object聽getObject()聽throws聽BeansException聽{
聽聽聽聽聽聽聽beforePrototypeCreation(beanName);//鍓嶆嫤鎴?/span>
聽聽聽聽聽聽聽try聽{
聽聽聽聽聽聽聽聽return聽createBean(beanName,聽mergedBeanDefinition,聽args);//璋冪敤瀛愮被鐨刢reateBean瀹炵幇鐪熸鐨凚ean鍒涘緩宸ヤ綔
聽聽聽聽聽聽聽}
聽聽聽聽聽聽聽finally聽{
聽聽聽聽聽聽聽聽afterPrototypeCreation(beanName);//鍚庢嫤鎴?/span>
聽聽聽聽聽聽聽}
聽聽聽聽聽聽}
聽聽聽聽聽});
聽聽聽聽聽bean聽=聽getObjectForBeanInstance(scopedInstance,聽name,聽mergedBeanDefinition);//榪斿洖姝g‘綾誨瀷鐨凚ean瀹炰緥
聽聽聽聽}
聽聽聽聽catch聽(IllegalStateException聽ex)聽{
聽聽聽聽聽throw聽new聽BeanCreationException(beanName,聽"Scope聽'"聽+聽scopeName聽+聽"'聽is聽not聽active",聽ex);
聽聽聽聽}
銆銆榛樿鎯呭喌涓嬶紝浣庡眰鐨凚ean宸ュ巶涓彧鏀寔singleton鍙?qiáng)prototype涓ょ綾誨瀷鐨凚ean銆傚綋鎶妔cope璁劇疆鎴恟equest鍙?session鏃跺皢浼?xì)鍑虹庮C笉鑳芥紜瘑鍒玈cope鐨勯敊璇傝繖鏄洜涓烘櫘閫氱殑Bean宸ュ巶閮芥病鏈夋敞鍐屾柊鐨凷cope銆傚彧鏈夊湪 WebApplicationContext涓敞鍐屾墠娉ㄥ唽浜?jiǎn)鏂熬c誨瀷鐨凚ean銆?br />銆銆涓嬮潰鐪嬪疄鐜版敞鍐孲cope鐨勪唬鐮?
銆銆鍦╓ebApplicationContext涓畾涔夊父閲?br />public聽interface聽WebApplicationContext聽extends聽ApplicationContext聽{
銆銆String聽SCOPE_REQUEST聽=聽"request";
銆銆String聽SCOPE_SESSION聽=聽"session";
聽聽聽聽聽聽聽聽聽String聽SCOPE_GLOBAL_SESSION聽=聽"globalSession";
}
銆銆鐒跺悗鍦ㄦ墍鏈夌被鍨嬬殑Web搴旂敤涓婁笅鏂囩殑瀹炵幇涓紝閮藉湪Bean宸ュ巶鐨勬嫤杞借繃紼嬩腑閫氳繃postProcessBeanFactory鏂規(guī)硶鏉ユ敞鍐屾柊綾誨瀷 Scope錛屽GenericWebApplicationContext銆丼taticWebApplicationContext銆?
AbstractRefreshableWebApplicationContext絳塛ebApplication搴旂敤涓婁笅鏂囧疄鐜頒腑銆?br />銆銆protected聽void聽postProcessBeanFactory(ConfigurableListableBeanFactory聽beanFactory)聽{
銆銆聽聽beanFactory.registerScope(SCOPE_REQUEST,聽new聽RequestScope());//娉ㄥ唽request綾誨瀷鐨凚ean
銆銆聽聽beanFactory.registerScope(SCOPE_SESSION,聽new聽SessionScope(false));//娉ㄥ唽session綾誨瀷鐨凚ean
銆銆聽聽beanFactory.registerScope(SCOPE_GLOBAL_SESSION,聽new聽SessionScope(true));//娉ㄥ唽glogalsession銆銆鐨凚ean
}
銆銆緇撳悎涓婇潰鐨勪唬鐮侊紝鐜板湪搴旇鏄庣櫧涓轟粈涔堝彧鏈夊湪Web搴旂敤涓婁笅鏂囦腑鎵嶈兘浣跨敤鏂板鍔犱笁縐嶇被鍨嬬殑Bean浜?jiǎn)鍚с傚綋鐒?dòng)灱岀斪兒庢湁浜?jiǎn)Scope錛屾垜浠篃鍙互闈炲父杞繪澗鐨勫疄鐜版垜浠嚜宸辯殑Scope錛屽鍔犳柊鐢ㄦ埛鑷畾涔夌被鍨嬬殑Bean錛岀劧鍚庤璁″嚭涓涓傚悎鎴戜滑鑷繁鐨凚ean宸ュ巶銆?img src ="http://www.tkk7.com/lijiajia418/aggbug/64968.html" width = "1" height = "1" />
]]>