基于xml配置的spring aop 的实现日志 和为通知传递参数

1、首先是在spring配置文件下配置如下信息:

<!-- 切面 -->
      <bean id="myAspectOfLoginAndLoginout" class="bea.com.aop.Log_loginAndLogout"></bean>
      <bean id="myAspectOfAUD" class="bea.com.aop.Log_Record" >
            <property name="userLogService">
                <ref bean="userLogService" />
            </property>
      </bean>

      <!-- 切入点 -->
      <aop:config>
        <aop:aspect id="log_login" ref="myAspectOfLoginAndLoginout">
          <aop:after method="log_Login" pointcut="execution(* bea.com.admin.Service.*.Login*(..))" />

        </aop:aspect>

        <aop:aspect id="log_AUD" ref="myAspectOfAUD">
            <aop:after-returning method="addUserLog" returning="returnValue"  pointcut="execution(* bea.com.admin.Service.*.add*(..))" />
            <aop:after-returning method="addUserLog" returning="returnValue" pointcut="execution(* bea.com.Service.*.add*(..))" />
            <aop:after-returning method="modifyUserLog" returning="returnValue" pointcut="execution(* bea.com.Service.*.modify*(..))" />
            <aop:after-returning method="modifyUserLog" returning="returnValue"  pointcut="execution(* bea.com.admin.Service.*.modify*(..))" />
        </aop:aspect>
      </aop:config>

2、然后在aop类里实现如下代码:

import javax.servlet.http.HttpSession;

import org.slf4j.Logger;  
import org.slf4j.LoggerFactory;  
import org.apache.struts2.ServletActionContext;
import org.aspectj.lang.JoinPoint;

import bea.com.aop.service.UserLogService;
import bea.com.spojo.TuserLog;
import bea.com.util.DateParse;
import bea.com.util.reflectUtil;

public class Log_Record{  
    private static final Logger logger = LoggerFactory.getLogger(Log_Record.class); 

    private UserLogService userLogService;

    public UserLogService getUserLogService() {
        return userLogService;
    }

    public void setUserLogService(UserLogService userLogService) {
        this.userLogService = userLogService;
    }

    /**
     * 新增日志
     * @param joinPoint
     * @param returnValue
     */
    public void addUserLog(JoinPoint joinPoint, Object returnValue){
        Object[] args = joinPoint.getArgs();
        String className="";//新增的类名
        boolean flag=false;

        int tableId=Integer.parseInt(returnValue.toString());

        //joinPoint.getTarget();
        //System.out.println("target:"+joinPoint.getTarget());
        //target:bea.com.Service.CunWeiService@d7721e7

        HttpSession session=ServletActionContext.getRequest().getSession();
        int userId=Integer.parseInt(session.getAttribute("userId").toString());
        System.out.println(args[0]);
        //bea.com.spojo.Tcunweiandzhiwei@7470f191

        Class userClass = args[0].getClass(); 
        className=userClass.getSimpleName();

        String datetimeNow=DateParse.date2String(new Date());
        TuserLog  tuserLog=new TuserLog();

        tuserLog.setOperationTime(datetimeNow);
        tuserLog.setOperationType(1);
        tuserLog.setTableName(className);
        tuserLog.setUserId(userId);
        tuserLog.setUserId(userId);
        tuserLog.setTableId(tableId);

        this.userLogService.addUserLog(tuserLog);
        System.out.println("returnValue"+returnValue);
    }

    /**
     * 记录修改的日志
     * @param joinPoint
     */
    public void modifyUserLog(JoinPoint joinPoint,Object returnValue){
        //如果修改成功!
        if(Boolean.parseBoolean(returnValue.toString())){
            Object[] args = joinPoint.getArgs();
            String className="";//修改的类名

            HttpSession session=ServletActionContext.getRequest().getSession();
            int userId=Integer.parseInt(session.getAttribute("userId").toString());

            System.out.println(args[0]);
            Class userClass = args[0].getClass(); 

            reflectUtil.reflect(args[0]);
            className=userClass.getSimpleName();

            String datetimeNow=DateParse.date2String(new Date());
            TuserLog  tuserLog=new TuserLog();

            tuserLog.setOperationTime(datetimeNow);
            tuserLog.setOperationType(1);
            tuserLog.setTableName(className);
            tuserLog.setUserId(userId);
            tuserLog.setUserId(userId);
        //  tuserLog.setTableId(tableId);

            this.userLogService.addUserLog(tuserLog);

        //修改不成功
        }else{
            return;
        }

    }

}  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值