刚刚出炉的我的aop日志功能 可以写入数据库

aop拦截器

package com.snail.dns.intercepts;

import java.io.IOException;
import java.util.Date;

import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.JoinPoint;
import org.springframework.aop.AfterAdvice;

import com.snail.dns.bean.CdnUser;
import com.snail.dns.bean.DnsLog;
import com.snail.dns.bean.DnsRecords;
import com.snail.dns.common.Constants;
import com.snail.dns.service.ILogService;
import com.snail.dns.util.DateUtil;

/**
@author Jeson
* @blog 
http://www.gbsou.com
* @date:Oct 30, 2009 1:51:34 PM
@version :1.0
*
*/
public class LogInterceptor2 {
    private String [] includeMethodNames = new String []{"update","remove","save","del","add","change"};
    private ILogService logService = null;
    private CdnUser logUser = null;
    public void log(JoinPoint p){
        String method = p.getSignature().getName();
        if("saveLog".equals(method)){
            return;
        }
        if(!isIgnoreMethod(method))return;
        Object obj = ServletActionContext.getRequest().getSession().getAttribute(Constants.SESSION_USER_INFO);
        
        if(obj == null){
            try {
                ServletActionContext.getResponse().sendRedirect("");
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return;
        }
        logUser = (CdnUser)obj;
        Object [] os = p.getArgs();
        Object  o = os[0];
        DnsLog log = this.getLog(o, method);
        logService.saveLog(log);
        System.out.println("日志已经保存");
    }

   

    public ILogService getLogService() {
        return logService;
    }

    public void setLogService(ILogService logService) {
        this.logService = logService;
    }
    
    private DnsLog getLog(Object e,String methodName){
        DnsLog log = new DnsLog();
        log.setLogUid(logUser.getUid());
        StringBuffer buf = new StringBuffer();
        buf.append(DateUtil.getNowTime()+" 用户:"+logUser.getUname());
        String tableName = e.getClass().getName();
        tableName = tableName.substring(tableName.lastIndexOf("."));
        buf.append("对"+tableName+"表做了");
        buf.append(methodName);
        buf.append("的操作,最新数据如下:\\n");
        buf.append(parseEntity(e));
        log.setLogData(buf.toString());
        log.setLogDate(new Date());
        return log;
    }
    
    private String parseEntity(Object o){
        StringBuffer buf = new StringBuffer();
        if(o instanceof CdnUser){
            CdnUser user = (CdnUser)o;
            buf.append("用户编号:");
            buf.append(user.getUid());
            buf.append(" 用户账户:");
            buf.append(user.getUname());
            buf.append(" 用户密码");
            buf.append("******");
        }else if(o instanceof DnsRecords){
            DnsRecords dr = (DnsRecords)o;
            buf.append("域名:");
            buf.append(dr.getZone());
            buf.append(" 二级域名:");
            buf.append(dr.getHost());
            buf.append(" 类型:");
            buf.append(dr.getType());
            buf.append(" 服务器地区和类型:");
            buf.append(dr.getView());
            buf.append(" 存活时间:");
            buf.append(dr.getTtl());
        }
        return buf.toString();
    }
    
    private boolean isIgnoreMethod(String targetActionName){
        boolean flag = false;
        targetActionName = targetActionName.toLowerCase();
        for(String includeName : includeMethodNames){
            if(targetActionName.indexOf(includeName)!=-1){
                flag = true;
                break;
            }
        }
        return flag;
    }
}

 

——————-配置文件

<aop:config>
    <aop:pointcut id="logPointCut"
        expression
="execution(* com.snail.dns.service.imp.*.*(..)) and execution(* com.snail.dns.service.imp.*.query*(..))"/>
    <aop:aspect id="log" ref="log2">
        <aop:after pointcut-ref="allServiceMethod" method="log"/>
    </aop:aspect>
</aop:config>

 

—————— result

Hibernate: insert into cdn_view.dns_log (logUid, logData, logDate) values (?, ?, ?)
日志已经保存
Hibernate: update cdn_view.dns_records set zone=?, host=?, type=?, data=?, ttl=?, mx_priority=?, state=?, uid=?, status=? where id=?

你可能感兴趣的:(AOP,Hibernate,object,String,user,insert)