基于spring@aspect注解的aop实现
第一步:编写切面类
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实现的更多相关文章
- Spring之注解实现aop(面向切面编程)
1:Aop(aspect object programming)面向切面编程,名词解释: 1.1:功能:让关注点代码与业务逻辑代码分离 1.2:关注点 重复代码就叫做关注点 ...
- Spring使用注解实现AOP
一.AspectJ概述 AspectJ是一个面向切面的框架,它扩展了Java语言.定义了AOP语法,能够在编译期提供代码的织入,它提供了一个专门的编译期用来生成遵守字节编码规范的Class文件. @A ...
- 基于 自己定义注解 和 aop 实现使用memcache 对数据库的缓存 演示样例
好久没更新blog了,在新公司打拼了两个月,每天都从早忙到晚,学到了非常多东西,可是没有时间来更新blog了.... 以下開始解说这次的主题 公司老大让我研究 ocs 就是阿里云的 开放缓存服务 点击 ...
- Spring Annotation注解进行aop的学习
使用Maven管理项目,pom文件为: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi=" ...
- Spring的AOP开发(基于ApsectJ的注解)
创建项目,导包 编写目标类并配置 创建OrderDao package com.rick.aop.demo1; public class OrderDao { public void save() { ...
- Spring的AOP基于AspectJ的注解方式开发2
参考自黑马培训机构 上一篇博客提到了在配置文件中开启aop的注解开发,以及简单使用了@Before,@Aspect 这是为了告诉spring为前置通知和切面类 接下来介绍aop的注解的通知类型,和切入 ...
- (转)使用Spring的注解方式实现AOP的细节
http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...
- Spring IOC 和Aspectj AOP
1.Aspectj AOP 是一套独立的AOP 解决方案,不仅限于java应用,不依赖其他方案,属于编译时增强,有自己单独的编译器.Spring AOP 是基于Spring 容器的的AOP解决方式,属 ...
- Spring学习笔记4——AOP
AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...
随机推荐
- linux下的终端利器----tmux
转:tmux 是指通过一个终端登录远程主机并运行后,在其中可以开启多个控制台的终端复用软件.类似GNU Screen,但来自于OpenBSD,采用BSD授权.使用它最直观的好处就是,通过一个终端登录远 ...
- sqlserver with(NOLOCK) 或 with(READPAST)
https://blog.csdn.net/shuicaohui5/article/details/6758868
- MSSQL sql numeric转字符串显示不补0
由于工作中需要把numeric转字符串显示,但是有一个问题会自动补0. DECLARE @f NUMERIC(18,4)=1.1200, @str VARCHAR(50) SELECT CAST(@f ...
- php邮箱发送
php发送邮件 -------------------------------------------------------------------------------- <?php he ...
- java.sql.SQLException: Error: Error: could not match input
impala执行sql,输出后我在控制台粘贴执行OK,奇怪了. java.sql.SQLException: Error: Error: could not match input 原因竟然是myba ...
- SpringMVC源码之Handler注册、获取以及请求controller中方法
总结 对requestMappingHandlerMapping进行initializeBean时register Handler http开始请求时,initHandlerMappings,Disp ...
- TensorFlow_笔记
Tensorflow 1.基本概念 TensorFlow是一个编程系统,使用图(graphs)来表示计算任务,图(graphs)中的节点称之为op(operation),一个op获得0个或多个Tens ...
- MySql的远程登录问题
1.linux中先连接数据库:mysql -uroot -p(密码) 2.在mysql命令行中输入: GRANT ALL PRIVILEGES ON *.* TO '登录id'@'%' IDENTIF ...
- java中对于浮点型数据操作
java的基本数据类型-浮点型:单精度(float)和双精度(double). float:单精度浮点数在机内占4个字节.有效数字8位.表示范围:-3.40E+38 ~ +3.40E+38; doub ...
- 如何使用Camtasia给视频或者图片调色
喜欢摄影过着做视频的朋友一定知道,一张好看的照片或者一段精美视频的构成因素很多,取景本身肯定是个很重要的条件,相机的素质是非常重要的硬件条件,接下来的就是后期的编辑和处理了,而在后期处理过程中调色就显 ...