閑人野居
          好好學習,天天向上
          posts - 57,  comments - 137,  trackbacks - 0
          在spring 的以前版本中,只是支持singleton,prototype兩種類型,
          在2.0中作了很大的改進,增加了RequestScope,和SessionScope兩種范圍。當然也支持自定義Scope
          下面簡單介紹一下,spring2.0是如何支持自定義Scope的。
          Scope接口,需要實現(xiàn)的接口,主要的方法:
          • Object get(String name, ObjectFactory objectFactory)
          • Object remove(String name)
          • void registerDestructionCallback(String name, Runnable callback)
          get 和remove這是最常用的方法,registerDestructionCallback用于回收對象時,可以異步調(diào)用其它操作。
          下面簡單定義一個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生效,有兩種方法:
          第一編程實現(xiàn):
          ConfigurableBeanFactory 定義了關(guān)于Scope的一些方法:
          void registerScope(String scopeName, Scope scope);
          String[] getRegisteredScopeNames();
          Scope getRegisteredScope(String scopeName);
          可以使用registerScope方法來注冊相應的scope
          ?
          applicationContext.getBeanFactory().registerScope("myScope", scope);??? ?

          另外一種實現(xiàn) xml 配置(建議使用)
          ? 通過CustomScopeConfigurer 來注冊相應的Scope,由于CustomScopeConfigurer 實現(xiàn)了BeanFactoryPostProcessor,對于ApplcationContext,自動會實現(xiàn)相應的配置
          ?
          <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"/>??? ?

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


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

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


          網(wǎng)站導航:
           

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

          常用鏈接

          留言簿(12)

          隨筆分類(59)

          隨筆檔案(57)

          blog

          java

          uml

          搜索

          •  

          積分與排名

          • 積分 - 358033
          • 排名 - 156

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 邵阳县| 荆州市| 比如县| 十堰市| 株洲县| 绥滨县| 久治县| 成都市| 昭苏县| 专栏| 灵寿县| 绍兴市| 辽阳县| 武穴市| 扶绥县| 文昌市| 舞阳县| 兴安县| 万年县| 奉新县| 宝鸡市| 昂仁县| 乐昌市| 罗源县| 阿克| 盈江县| 茌平县| 呼伦贝尔市| 潜江市| 库尔勒市| 宁乡县| 喀喇| 普兰店市| 广东省| 铁力市| 黄梅县| 柞水县| 河东区| 武强县| 岳阳县| 仙游县|