posts - 2, comments - 2, trackbacks - 0, articles - 1
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理
          從Tapestry4.0開(kāi)始使用了Hivemind作為注入容器,合理有效的使用該容器,可以極大的簡(jiǎn)化開(kāi)發(fā)工作,減少代碼量.比如說(shuō),在開(kāi)發(fā)Hibernate應(yīng)用時(shí),就有一個(gè)關(guān)于事務(wù)控制的問(wèn)題,在Spring中可以使用Spring來(lái)管理事務(wù),但是我覺(jué)得那樣對(duì)自己的代碼侵入性比較大,而利用Hivemind中的攔截器,則可以方便的解決此問(wèn)題,而且侵入性小.
          步驟是這樣的:1.配置Hibernate Session服務(wù),并保證該服務(wù)是線程級(jí)的.2.配置攔截器服務(wù),并向其注入Hibernate Session服務(wù).3.配置DAO服務(wù),并配置其攔截器為上一步的攔截器.
          具體的實(shí)現(xiàn)如下:1.配置文件

          <?xml?version="1.0"?>
          <!--?
          ???Copyright?2004,?2005?The?Apache?Software?Foundation

          ???Licensed?under?the?Apache?License,?Version?2.0?(the?"License");
          ???you?may?not?use?this?file?except?in?compliance?with?the?License.
          ???You?may?obtain?a?copy?of?the?License?at

          ???????http://www.apache.org/licenses/LICENSE-2.0

          ???Unless?required?by?applicable?law?or?agreed?to?in?writing,?software
          ???distributed?under?the?License?is?distributed?on?an?"AS?IS"?BASIS,
          ???WITHOUT?WARRANTIES?OR?CONDITIONS?OF?ANY?KIND,?either?express?or?implied.
          ???See?the?License?for?the?specific?language?governing?permissions?and
          ???limitations?under?the?License.
          -->

          <module?id="mymodule"?version="4.0.0">

          <service-point?id="userDao"?interface="com.ims.dao.UserInfoDAO">
          ????
          <invoke-factory>
          ????????
          <construct?class="com.ims.dao.impl.UserinfoDAOImpl">
          ????????????
          <set-object?property="session"?value="service:hibernateSession"/>
          ????????
          </construct>
          ????????
          ????
          </invoke-factory>
          ????
          <interceptor?service-id="ims.SessionInterceptor">
          ????????
          <include?method="make*"/>
          ????
          </interceptor>
          </service-point>
          <service-point?id="SessionInterceptor"?interface="org.apache.hivemind.ServiceInterceptorFactory">
          ????
          <invoke-factory>?
          ????
          <construct?class="com.hivemind.util.interceptor.SessionInterceptor"?>?
          ????????
          <set-object?property="session"?value="service:hibernateSession"?/>?
          ????
          </construct>?
          </invoke-factory>?

          </service-point>


          <service-point?id="hibernateHivemindFactory"?interface="org.apache.hivemind.ServiceImplementationFactory">
          ????
          <invoke-factory>
          ????????
          <construct?class="com.hivemind.util.HibernateHivemindFactory"?initialize-method="init">
          ????????????
          <set-object?property="threadEventNotifier"?value="service:hivemind.ThreadEventNotifier"/>
          ????????
          </construct>
          ????
          </invoke-factory>
          </service-point>
          <service-point?id="hibernateSession"?interface="org.hibernate.Session">
          ????
          <invoke-factory?service-id="ims.hibernateHivemindFactory"?model="threaded">
          ????
          <config?file="hibernate.cfg.xml"/>
          ????
          </invoke-factory>
          </service-point>
          </module>

          2.代碼:
          /**
          ?*?SessionInterceptor.java
          ?*
          ?*??
          */

          package?com.hivemind.util.interceptor;

          import?java.lang.reflect.InvocationHandler;
          import?java.lang.reflect.InvocationTargetException;
          import?java.lang.reflect.Method;
          import?java.lang.reflect.Proxy;
          import?java.util.List;

          import?org.apache.commons.logging.Log;
          import?org.apache.hivemind.Element;
          import?org.apache.hivemind.InterceptorStack;
          import?org.apache.hivemind.ServiceInterceptorFactory;
          import?org.apache.hivemind.internal.Module;
          import?org.apache.hivemind.service.impl.LoggingUtils;
          import?org.apache.hivemind.util.StringUtils;
          import?org.hibernate.Session;
          import?org.hibernate.Transaction;

          /**
          ?*?Hibernate?Session的攔截器,對(duì)符合指定條件的方法進(jìn)行攔截,?加入事務(wù)處理功能。
          ?*?
          ?*??
          ?
          */

          public?class?SessionInterceptor?implements?ServiceInterceptorFactory?{
          ????
          private?String[]?required_method;

          ????
          private?Session?session;

          ????
          public?void?setSession(Session?session)?{
          ????????
          this.session?=?session;
          ????}


          ????
          /**
          ?????*?核心服務(wù)方法,生成攔截器
          ?????
          */

          ????
          public?void?createInterceptor(InterceptorStack?stack,
          ????????????Module?invokingModule,?List?parameters)?
          {
          ????????Log?log?
          =?stack.getServiceLog();
          ????????
          for?(int?i?=?0;?i?<?parameters.size();?i++)?{
          ????????????Object?obj?
          =?parameters.get(i);
          ????????????
          if?(obj?instanceof?Element)?{
          ????????????????Element?element?
          =?(Element)?obj;
          ????????????????
          if?("include".equals(element.getElementName()))?{
          ????????????????????String?value?
          =?element.getAttributeValue("method");
          ????????????????????required_method?
          =?StringUtils.split(value);
          ????????????????}


          ????????????}

          ????????}

          ????????Object?obj?
          =?stack.peek();

          ????????InvocationHandler?handler?
          =?new?HibernateSessionInvocationHandler(log,
          ????????????????obj,?session,?required_method);
          ????????Object?interceptor?
          =?Proxy.newProxyInstance(invokingModule
          ????????????????.getClassResolver().getClassLoader(),?
          new?Class[]?{?stack
          ????????????????.getServiceInterface()?}
          ,?handler);
          ????????stack.push(interceptor);
          ????}


          }


          /**
          ?*?攔截器方法調(diào)用處理類(lèi)。
          ?*?
          ?
          */

          class?HibernateSessionInvocationHandler?implements?InvocationHandler?{
          ????
          private?Log?log;

          ????
          /**
          ?????*?原始對(duì)象
          ?????
          */

          ????
          private?Object?delegate;

          ????
          private?Session?session;

          ????
          /**
          ?????*?配置的方法名稱(chēng)列表
          ?????
          */

          ????
          private?String[]?requredName;

          ????
          public?HibernateSessionInvocationHandler(Log?log,?Object?delegate,
          ????????????Session?session,?String[]?required_name)?
          {
          ????????
          this.log?=?log;
          ????????
          this.delegate?=?delegate;
          ????????
          this.session?=?session;
          ????????
          this.requredName?=?required_name;

          ????}


          ????
          /**
          ?????*?處理攔截到的方法
          ?????
          */

          ????
          public?Object?invoke(Object?proxy,?Method?method,?Object[]?args)
          ????????????
          throws?Throwable?{
          ????????Object?result?
          =?null;
          ????????
          boolean?debug?=?log.isDebugEnabled();
          ????????Transaction?tx?
          =?null;

          ????????
          if?(debug)?{
          ????????????LoggingUtils.entry(log,?method.getName(),?args);
          ????????}


          ????????
          if?(isMatch(method.getName(),?requredName))?{
          ????????????
          try?{
          ????????????????tx?
          =?session.beginTransaction();
          ????????????????result?
          =?method.invoke(delegate,?args);

          ????????????????
          if?(debug)?{
          ????????????????????
          if?(method.getReturnType()?==?void.class)
          ????????????????????????LoggingUtils.voidExit(log,?method.getName());
          ????????????????????
          else
          ????????????????????????LoggingUtils.exit(log,?method.getName(),?result);
          ????????????????}

          ????????????????tx.commit();
          ????????????????
          return?result;
          ????????????}
          ?catch?(InvocationTargetException?ex)?{
          ????????????????
          try?{
          ????????????????????tx.rollback();
          ????????????????}
          ?catch?(Exception?e)?{

          ????????????????}

          ????????????????Throwable?targetException?
          =?ex.getTargetException();

          ????????????????
          if?(debug)
          ????????????????????LoggingUtils.exception(log,?method.getName(),
          ????????????????????????????targetException);

          ????????????????
          throw?targetException;

          ????????????}

          ????????}
          ?else?{
          ????????????
          try?{
          ????????????????result?
          =?method.invoke(delegate,?args);
          ????????????????
          if?(debug)?{
          ????????????????????
          if?(method.getReturnType()?==?void.class)
          ????????????????????????LoggingUtils.voidExit(log,?method.getName());
          ????????????????????
          else
          ????????????????????????LoggingUtils.exit(log,?method.getName(),?result);
          ????????????????}

          ????????????????
          return?result;
          ????????????}
          ?catch?(InvocationTargetException?e)?{
          ????????????????Throwable?targetException?
          =?e.getTargetException();

          ????????????????
          if?(debug)
          ????????????????????LoggingUtils.exception(log,?method.getName(),
          ????????????????????????????targetException);

          ????????????????
          throw?targetException;
          ????????????}


          ????????}


          ????}


          ????
          /**
          ?????*?當(dāng)前調(diào)用的方法名稱(chēng)是否符合指定的條件
          ?????*?
          ?????*?
          @param?name
          ?????*?
          @param?names
          ?????*?
          @return
          ?????
          */

          ????
          private?boolean?isMatch(String?name,?String[]?names)?{
          ????????
          if?(names?==?null)
          ????????????
          return?false;
          ????????
          for?(int?i?=?0;?i?<?names.length;?i++)?{
          ????????????String?sname?
          =?names[i];

          ????????????
          if?(name.equals(sname)?||?sname.endsWith("*")
          ????????????????????
          &&?name.startsWith(sname.substring(0,?sname.length()?-?1))
          ????????????????????
          ||?sname.startsWith("*")
          ????????????????????
          &&?name.endsWith(sname.substring(1)))
          ????????????????
          return?true;

          ????????}

          ????????
          return?false;
          ????}

          }


          評(píng)論

          # re: 在Tapestry中使用攔截器實(shí)現(xiàn)Hibernate的事務(wù)管理  回復(fù)  更多評(píng)論   

          2006-10-23 09:49 by ELFer
          你好,我現(xiàn)在正在學(xué)習(xí)tapestry+hivemind+hibernate框架,不知道你有沒(méi)有什么好的實(shí)例,謝謝。我有一定的struct+spring+hibernate基礎(chǔ)!
          非常感謝!

          # re: 在Tapestry中使用攔截器實(shí)現(xiàn)Hibernate的事務(wù)管理  回復(fù)  更多評(píng)論   

          2006-10-24 09:30 by 浪跡天涯
          @ELFer
          你好,如果有好的實(shí)例我會(huì)發(fā)布出來(lái)的,謝謝你的關(guān)注。

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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 娄底市| 哈密市| 比如县| 阿拉善右旗| 丰都县| 广水市| 锦屏县| 新民市| 阿拉善右旗| 象山县| 彭阳县| 渭南市| 丹棱县| 六安市| 洪洞县| 友谊县| 寿光市| 汝州市| 盈江县| 吉木乃县| 崇礼县| 昭觉县| 高台县| 麟游县| 永春县| 明溪县| 固安县| 乐山市| 容城县| 汕头市| 哈巴河县| 横峰县| 布拖县| 奈曼旗| 靖边县| 金乡县| 盐亭县| 盈江县| 九龙坡区| 呼伦贝尔市| 右玉县|