閑人野居
          好好學習,天天向上
          posts - 57,  comments - 137,  trackbacks - 0
          在spring 的以前版本中,只是支持singleton,prototype兩種類型,
          在2.0中作了很大的改進,增加了RequestScope,和SessionScope兩種范圍。當然也支持自定義Scope
          下面簡單介紹一下,spring2.0是如何支持自定義Scope的。
          Scope接口,需要實現的接口,主要的方法:
          • Object get(String name, ObjectFactory objectFactory)
          • Object remove(String name)
          • void registerDestructionCallback(String name, Runnable callback)
          get 和remove這是最常用的方法,registerDestructionCallback用于回收對象時,可以異步調用其它操作。
          下面簡單定義一個Scope對象:
          ?
          Scope scope = new Scope() {
          ??? private int index;
          ??? private List objects = new LinkedList(); {
          ??? ??? objects.add(new TestBean());
          ??? ??? objects.add(new TestBean());
          ??? }
          ??? public String getConversationId() {
          ??? ??? return null;
          ??? }
          ??? public Object get(String name, ObjectFactory objectFactory) {
          ??? ??? if (index >= objects.size()) {
          ??? ??? ??? index = 0;
          ??? ??? }
          ??? ??? return objects.get(index++);
          ??? }
          ??? public Object remove(String name) {
          ??? ??? throw new UnsupportedOperationException();
          ??? }
          ??? public void registerDestructionCallback(String name, Runnable callback) {
          ??? }
          };??? ?

          如何使用讓此scope生效,有兩種方法:
          第一編程實現:
          ConfigurableBeanFactory 定義了關于Scope的一些方法:
          void registerScope(String scopeName, Scope scope);
          String[] getRegisteredScopeNames();
          Scope getRegisteredScope(String scopeName);
          可以使用registerScope方法來注冊相應的scope
          ?
          applicationContext.getBeanFactory().registerScope("myScope", scope);??? ?

          另外一種實現 xml 配置(建議使用)
          ? 通過CustomScopeConfigurer 來注冊相應的Scope,由于CustomScopeConfigurer 實現了BeanFactoryPostProcessor,對于ApplcationContext,自動會實現相應的配置
          ?
          <bean id="myScope" class="MyScope"/>
          <bean id="customerScope" class="org.springframework.beans.factory.config.CustomScopeConfigurer">
          ??? <property name="scopes">
          ??? ??? <map>
          ??? ??? ??? <entry key="myScope">
          ??? ??? ??? ??? <bean class="myScope"/>
          ??? ??? ??? </entry>
          ??? ??? </map>
          ??? </property>
          </bean>
          <bean id="usesScope" class="org.springframework.beans.TestBean" scope="myScope"/>??? ?

          當然也可以編程實現
          ?
          Map scopes = new HashMap();
          scopes.put(this, new NoOpScope()); ??? ??? ??? ??? ??? ???
          CustomScopeConfigurer figurer = new CustomScopeConfigurer();
          figurer.setScopes(scopes);??? ?


          posted on 2007-01-04 18:37 布衣郎 閱讀(1556) 評論(0)  編輯  收藏

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          <2007年1月>
          31123456
          78910111213
          14151617181920
          21222324252627
          28293031123
          45678910

          常用鏈接

          留言簿(12)

          隨筆分類(59)

          隨筆檔案(57)

          blog

          java

          uml

          搜索

          •  

          積分與排名

          • 積分 - 357322
          • 排名 - 155

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 随州市| 淮北市| 德昌县| 龙口市| 隆德县| 靖远县| 孟津县| 陆良县| 梅河口市| 五大连池市| 贵溪市| 巫溪县| 临邑县| 灌云县| 攀枝花市| 江北区| 广宗县| 昂仁县| 榆树市| 内江市| 陵川县| 潮州市| 科技| 农安县| 同德县| 隆化县| 桐城市| 永兴县| 马关县| 岫岩| 保亭| 沁水县| 河东区| 房山区| 土默特左旗| 涟水县| 肃南| 五指山市| 合江县| 长兴县| 广宗县|