Spring AOP记录日志!

 对所有接口以UCC结尾的、方法名以ItsLg结尾的,记录操作日志!

     class="cn.ccb.boup.tcpjjh.util.UccAdviceAround">
  

 
      class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
   parent="sup2BaseTransactionProxy">
   
    
     
      org.aopalliance.intercept.MethodInterceptor
     

    

   

   
    
   

  

      class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
   
    *ItsLg
   

   
    
   

  

      class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
   
    
     *UCC
    

   

   
    
     uccAdviceAroundAdvisor
    

   

  

----------------------------
import java.util.Date;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
 * 【类名】UccAdviceAround

 * 【类描述】业务逻辑层拦截器

 * 【对外接口】无

 */
public class UccAdviceAround extends AdviceBase implements MethodInterceptor {
 //注入交易日志
 private Tpjtradelog tradeLog;
 
 //开始时间
 private long startTime=0;
 
 

 //注入可扩展的日志工厂类
 private ITpjTradeLogUCC logUcc;
 
 private boolean checkS(String value) {
  if (null == value)
   return false;
  if ("".equals(value))
   return false;
  if ("null".equals(value))
   return false;
  return true;
 }
 
 public Object invoke(MethodInvocation methodInvocation)
   throws Exception, Throwable {
  Object result = null;
  this.logUCC1();
  boolean flag = true;
  try{
   result = methodInvocation.proceed();// 为了第二次拦截时能正常进行业务操作
  }catch(Exception e){
   //eLog = e;
   flag = false;
   e.printStackTrace();
   throw e;
  }finally{
   if(flag){
    logUCC2(methodInvocation);
   }
  }
  return result;
 }

 /**
  *
   *

包名类名:cn.com.pansky.usp4i.business.aop.UccAdviceAround


   *

  方法名:logUCC1


   *

    描述:UCC日志方法1


   *

    参数:


   *

返回类型:void


   *

创建时间:2012-6-7 下午05:24:38


  */
 private void logUCC1() {
  // 获得开始时间
  tradeLog = new TpjtradelogImpl();
  Date startDate = new Date();
  startTime=System.currentTimeMillis();

 }

 /**
  *
   *

包名类名:cn.com.pansky.usp4i.business.aop.UccAdviceAround


   *

  方法名:logUCC2


   *

    描述:UCC日志方法2


   *

    参数:@param methodInvocation


   *

返回类型:void


   *

创建时间:2012-6-7 下午05:24:53


  */
 protected void logUCC2(MethodInvocation methodInvocation){

  try{
   UserInfo userLogin = BizRuntimeContext.current().getSessionAttribute("userInfo");// 从session中获得当前登陆用户信息
   String opertaorid = userLogin.getAccount();// 操作员ID
   String uccName = methodInvocation.getMethod().toString();
   String allName = "";
   String packageName = "";
   String className = "";
   String lowerMethodName = "";
   String methodName = "";
   int endIndex = uccName.lastIndexOf("(");
   if(endIndex>0){
    allName = uccName.substring(0,endIndex);
   }
   if(checkS(allName)){
    //截取包名类名和方法名
    int endBit = allName.lastIndexOf(".");
    int startBit = allName.lastIndexOf("cn.");
    if((endBit>0)&&(startBit>0)){
     packageName = allName.substring(startBit,endBit);
     if(endBit+1<=allName.length()){
      className = allName.substring(endBit+1, allName.length());
     }else{
      className = allName.substring(endBit, allName.length());
     }
     methodName = allName.substring(startBit,allName.length());
    }
    lowerMethodName = packageName.toLowerCase() + className;
   }
  
   tradeLog.setFsModelcode(TfAmt.log_map.get(className));
      tradeLog.setFsOperaaction(className);
      tradeLog.setFsOperausrcode(opertaorid);
      tradeLog.setFtOperadt(new Date());
      System.err.println("methodName=:\"" + methodName);
      System.err.println("method process time:"+new Long(System.currentTimeMillis()-startTime));
      logUcc = (ITpjTradeLogUCC)SpringCallUtil.INSTANCE.getBean("tcpjjhTpjTradeLogUCC");
      logUcc.saveTradeLog(tradeLog);
  }catch(Exception e){
   System.err.println("记录日志时出现错误,可能是session过期造成,重新登录系统后可避 免此问题!");
   e.printStackTrace();
  }
  
 }

 
}

 

你可能感兴趣的:(技术天地,spring,aop,exception,string,bean,date)