Spring AOP记录日志!

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

 <bean id="uccAdviceAround"
   class="cn.ccb.boup.tcpjjh.util.UccAdviceAround">
  </bean>
 
  <bean id="uccAdviceAroundProxy"
   class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
   parent="sup2BaseTransactionProxy">
   <property name="proxyInterfaces">
    <list>
     <value>
      org.aopalliance.intercept.MethodInterceptor
     </value>
    </list>
   </property>
   <property name="target">
    <ref bean="uccAdviceAround" />
   </property>
  </bean>
   <bean id="uccAdviceAroundAdvisor"
   class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
   <property name="mappedName">
    <value>*ItsLg</value>
   </property>
   <property name="advice">
    <ref bean="uccAdviceAroundProxy" />
   </property>
  </bean>
  <bean id="uccProxy"
   class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
   <property name="beanNames">
    <list>
     <value>*UCC</value>
    </list>
   </property>
   <property name="interceptorNames">
    <list>
     <value>uccAdviceAroundAdvisor</value>
    </list>
   </property>
  </bean>

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

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
/**
 * 【类名】UccAdviceAround <br/>
 * 【类描述】业务逻辑层拦截器<br/>
 * 【对外接口】无<br/>
 */
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;
 }

 /**
  *
   *<p>包名类名:cn.com.pansky.usp4i.business.aop.UccAdviceAround</p>
   *<p>  方法名:logUCC1</p>
   *<p>    描述:UCC日志方法1</p>
   *<p>    参数:</p>
   *<p>返回类型:void</p>
   *<p>创建时间:2012-6-7 下午05:24:38</p>
  */
 private void logUCC1() {
  // 获得开始时间
  tradeLog = new TpjtradelogImpl();
  Date startDate = new Date();
  startTime=System.currentTimeMillis();

 }

 /**
  *
   *<p>包名类名:cn.com.pansky.usp4i.business.aop.UccAdviceAround</p>
   *<p>  方法名:logUCC2</p>
   *<p>    描述:UCC日志方法2</p>
   *<p>    参数:@param methodInvocation</p>
   *<p>返回类型:void</p>
   *<p>创建时间:2012-6-7 下午05:24:53</p>
  */
 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,Date,bean,String)