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

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

          當(dāng)然也可以編程實(shí)現(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 布衣郎 閱讀(1556) 評(píng)論(0)  編輯  收藏

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


          網(wǎng)站導(dǎo)航:
           

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

          常用鏈接

          留言簿(12)

          隨筆分類(59)

          隨筆檔案(57)

          blog

          java

          uml

          搜索

          •  

          積分與排名

          • 積分 - 357319
          • 排名 - 155

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          主站蜘蛛池模板: 浦北县| 卢湾区| 景洪市| 翁源县| 兰西县| 龙海市| 通渭县| 巨鹿县| 阿图什市| 卢氏县| 日照市| 馆陶县| 株洲市| 宁国市| 元氏县| 剑阁县| 阿瓦提县| 上饶县| 宜兰县| 胶南市| 呈贡县| 乾安县| 南雄市| 梓潼县| 六枝特区| 呼玛县| 枣庄市| 伊川县| 谷城县| 凌云县| 措勤县| 凤凰县| 平远县| 资阳市| 黔西县| 巢湖市| 双辽市| 孟村| 疏勒县| 合阳县| 永丰县|