当前位置: 首页 > news >正文

Spring+AOP+Log4j 用注解的方式记录指定某个方法的日志

一、spring aop execution表达式说明

在使用spring框架配置AOP的时候,不管是通过XML配置文件还是注解的方式都需要定义pointcut"切入点"

例如定义切入点表达式 execution(* com.sample.service.impl..*.*(..))

execution()是最常用的切点函数,其语法如下所示:

 整个表达式可以分为五个部分:

 1、execution(): 表达式主体。

 2、第一个*号:表示返回类型,*号表示所有的类型。

 3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,com.sample.service.impl包、子孙包下所有类的方法。

 4、第二个*号:表示类名,*号表示所有的类。

 5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数。

二、程序

1.要记录日志的某个方法: updatePromote

/**
     * 修改商品活动
     * @param vo
     * @param promoteId为,AOP监控的查询ID
     * @return
     * @throws Exception
     */
    public ResultVO updatePromote(PromoteVO vo,Long promoteId)throws Exception;

2.增加一个横切关注点,打印日志,Java类为

package com.fortis.drugstore.web.userdbThrift.aop;

import java.util.Date;
import org.apache.log4j.Logger;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.fortis.drugstore.base.action.BaseAction;
import com.fortis.drugstore.system.utils.SessionInfo;
import com.fortis.drugstore.web.userdbThrift.activity.service.PromoteService;
import com.fortis.thrift.userdb.PromoteVO;
//声明这是一个组件
@Component
//声明这是一个切面Bean
@Aspect
public class ServiceAspect extends BaseAction<Object>{
    private static final long serialVersionUID = 7690224540336380592L;
    private final static Logger log = Logger.getLogger(ServiceAspect.class);
    @Autowired
    private PromoteService service;
    
    //配置切入点,该方法无方法体,主要为方便同类中其他方法使用此处配置的切入点
    @Pointcut("execution(* com.fortis.drugstore.web.userdbThrift.activity.service.impl..PromoteServiceImpl.updatePromote(..))")
    public void aspect(){    }
    /*
     * 配置前置通知,使用在方法aspect()上注册的切入点
     * 同时接受JoinPoint切入点对象,可以没有该参数
     */
    @Before("aspect()")
    public void before(JoinPoint joinPoint){
        PromoteVO obBefore = new PromoteVO();
        Object []param = joinPoint.getArgs();
        if(log.isInfoEnabled()){
            Object promoteId = param[1]; //切点中有两个参数,第二个为表的主键,用于查询
            SessionInfo sessionInfo = (SessionInfo) getSession().getAttribute("sessionInfo");
            try {
                obBefore = service.queryPromoteDetail(Long.valueOf(promoteId.toString()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            log.info("【修改活动数据】:"+new Date()+" 【账号】:"+sessionInfo.getUserAcct()+" 【名称】:"+sessionInfo.getUserName());
            log.info("修改之前:"+obBefore.toString());
        }
    }
    
    //配置后置通知,使用在方法aspect()上注册的切入点
    @After("aspect()")
    public void after(JoinPoint joinPoint){
        PromoteVO obAfter = new PromoteVO();
        Object []param = joinPoint.getArgs();
        if(log.isInfoEnabled()){
            Object promoteId = param[1];
            try {
                obAfter = service.queryPromoteDetail(Long.valueOf(promoteId.toString()));
            } catch (Exception e) {
                e.printStackTrace();
            }
            log.info("修改之后:"+obAfter.toString());
        }
    }
    
}

3.spring aop的配置文件 spring-aop.xml

<?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:aop="http://www.springframework.org/schema/aop" xmlns:c="http://www.springframework.org/schema/c" xmlns:cache="http://www.springframework.org/schema/cache" xmlns:context="http://www.springframework.org/schema/context" xmlns:jdbc="http://www.springframework.org/schema/jdbc" xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:task="http://www.springframework.org/schema/task" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd
        http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd
        http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <!-- 激活自动代理功能 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    
</beans>

 

转载于:https://www.cnblogs.com/qiuren/p/6068518.html

相关文章:

  • 使用Apache的Base64类实现Base64加解密
  • 【ARM-Linux开发】在win下开发的eclipse+yougatoo+jlink环境搭建
  • Python 绘图利器 —— ggplot
  • Linux下多网卡同网段多IP网络分流设定方法
  • 跟庄
  • SQL连接查询
  • 算法导论笔记之红黑树
  • Hibernate 系列教程10-组成关系
  • Java丨JDK与JRE
  • JDBC基础
  • 要不搞个blog公告?
  • 2016.11.19
  • 手机常用meta标签-有注释
  • Spring Boot 系列教程2-Data JPA
  • python :页面布局 ,后台管理页面之左侧菜单跟着滚动条动
  • [js高手之路]搞清楚面向对象,必须要理解对象在创建过程中的内存表示
  • 【RocksDB】TransactionDB源码分析
  • 8年软件测试工程师感悟——写给还在迷茫中的朋友
  • Android框架之Volley
  • Docker 笔记(2):Dockerfile
  • js 实现textarea输入字数提示
  • Js基础知识(一) - 变量
  • Otto开发初探——微服务依赖管理新利器
  • Python学习笔记 字符串拼接
  • react-core-image-upload 一款轻量级图片上传裁剪插件
  • Shell编程
  • 阿里云容器服务区块链解决方案全新升级 支持Hyperledger Fabric v1.1
  • 离散点最小(凸)包围边界查找
  • 前端 CSS : 5# 纯 CSS 实现24小时超市
  • 微信小程序:实现悬浮返回和分享按钮
  • 微信小程序上拉加载:onReachBottom详解+设置触发距离
  • 想晋级高级工程师只知道表面是不够的!Git内部原理介绍
  • 消息队列系列二(IOT中消息队列的应用)
  • 小李飞刀:SQL题目刷起来!
  • 云栖大讲堂Java基础入门(三)- 阿里巴巴Java开发手册介绍
  • 7行Python代码的人脸识别
  • MPAndroidChart 教程:Y轴 YAxis
  • Salesforce和SAP Netweaver里数据库表的元数据设计
  • Semaphore
  • ​VRRP 虚拟路由冗余协议(华为)
  • ​如何在iOS手机上查看应用日志
  • #Linux杂记--将Python3的源码编译为.so文件方法与Linux环境下的交叉编译方法
  • (1)bark-ml
  • (六)c52学习之旅-独立按键
  • (牛客腾讯思维编程题)编码编码分组打印下标(java 版本+ C版本)
  • (一)RocketMQ初步认识
  • (转)EXC_BREAKPOINT僵尸错误
  • (转)菜鸟学数据库(三)——存储过程
  • (转)平衡树
  • (转载)CentOS查看系统信息|CentOS查看命令
  • *** 2003
  • .net mvc部分视图
  • .NET Reactor简单使用教程
  • .net 前台table如何加一列下拉框_如何用Word编辑参考文献
  • .NET委托:一个关于C#的睡前故事