第一步:编写切面类

package com.dascom.hawk.app.web.tool;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class AnnotationAspectJ { //定义切面("execution(* com.dascom.common.aop.*.*(..)))
//当前配置的意思是所有添加了SuiteMessage的注解的方法作为切点
@Pointcut("@annotation(com.dascom.common.annotation.SuiteMessage)")
public void logPointCut() {
} //前置通知
@Before("logPointCut()")
public void before(JoinPoint point) {
String calssName = point.getTarget().getClass().getName();
String method = point.getSignature().getName();
System.out.println(calssName + " : " + method);
} //后置通知
@After("logPointCut()")
public void after(JoinPoint point) {
String method = point.getSignature().getName();
System.out.println(method + ": end----");
} //环绕通知
@Around("logPointCut()")
public Object around(ProceedingJoinPoint point) throws Throwable {
long beginTime = System.currentTimeMillis();
// 执行方法
Object result = point.proceed();
// 执行时长(毫秒)
long time = System.currentTimeMillis() - beginTime;
//异步保存日志
System.out.println(time);
return result;
}
}

第二步:在spring的配置文件中添加注解扫描

<?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:context="http://www.springframework.org/schema/context"
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/context http://www.springframework.org/schema/context/spring-context.xsd">
<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.dascom.hawk.app.web.tool"></context:component-scan>
<!-- 自动为切面方法中匹配的方法所在的类生成代理对象。
proxy-target-class="true" 这个的作用是struts的控制类都基础的actionSupport,必须添加这个,不然会报错
-->
<aop:aspectj-autoproxy proxy-target-class="true" /> </beans>

第三步:搞定。爽歪歪~~~

基于spring@aspect注解的aop实现的更多相关文章

  1. Spring之注解实现aop(面向切面编程)

    1:Aop(aspect object programming)面向切面编程,名词解释:    1.1:功能:让关注点代码与业务逻辑代码分离    1.2:关注点        重复代码就叫做关注点  ...

  2. Spring使用注解实现AOP

    一.AspectJ概述 AspectJ是一个面向切面的框架,它扩展了Java语言.定义了AOP语法,能够在编译期提供代码的织入,它提供了一个专门的编译期用来生成遵守字节编码规范的Class文件. @A ...

  3. 基于 自己定义注解 和 aop 实现使用memcache 对数据库的缓存 演示样例

    好久没更新blog了,在新公司打拼了两个月,每天都从早忙到晚,学到了非常多东西,可是没有时间来更新blog了.... 以下開始解说这次的主题 公司老大让我研究 ocs 就是阿里云的 开放缓存服务 点击 ...

  4. Spring Annotation注解进行aop的学习

    使用Maven管理项目,pom文件为: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...

  5. Spring的AOP开发(基于ApsectJ的注解)

    创建项目,导包 编写目标类并配置 创建OrderDao package com.rick.aop.demo1; public class OrderDao { public void save() { ...

  6. Spring的AOP基于AspectJ的注解方式开发2

    参考自黑马培训机构 上一篇博客提到了在配置文件中开启aop的注解开发,以及简单使用了@Before,@Aspect 这是为了告诉spring为前置通知和切面类 接下来介绍aop的注解的通知类型,和切入 ...

  7. (转)使用Spring的注解方式实现AOP的细节

    http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...

  8. Spring IOC 和Aspectj AOP

    1.Aspectj AOP 是一套独立的AOP 解决方案,不仅限于java应用,不依赖其他方案,属于编译时增强,有自己单独的编译器.Spring AOP 是基于Spring 容器的的AOP解决方式,属 ...

  9. Spring学习笔记4——AOP

    AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...

随机推荐

  1. 转载-Eclipse导入第三方库的方法

    作者:wyf_phper 原文:https://blog.csdn.net/qq_32985981/article/details/49976193 一:导入*.jar包步骤:将下载好的jar包复制到 ...

  2. Navicat无法直连MySQL怎么办?

    本文背景 Navicat是图形化操作MySQL的强大工具,但是当数据库的服务器没有开放3306端口给办公网络时,在办公网使用navicat连接数据库是连不上的.要操作数据库,只能先ssh登陆到数据库服 ...

  3. 求0到n之间素数个数的序列

    要求: (1) 找出0-1000之间素数(2) 设f(n)表示0-n之间的素数个数,计算出当n=0,1,2,3,.....,997时f(n)的值,并写入文件 分析: 首先找素数使用一个效率较高的方法- ...

  4. streamreader

    using (StreamReader sr = new StreamReader(@"C:\Documents and Settings\Administrator\桌面\1.txt&qu ...

  5. python的数据处理一

    def load_data(filename): features = [] labels = [] f = open(filename, encoding='utf-8') medical = js ...

  6. 转载:java web 项目中如何设置项目打开的默认页面

    通过博客学到的两种方法总结: 一.在web.xml文件中加入: 此时项目打开的默认页面就是loginS.html 二.在WebContent文件夹下添加index.jsp文件,此时这个index.js ...

  7. 后端狗的Vue学习历程(一) - demo示例与基本逻辑语法

    目录 demo的三部分结构 判断:v-if.v-else-if.v-else 循环:v-for 事件绑定 v-on:eventType 内容输入的双向绑定v-model 源码:Github demo的 ...

  8. ElementUI表格行编辑单元格编辑支持(输入框,选择框)Demo

    嗯,需要做成这个样子,所以网上查了些资料.整理了下.提供几个一个思路.不足之处请小伙伴指出来.  普通版的table可编辑内嵌select选择框,输出框,编辑删除添加等 <!DOCTYPE ht ...

  9. 自定义圆角背景的textview,抛弃shape

    自定义一个圆角背景的TextView,解放双手,不用再写shape了. 1.values目录新建attrs.xml. <?xml version="1.0" encoding ...

  10. pycharm 报错及解决方法

    1.报错: AttributeError: 'list' object has no attribute 'click' 原因:应是find_element_by 不是 find_elements_b ...