package tpf.aspect;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.stereotype.Component; import java.util.Arrays;
import java.util.HashMap;
import java.util.Map; /**
* Created by zipon on 2017/9/20.
*/
@Aspect
@Component
public class AspectTest {
private final static Log logger = LogFactory.getLog(AspectTest.class); private String joinPointInfo(JoinPoint joinPoint) {
return simpleJoinPointInfo(joinPoint) + " args:" + Arrays.toString(joinPoint.getArgs());
} private String simpleJoinPointInfo(JoinPoint joinPoint) {
return joinPoint.getTarget().getClass() + "#" + joinPoint.getSignature().getName();
} //定义切点
@Pointcut("execution(* tpf.controller.loginController.* (..))")
public void cutPublicMethod() {
} @Before("cutPublicMethod()")
public void logBefore(JoinPoint pjp) throws Throwable {
} @AfterReturning("cutPublicMethod()")
public void logAfter(JoinPoint pjp) throws Throwable {
} @Around("cutPublicMethod()")
public Object consumingAround(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("**************登陆开始**************");
String joinPointInfo = joinPointInfo(joinPoint);
String simpleJoinPointInfo = simpleJoinPointInfo(joinPoint);
long startTime = System.currentTimeMillis();
logger.info("[API_START]" + joinPointInfo);
Object[] args = joinPoint.getArgs();
Object obj = joinPoint.proceed(args);
// logger.info("[API_END]" + simpleJoinPointInfo);
long endTime = System.currentTimeMillis();
long diffTime = endTime - startTime;
Map<String, String> map = new HashMap<String, String>();
map.put("method", simpleJoinPointInfo);
map.put("time", String.valueOf(diffTime));
map.put("start", String.valueOf(startTime));
map.put("end", String.valueOf(endTime)); //方法执行完,要用joinPoint.proceed();
joinPoint.proceed();
System.out.println("==================登陆结束后==================");
return obj;
} }

【spring aop切面】基础使用教程的更多相关文章

  1. spring AOP(切面) 表达式介绍

    在 spring AOP(切面) 例子基础上对表达式进行介绍 1.添加接口删除方法 2.接口实现类 UserDaoServer 添加实现接口删除方法 3.测试类调用delUser方法 4. 输出结果截 ...

  2. Spring AOP切面的时候参数的传递

    Spring AOP切面的时候参数的传递 Xml: <?xml version="1.0" encoding="UTF-8"?> <beans ...

  3. Spring AOP 切面编程记录日志和接口执行时间

    最近客户现在提出系统访问非常慢,需要优化提升访问速度,在排查了nginx.tomcat内存和服务器负载之后,判断是数据库查询速度慢,进一步排查发现是因为部分视图和表查询特别慢导致了整个系统的响应时间特 ...

  4. 使用Spring AOP切面解决数据库读写分离

    http://blog.jobbole.com/103496/ 为了减轻数据库的压力,一般会使用数据库主从(master/slave)的方式,但是这种方式会给应用程序带来一定的麻烦,比如说,应用程序如 ...

  5. 利用Spring AOP切面对用户访问进行监控

    开发系统时往往需要考虑记录用户访问系统查询了那些数据.进行了什么操作,尤其是访问重要的数据和执行重要的操作的时候将数记录下来尤显的有意义.有了这些用户行为数据,事后可以以用户为条件对用户在系统的访问和 ...

  6. Spring AOP 切面编程的方法

    spring aop的使用分为两种,一种是使用注解来实现,一种是使用配置文件来实现. 先来简单的介绍一下这两种方法的实现,接下来详细的介绍各处的知识点便于查阅.目录如下: 1.基于注解实现spring ...

  7. [Spring] Aspect Oriented Programming with Spring | AOP | 切面 | 切点

    使用Spring面向切面编程 1.介绍 AOP是OOP的补充,提供了另一种关于程序结构的思路. OOP的模块化的关键单位是 类 . AOP的则是aspect切面. AOP 将程序的逻辑分成独立的块(叫 ...

  8. Spring AOP 切面实现操作日志

    创建接口注解日志类 package com.fh.service.logAop; /** * Created by caozengling on 2018/7/21. */ import java.l ...

  9. spring AOP(切面)

    AOP 基本概念cross  cutting concern横切性关注点:独立服务,遍布在系统处理流程之中 Aspect对横切关注点模块化 advice对横切关注点具体实现 pointcut定义adv ...

  10. Spring aop切面插入事物回滚

    <!-- tx标签配置 事物--> <tx:advice id="txadvice" transaction-manager="transactionM ...

随机推荐

  1. 使用ErrorProvider组件验证文本框输入

    实现效果: 知识运用: ErrorProvider组件的BlinkStyle属性 //指示错误图标的闪烁时间 public ErrorBlinkStyle BlinkStyle{ get;set; } ...

  2. VMware vSphere6.0 服务器虚拟化部署安装图解

    一 VMware vSphere部署的前期规划要点 1 vSphere的优点 (略) 2 如何利用现在的设备架构虚拟化环境 在虚拟化过程中,用户大多会考虑目前现有的服务器.存储.交换机等基础设备是否可 ...

  3. 解决cocos2dx 3.x 导入cocostudio的ui界面出现错位问题

    笔者今天发现导入cocostudio的ui界面时,会有部分控件出现错位的现象,后来我看了一下源码,发现是部分控件是没有继承 Layout类,导致不能设置控件位置造成,原因可以看看cocos2dx 源码 ...

  4. Python的ORM介绍

    实现方法: SQLOject peewee Django's ORM SQLAlchemy

  5. w3resource_MySQL练习:Subquery

    w3resource_MySQL练习题:Subquery 1. Write a query to find the name (first_name, last_name) and the salar ...

  6. (转)webView清除缓存

    NSURLCache * cache = [NSURLCache sharedURLCache]; [cache removeAllCachedResponses]; [cache setDiskCa ...

  7. JFinal 结合Dubbo发生的一些问题

    1.java.lang.NoSuchMethodError: org.jboss.resteasy.specimpl.BuiltResponse.getHeaders()Ljavax/ws/rs/co ...

  8. 【HIHOCODER 1142】 三分·三分求极值

    描述 这一次我们就简单一点了,题目在此: 在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求点P到抛物线的最短距离d. 输入 第1行:5个整数a,b,c,x,y.前三个数构成抛物 ...

  9. list_for_each_entry()函数分析

    list_for_each原型: #define list_for_each(pos, head) \ for (pos = (head)->next, prefetch(pos->nex ...

  10. Python虚拟机之while循环控制结构(三)

    Python虚拟机中的while循环控制结构 在Python虚拟机之if控制流(一)和Python虚拟机之for循环控制流(二)两个章节中,我们介绍了if和for两个控制结构在Python虚拟机中的实 ...