Let's go inside

          this blog is deprecated as a result of laziness.
          posts - 59, comments - 2, trackbacks - 0, articles - 0

          TrailBlazer 第 4, 5 天

          Posted on 2006-07-27 13:07 Earth 閱讀(171) 評(píng)論(0)  編輯  收藏 所屬分類: JavaEE5/EJB3

          唉,打雷下雨收衣服落~~
          有狀態(tài)session bean, 曾經(jīng)有菜鳥告訴過我們它們是一對(duì)一的關(guān)系

          非常直接, 只有一點(diǎn)需要注意,標(biāo)注了@Stateful的Bean不要忘了系列化

          另外一點(diǎn)是在JSP中使用時(shí),記得把lookup得到的SFSB實(shí)例放在session中,下次用這個(gè)實(shí)例的時(shí)候從session取,而不是重新lookup一次。

          Calculator?cal? =
          ??????(Calculator)?session.getAttribute(
          " sfsb_cal " );
          if ?(cal? == ? null )?{
          ??
          try ?{
          ????InitialContext?ctx?
          = ? new ?InitialContext();
          ????cal?
          = ?(Calculator)?ctx.lookup(
          ????????????
          " EJB3Trail/StatefulCalculator/local " );
          ????session.setAttribute?(
          " sfsb_cal " ,?cal);
          ??}?
          catch ?(Exception?e)?{
          ????e.printStackTrace?();
          ??}
          }

          // ?Make?use?of?the?cal?object

          下一講很重要:Session Bean 的生命周期
          第四天結(jié)束了,外面嘩啦啦下起雨來,下次見吧。


          當(dāng)你需要在對(duì)象 創(chuàng)建時(shí)初始化某些變量

          或者在對(duì)象銷毀時(shí)釋放某些資源的時(shí)候,

          你可以對(duì)這個(gè)POJO的進(jìn)行標(biāo)注,然后服務(wù)就回自動(dòng)回調(diào) 這些經(jīng)過 特殊 標(biāo)注的方法。

          你不用實(shí)現(xiàn)那些ejbCreate()...等等無聊的哪怕僅僅是空的方法。再也不用了。沒有人強(qiáng)迫你。只需要在你想要用的方法上標(biāo)注,僅此而已

          廢話少說,有下面這幾個(gè)標(biāo)注需要記得
          @PostConstruct
          @PreDestroy
          @PrePassivate
          @PostActivate這四個(gè)我一下就記得了,明顯和EJB2中的那幾個(gè)方法一一對(duì)應(yīng)~

          @Init,這個(gè)要單獨(dú)說一下:
          This annotation designates initialization methods for a stateful session bean. It is different from the @PostConstruct annotation in that there can be multiple methods tagged with @Init in a stateful session bean. However, each bean instance can only have one @Init method invoked. The EJB 3.0 container determines which @Init method to invoke depending on how the bean is created (see the EJB 3.0 specification for details). The @PostConstruct method is called after the @Init method.
          說完了~
          哈哈。看懂了嗎?
          說的是@Init是SFSB專用的,而且可以有好幾個(gè),但是容器最終要調(diào)哪個(gè)我們也不知道,要想知道的話就去看EJB Spec.
          我們只能告訴你這個(gè)方法在@PostConstruct之后被調(diào)用。

          暈了~不知道便罷,還偏要擺出一幅很酷的樣子~ ~ 
          算了,不去惹他,讓我們看最后一個(gè)叫@Remove的標(biāo)簽(之前已經(jīng)見識(shí)過了),

          我們用@Remove告訴服務(wù),滅掉該實(shí)例吧,我們已經(jīng)用完不再需要了!

          這個(gè)和前面幾個(gè)都不一樣哦,前面幾個(gè)是-“實(shí)例”在過期或無效時(shí)服務(wù)自動(dòng)回調(diào)的~,這個(gè)是我們主動(dòng)請(qǐng)求地。

          @Stateful
          public ? class ?SessionCalculator? implements ?Calculator?{

          ????
          // ??
          ????
          ????@Remove
          ????
          public ? void ?stopSession?()?{
          ????????
          // ?Call?to?this?method?signals?the?container
          ????????
          // ?to?remove?this?bean?instance?and?terminates
          ????????
          // ?the?session.
          ????}
          ?????
          ????
          // ??
          }

          The following code shows how the @Remove method is called in the JSP page. Note that we also empty the HttpSession cache to remove the invalid stub.

          // ?"cal"?is?the?stub?of?the?stateful?session?bean.
          // ?It?is?cached?in?the?HttpSession's?"lifecycle_cal"?attribute

          if ?( " Logout " .equals(request.getParameter( " action " )))?{
          ??cal.stopSession?();
          ??session.setAttribute?(
          " lifecycle_cal " ,? null );
          ??
          ??
          // ??
          }

          SFSB在被@Remove后,然后重新lookup,就會(huì)建新的實(shí)例。這時(shí)@PostConstruct會(huì)被調(diào)用

          如果覺得這些方法很煩人,可以把它們放在一邊,然后用@CallbackListener標(biāo)注就行了

          Separate life cycle methods into another class?
          ?If all those callback methods seem to clutter up your session bean class, you can also separate out the callback methods into a separate callback listener class. You need to annotate the session bean class with the @CallbackListener tag and specify the listener class name in the annotation parameter.

          @Stateful
          @CallbackListener(CalculatorCallbackListener.
          class )
          public ? class ?SessionCalculator? implements ?Calculator?{
          ????
          // ??
          }


          The listener class contains all the annotated callback methods for this bean. Each callback method now takes the bean instance as input parameter. The container pasess the bean instance that causes the callback event to the callback method at runtime.

          public ? class ?CalculatorCallbackListener?{

          ????@PostConstruct
          ????
          public ?initialize?(CalculatorBean?cal)?{
          ????????
          // ??
          ????}
          ????
          ????@PreDestroy
          ????
          public ?exit?(CalculatorBean?cal)?{
          ????????
          // ??
          ????}
          }

          第五天完了,要睡覺了,下次見吧。

          主站蜘蛛池模板: 莲花县| 和龙市| 琼中| 阿拉善盟| 金寨县| 富川| 隆林| 隆尧县| 长宁区| 肇源县| 鹤峰县| 五台县| 渑池县| 南乐县| 阿拉善盟| 尖扎县| 宜良县| 北安市| 石狮市| 山西省| 南投市| 宁乡县| 蛟河市| 吴忠市| 铁岭县| 保山市| 大兴区| 大丰市| 怀集县| 锡林浩特市| 辛集市| 来安县| 万山特区| 萝北县| 罗山县| 商城县| 池州市| 上虞市| 九台市| 江阴市| 弋阳县|