Spring3.0 +Struts 框架 使用Spring AOP 进行操作日志记录

最近旧项目需要加入操作日志记录,项目框架String3.0.1+Struts 2.5.26 xml配置化方式。最后选择了 使用AOP简单 工作量小,不需要改源程序业务, 查询很多资料,难点在获取request 方法中的参数中,网上很多方式无法适用 例如RequestAttributes ra = RequestContextHolder.getRequestAttributes();
   ServletRequestAttributes sra = (ServletRequestAttributes) ra;
   HttpServletRequest request = sra.getRequest();
原文链接:https://blog.csdn.net/u010924288/article/details/53215769,仅适合 Main线程法获取,无法 在aop异步线程使用。不废话 直接上代码。

一、引入JAR包

aspectjrt-1.7.4.jar

aspectjweaver.jar

aopalliance-1.0.jar

下载地址 

链接:https://pan.baidu.com/s/1WN1TJ8YO5ZxMR3jwS3LcjQ 
提取码:qhy4

二、Spring XML 开启AOP 注解配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"  
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mongo="http://www.springframework.org/schema/data/mongo"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
        <aop:aspectj-autoproxy proxy-target-class="true"/>
      <bean class="com.anran.aop.LogAspect" >
            <property name="operateHisService" ref="operateHisService"></property>
        </bean>

以上红色部分为新加配置,在applicationContext.xml 加入配置接口

三、创建日志类

@Aspect  
@Component
public class LogAspect extends BaseAction {  
   private final Logger logger = LoggerFactory.getLogger(this.getClass());
   private String requestPath = null ; // 请求地址  
   private String userName = null ; // 用户名 
   private String personName = null ; // 人名
   private String inputParamMap = null ; // 传入参数   
   private long startTimeMillis = 0; // 开始时间  
   private long endTimeMillis = 0; // 结束时间
   private String operateName = ""; // 操作名称
   private String ip="";
   private String companyNo="";

    /****************************************************************************
     * *****************************列表查询************************************
     ***************************************************************************/
    /**
     *
     * @Title:doBeforeSelect
     * @Description: select方法调用前触发
     * @author 武玉锋
     * @date 2020年07月05日 下午4:46:21
     * @param joinPoint
     */
   @Before("execution(* com.anran.hgc.actions..*.select(..))")  
   public void doBeforeSelect(JoinPoint joinPoint) {
       operateName="查询";
        startTimeMillis = System.currentTimeMillis(); // 记录方法开始执行的时间
        ActionContext actionContext = ServletActionContext.getContext();
        DealParameter(joinPoint,actionContext);

        
   }

    /**
     *
     * @Title:doAfterSelect
     * @Description: select方法调用后触发
     * @author 武玉锋
     * @date 2020年07月05日 下午4:46:21
     * @param joinPoint
     */
    @After("execution(* com.anran.hgc.actions..*.select(..))")
    public void doAfterSelect(JoinPoint joinPoint) {
        endTimeMillis = System.currentTimeMillis(); // 记录方法执行完成的时间
        this.saveOperateLog();
    }

}

 private void DealParameter(JoinPoint joinPoint, ActionContext actionContext  ) {
     Map<?,?> map = actionContext.getParameters().toMap();
     Gson gson = new Gson();
     inputParamMap =gson.toJson(map);
     requestPath = joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName();
     ip = ServletActionContext.getRequest().getRemoteAddr();
     if ((ip != null) && (ip.equals("0:0:0:0:0:0:0:1"))) {
         ip = "127.0.0.1";
     }
     userName = (String) actionContext.getSession().get("userName");
     companyNo = actionContext.getSession().get("companyNo")==null?"":actionContext.getSession().get("companyNo").toString();
     User  user=(actionContext.getSession().get("user")==null?new User():(User)actionContext.getSession().get("user"));
     if(user.getUserRate()!=null &&!Common.UseRate.SUPERADMIN.equals(user.getUserRate())){
         personName = user.getPerName();
     }
 }

 /**
 *  
 * @Title:printLog
 * @Description: 输出日志
  * @author 武玉锋
  * @date 2020年07月05日 下午4:46:21
  */
private void saveOperateLog() {
    Gson gson = new Gson(); // 需要用到google的gson解析包  
    String optTime = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(startTimeMillis);
    logger.info(" user:"+userName  +" person:"+personName  +" ip:"+ip  +"  url:"+requestPath+"; op_time:" + optTime + " pro_time:" + (endTimeMillis - startTimeMillis) + "ms ;" +" param:"+inputParamMap);
    
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值