<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    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 閱讀(170) 評論(0)  編輯  收藏 所屬分類: JavaEE5/EJB3

    唉,打雷下雨收衣服落~~
    有狀態session bean, 曾經有菜鳥告訴過我們它們是一對一的關系

    非常直接, 只有一點需要注意,標注了@Stateful的Bean不要忘了系列化

    另外一點是在JSP中使用時,記得把lookup得到的SFSB實例放在session中,下次用這個實例的時候從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 的生命周期
    第四天結束了,外面嘩啦啦下起雨來,下次見吧。


    當你需要在對象 創建時初始化某些變量

    或者在對象銷毀時釋放某些資源的時候,

    你可以對這個POJO的進行標注,然后服務就回自動回調 這些經過 特殊 標注的方法。

    你不用實現那些ejbCreate()...等等無聊的哪怕僅僅是空的方法。再也不用了。沒有人強迫你。只需要在你想要用的方法上標注,僅此而已

    廢話少說,有下面這幾個標注需要記得
    @PostConstruct
    @PreDestroy
    @PrePassivate
    @PostActivate這四個我一下就記得了,明顯和EJB2中的那幾個方法一一對應~

    @Init,這個要單獨說一下:
    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專用的,而且可以有好幾個,但是容器最終要調哪個我們也不知道,要想知道的話就去看EJB Spec.
    我們只能告訴你這個方法在@PostConstruct之后被調用。

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

    我們用@Remove告訴服務,滅掉該實例吧,我們已經用完不再需要了!

    這個和前面幾個都不一樣哦,前面幾個是-“實例”在過期或無效時服務自動回調的~,這個是我們主動請求地。

    @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,就會建新的實例。這時@PostConstruct會被調用

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

    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)?{
    ????????
    // ??
    ????}
    }

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

    主站蜘蛛池模板: 在线观看片免费人成视频播放| 精品日韩99亚洲的在线发布| 一道本在线免费视频| 日本一区免费电影| 亚洲av无码一区二区三区四区| 全免费a级毛片免费**视频| 亚洲av无码一区二区三区天堂| 免费日本黄色网址| 极品美女一级毛片免费| 免费一级毛片不卡不收费| 日日躁狠狠躁狠狠爱免费视频| 亚洲AV无码乱码在线观看性色扶 | 国产亚洲精品激情都市| 中国黄色免费网站| 91精品国产亚洲爽啪在线影院| 最近的中文字幕大全免费8| 亚洲一卡2卡4卡5卡6卡在线99 | 国产精品二区三区免费播放心 | 国产成人va亚洲电影| 亚洲国产人成精品| 国产精品免费大片| 亚洲人成7777| 国产一级做a爱免费视频| g0g0人体全免费高清大胆视频| 亚洲av永久无码精品漫画| 18女人毛片水真多免费| 亚洲熟妇AV一区二区三区宅男| 亚洲裸男gv网站| 99久久免费中文字幕精品| 亚洲久热无码av中文字幕| 红杏亚洲影院一区二区三区| 亚洲免费黄色网址| jzzijzzij在线观看亚洲熟妇| 亚洲色欲久久久综合网| 日本免费网址大全在线观看| 免费无码国产在线观国内自拍中文字幕 | 麻豆91免费视频| 亚洲今日精彩视频| 日韩午夜免费视频| 久久九九全国免费| 亚洲av日韩专区在线观看|