当前位置:编程学习 > JAVA >>

Spring AOP记录系统日志

配置文件:
Xml代码 
<!-- 操作日志切面声明  -->     
   <bean id="logAspect" class="com.tq365.service.sys.log.SystemLogAspect"/>  
   <aop:config>  
       <aop:aspect ref="logAspect">  
       </aop:aspect>  
   </aop:config>  
 
 
实现代码:
Java代码 
/** 
 * 系统操作日志切面 
 *  
 * @author archie2010 
 *  since 2011-3-17 下午02:44:03 
 */  
@Aspect  
public class SystemLogAspect {  
  
    // int与long之Class会自动转为其封装类型之Class  
    private static final String integerClazz = "class java.lang.Integer";  
    private static final String longClazz = "class java.lang.Long";  
  
    @Resource  
    private SystemLogService systemLogService;  
  
    private Logger logger = Logger.getLogger(this.getClass().getName());  
  
    @Pointcut("execution(* com.tq365.service..*.*(..))")  
    public void myAspect() {  
    };  
  
    @AfterThrowing(pointcut = "myAspect()", throwing = "e")  
    public void doAfterThrowing(JoinPoint jp, Throwable e) {  
        System.out.println("出现异常:" + e.getMessage());  
        System.out.println(e.getClass().getName());  
        System.out.println("异常所在类:" + jp.getTarget().getClass().getName());  
        System.out.println("" + jp.getSignature().getName()  
                + "方法 throw exception");  
        // logger.error("错误! error级别的!!!"+e.getMessage());  
        logger.error("Oops===" + jp.getTarget().getClass().getName() + "中的"  
                + jp.getSignature().getName() + "方法抛出" + e.getClass().getName()  
                + "异常");  
        System.out.println("参数:");  
        ;  
        if (jp.getArgs() != null && jp.getArgs().length > 0) {  
            for (int i = 0; i < jp.getArgs().length; i++) {  
                System.out.println(jp.getArgs()[i].toString());  
                logger.error("参数:--" + jp.getArgs()[i].toString());  
            }  
        }  
    }  
  
    @SuppressWarnings("unchecked")  
    @After("@annotation(com.tq365.sys.annotation.SystemLogAnnotation)")  
    public void doAfter(JoinPoint jp) {  
        System.out.println("----------后置通知");  
        System.out.println("方法所在类:" + jp.getTarget().getClass().getName());  
        System.out.println("" + jp.getSignature().getName() + "方法");  
  
        String methodName = jp.getSignature().getName();  
  
        // 操作日志对象-----------------  
        SystemLog sysLog = new SystemLog();  
  
        // 操作参数-----------------  
        String descArgs = "参数";  
        if (jp.getArgs() != null && jp.getArgs().length > 0) {  
            for (int i = 0; i < jp.getArgs().length; i++) {  
                if(jp.getArgs()[i]!=null){  
                    //System.out.println(jp.getArgs()[i].toString());  
                    descArgs += jp.getArgs()[i].toString()+",";  
                }else{  
                    descArgs +="null"+",";  
                }  
            }  
            System.out.println("------参数" + descArgs);  
        }  
        sysLog.setOperateArgs(descArgs);  
  
        String des = null;//方法描述  
        if (!(methodName.startsWith("set") || methodName.startsWith("get"))) {  
  
            Class targetClass = jp.getTarget().getClass();  
  
            //方法不定向参数Clazz...  
            Class[] claszs = new Class[jp.getArgs().length];  
  
            for (int i = 0; i < jp.getArgs().length; i++) {  
                //System.out.println(jp.getArgs()[i]);  
                if(jp.getArgs()[i]!=null){  
                    System.out.println(jp.getArgs()[i].getClass());  
                    if (jp.getArgs()[i].getClass().toString().equals(integerClazz)) {  
                        claszs[i] = int.class;  
                    } else if (jp.getArgs()[i].getClass().toString().equals(  
                            longClazz)) {  
                        claszs[i] = long.class;  
                    }else{  
                        claszs[i] =jp.getArgs()[i].getClass();  
&
补充:软件开发 , Java ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,