step1 开启切面编程

    <!-- 开启切面编程(通过配置织入@Aspectj切面 )  -->
<aop:aspectj-autoproxy/>

  <aop:aspectj-autoproxy />有一个proxy-target-class属性,默认为false,表示使用jdk动态代理织入增强,当配为<aop:aspectj-autoproxy poxy-target-class="true"/>时,表示使用CGLib动态代理技术织入增强。不过即使proxy-target-class设置为false,如果目标类没有声明接口,则spring将自动使用CGLib动态代理。

step2 编写日志注解类

@Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemLog {
public String description() default "";
}
@Aspect
@Component
public class SystemLogAspect { @Pointcut("@annotation(com.tj.common.log.system.SystemLog)")
public void controllerAspect() {} @Pointcut("@annotation(com.tj.common.log.system.SystemLog)")
public void serviceAspect() {} @Pointcut("@annotation(com.tj.common.log.system.SystemLog)")
public void repositoryAspect() {} @After("controllerAspect()")
public void doBefore(JoinPoint joinPoint) {
try {
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = request.getRemoteAddr();
String description = getControllerMethodDescription(joinPoint);
Object obj = request.getSession().getAttribute("loginUser");
LogUser user = new LogUser(null, null);
/*对象obj中必须拥有属性account、userName*/
BeanUtils.copyProperties(user, obj);
if(StringUtils.isBlank(user.getAccount())){
user = new LogUser("Anonymous", "匿名用户");
}
} catch (Exception e) { }
} @SuppressWarnings("rawtypes")
private static String getControllerMethodDescription(JoinPoint joinPoint) throws Exception {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
String description = "";
for (Method method : methods) {
if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) {
description = method.getAnnotation(SystemLog.class).description();
break;
}
}
}
return description;
}
}

step2 日志记录

  

@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping(value = "/cxOrders")
public class CxOrderResources { @SystemLog(description="查询订单列表操作")
@RequestMapping( value="/showData", method = RequestMethod.GET)
public ResponseEntity<String> showData()throws ApplicationRuntimeException {
return new ResponseEntity<String>("", HttpStatus.OK);
} }

参考:

http://kld208.iteye.com/blog/1632935

http://www.oschina.net/code/snippet_201779_53788

spring统一日志管理,切面(@Aspect),注解式日志管理的更多相关文章

  1. spring+mybatis之注解式事务管理初识(小实例)

    1.上一章,我们谈到了spring+mybatis声明式事务管理,我们在文章末尾提到,在实际项目中,用得更多的是注解式事务管理,这一章将学习一下注解式事务管理的有关知识.注解式事务管理只需要在上一节的 ...

  2. 【Spring】每个程序员都使用Spring(四)——Aop+自定义注解做日志拦截

    一.前言 上一篇博客向大家介绍了Aop的概念,对切面=切点+通知 .连接点.织入.目标对象.代理(jdk动态代理和CGLIB代理)有所了解了.理论很强,实用就在这篇博客介绍. 这篇博客中,小编向大家介 ...

  3. Spring2.5那些事之基于AOP的方法级注解式日志配置

    在日常开发中经常需要在代码中加入一些记录用户操作日志的log语句,比如谁在什么时间做了什么操作,等等. 把这些对于开发人员开说无关痛痒的代码写死在业务方法中实在不是一件很舒服的事情,于是AOP应运而生 ...

  4. Spring AOP基础概念及自定义注解式AOP初体验

    对AOP的理解开始是抽象的,看到切点的匹配方式其实与正则表达式性质大致一样就基本了解AOP是基本是个什么作用了.只是整个概念更抽象,需要具化理解.下图列表是AOP相关概念解释,可能也比较抽象^_^ 比 ...

  5. Spring笔记 #02# 利用切面和注解校验方法参数

    例子还是之前的例子.仍然是对mage进行法术攻击时的咒语进行校验,不过略微提高了扩展性. 应用示例 1.在.properties文件中定义参数格式(正则): sp1=^\\D*hello\\D*$ s ...

  6. Spring AOP实现注解式的Mybatis多数据源切换

    一.为什么要使用多数据源切换? 多数据源切换是为了满足什么业务场景?正常情况下,一个微服务或者说一个WEB项目,在使用Mybatis作为数据库链接和操作框架的情况下通常只需要构建一个系统库,在该系统库 ...

  7. Spring 16: SM(Spring + MyBatis) 注解式事务 与 声明式事务

    Spring事务处理方式 方式1:注解式事务 使用@Transactional注解完成事务控制,此注解可添加到类上,则对类中所有方法执行事务的设定,注解添加到方法上,则对该方法执行事务处理 @Tran ...

  8. 框架整合小小总结【SSH】注解式

    Spring 注解式注册 bean: 大致分为以下几步: 开启 context 空间支持 开启自动扫描功能,指定扫描包路径 使用注解配置 bean (使用@Component 注解) 给 bean 注 ...

  9. Spring的声明式事务----Annotation注解方式(2)

    使用步骤: 步骤一.在spring配置文件中引入<tx:>命名空间<beans xmlns="http://www.springframework.org/schema/b ...

随机推荐

  1. Jsoup系列学习(1)-发送get或post请求

    简介 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQuery的操作方法来取出和操作数据. 官 ...

  2. L1-009. N个数求和

    https://www.patest.cn/contests/gplt/L1-009 原来写的找了好久还是有一个测试点没过, 虽说是道水题,但是今天一遍就过了还是挺高兴的. 送你机组数据 52/5 4 ...

  3. C#体检套餐项目

    使用泛型集合写的一个小项目 1.要实现新建体检套餐,并且如果已经有了该体检套餐就不能再次新建, 2.要实现套餐列表动态更新,没添加一个体检套餐,在套餐列表里就自动添加一项; 3.向当前套餐类表里添加检 ...

  4. [LeetCode] Hamming Distance 汉明距离

    The Hamming distance between two integers is the number of positions at which the corresponding bits ...

  5. [LeetCode] Flip Game 翻转游戏之二

    You are playing the following Flip Game with your friend: Given a string that contains only these tw ...

  6. 走格子 51nod

    球最少需要的能量,就是保证能量一直>=0,从头遍历取过程中能量最小值,绝对值为答案. #include<iostream> #include<algorithm> #in ...

  7. 【WPF】释放图像资源, [删除时报另一进程占用]

    源:zhantianyou CODE private BitmapImage ReturnBitmap(string destFile) { // Read byte[] from png file ...

  8. SQLSERVER 获取datetime日期的查询语句

    SELECT varchar(10:57AM SELECT varchar(CONVERT(100), GETDATE(), 2): 11.05.16 SELECT varchar(CONVERT(1 ...

  9. UISearchController 的用法[点击搜索框,自动到顶部]

    //在ViewDidLoad里面如下代码 self.searchViewController = [[UISearchController alloc]initWithSearchResultsCon ...

  10. 常用jQuery 方法

    //强制给数字补全小数点 function toDecimal2(x) { var f = parseFloat(x); if(isNaN(f)) { return false; } var f = ...