一、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>

Spring+AOP+Log4j 用注解的方式记录指定某个方法的日志的更多相关文章

  1. 运用Spring Aop,一个注解实现日志记录

    运用Spring Aop,一个注解实现日志记录 1. 介绍 我们都知道Spring框架的两大特性分别是 IOC (控制反转)和 AOP (面向切面),这个是每一个Spring学习视频里面一开始都会提到 ...

  2. nginx不记录指定文件类型的日志

    1.指定记录文件日志记录的内容. vim /usr/local/nginx/conf/nginx.conf如下部分: log_format dd '$remote_addr $http_x_forwa ...

  3. 采用Spring AOP+Log4j记录项目日志

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6567672.html 项目日志记录是项目开发.运营必不可少的内容,有了它可以对系统有整体的把控,出现任何问题 ...

  4. Spring AOP+Log4j记录项目日志

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6567672.html 项目日志记录是项目开发.运营必不可少的内容,有了它可以对系统有整体的把控,出现任何问题 ...

  5. Spring系列(四):Spring AOP详解和实现方式(xml配置和注解配置)

    参考文章:http://www.cnblogs.com/hongwz/p/5764917.html 一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程, ...

  6. spring AOP的两种配置方式

    连接点(JoinPoint) ,就是spring允许你是通知(Advice)的地方,那可就真多了,基本每个方法的前.后(两者都有也行),或抛出异常是时都可以是连接点,spring只支持方法连接点.其他 ...

  7. 利用Spring AOP和自定义注解实现日志功能

    Spring AOP的主要功能相信大家都知道,日志记录.权限校验等等. 用法就是定义一个切入点(Pointcut),定义一个通知(Advice),然后设置通知在该切入点上执行的方式(前置.后置.环绕等 ...

  8. Spring AOP源码分析--代理方式的选择

    能坚持别人不能坚持的,才能拥有别人未曾拥有的.关注编程大道公众号,让我们一同坚持心中所想,一起成长!! 年前写了一个面试突击系列的文章,目前只有redis相关的.在这个系列里,我整理了一些面试题与大家 ...

  9. Spring AOP详解和实现方式

    一.什么是AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善. ...

随机推荐

  1. (转载)Mac和iOS开发资源汇总—更新于2013-07-19

    (转载)http://beyondvincent.com/2013/07/18/resources-for-mac-and-ios-developers/ 小引 本文主要汇集一些苹果开发的资源,会经常 ...

  2. Move Zeroes——Leetcode

    Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...

  3. iOS不越狱装收费App——注册iOS设备为开发者工具

    额,这篇教程主要是我写下来用于总结注册iOS设备和用iResign安装App的过程,想要不越狱安装App当然有办法,但是有几个前提--你是一个Apple开发者,或者你有个朋友是App的开发者.如果没有 ...

  4. 《A First Course in Probability》-chape4-离散型随机变量-方差

    为了描述一组数据的均值,我们引入了数学期望的概念,为了描述一组数据相对均值的波动情况,我们引入了方差. 能够看到,方差的本质也是一个期望,那么我们就能够利用期望的定义将其继续展开. 方差的一条重要性质 ...

  5. STL之set、multiset、functor&pair使用方法

    set是一个集合容器,其中包含的元素是唯一的,集合中的元素是按照一定的顺序排列的.元素插入过程是按照排序规则插入,所以不能使用指定位置插入. set采用红黑树变体的数据结构实现,红黑树属于平衡二叉树. ...

  6. 第十七章、程序管理与 SELinux 初探

    ---恢复内容开始--- 什么是程序 (process) 在 Linux 底下所有的命令与你能够进行的动作都与权限有关, 而系统依据UID/GID以及文件的属性相关性判定你的权限!在 Linux 系统 ...

  7. FIREDAC TFDCONNECTION连接MYSQL数据库

    FIREDAC TFDCONNECTION连接MYSQL数据库 procedure TfrmDB.ConnectDB;begin FDMoniFlatFileClientLink1.FileName ...

  8. winform Label与DataGridView右对齐 分类: WinForm 2014-05-19 20:51 446人阅读 评论(0) 收藏

    实现Label与DataGridView对齐有两种方法,差别不大: 定义: Label名称:lblName DataGridView名称:dgvData 第一种: lblName.Location = ...

  9. iOS开发总结-UIWebView 集成 浏览器

    // // detailWebViewController.m // BJ // // Created by shirenfeng on 16/11/6. // Copyright © 2016年 c ...

  10. crontab 定时任务 每过多少分钟执行

    被下面的问题纠结了一会 记录一下以备提醒 每过5分钟执行 */ * * * * curl localhost/system/dns/dnns.php?ac=ToDNS 每小时的第五分钟执行 0 * * ...