基于注解方式实现Aop
开启注解扫描
<context:component-scan base-package="aopSpring"></context:component-scan>
将AOP的注解应用到容器中
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
把横切关注点的代码添加到切面类中
@component
@Aspect
应用如下
aop/ArithMath
import org.springframework.stereotype.Component;
@Component
public class ArithMath {
public ArithMath(){}
public int add(int i,int j){
return i + j;
}
public int div(int i,int j){
return i / j;
}
}
在ArithMath方法执行过程中插入日志
编写切面类aop/ArithMathAopImp
@Component
@Aspect
public class ArithMathAopImp {
//前置增强@Before
@Before("execution(* aopSpring.ArithMath.add(int,int))")
public void loggingArithMath(JoinPoint joinPoint){ //添加参数JoinPoint 可以获取目标的参数
String methd = joinPoint.getSignature().getName();
List<Object> list = Arrays.asList(joinPoint.getArgs());
System.out.println("the mathod "+ methd +" on load begin whih "+list);
}
//后置增强不可访问方法返回值
@After(value="execution(* aopSpring.ArithMath.add(int,int))")
public void AfterMethod(JoinPoint joinPoint){
String method = joinPoint.getSignature().getName();
System.out.println("the mathod "+ method +" on end!");
}
//返回通知
@AfterReturning(value="execution(* aopSpring.ArithMath.add(int,int))",returning="rt")
public void AfterReturn(JoinPoint joinPoint,Object rt){
String method = joinPoint.getSignature().getName();
System.out.println("the mathod "+ method +" return "+rt);
}
//异常通知,
@AfterThrowing(value="execution(* aopSpring.ArithMath.*(int,int))",throwing="ex")
public void AfterThrowingMethod(JoinPoint joinPoint,Exception ex){ //指定特定的异常(Exception )发生时才执行代码
String method = joinPoint.getSignature().getName();
System.out.println("the mathod "+ method +" throw "+ex);
}
/**
* 环绕通知@Around
* 通知必须加参数ProceedingJoinPoint,且必须有返回值
* proceed()表示执行目标方法
*/
@Around("execution(* aopSpring.ArithMath.*(int,int))")
public Object AroundMethed(ProceedingJoinPoint pj){
Object rt = null;
String method = pj.getSignature().getName();
try {
//前置
System.out.println(method + "before");
rt = pj.proceed();
//后置
System.out.println(method + "after");
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println(method + e);
}
//后置
System.out.println(method + "returnning");
return rt;
}
基于注解方式实现Aop的更多相关文章
- 基于AspectJ的注解方式进行AOP开发
-------------------siwuxie095 基于 AspectJ 的注解方式进行 AOP 开发 ...
- 基于注解的Spring AOP的配置和使用
摘要: 基于注解的Spring AOP的配置和使用 AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.可以通过预编译方式和运行期动态代理实现在不 ...
- 基于注解的Spring AOP示例
基于注解的Spring AOP示例 目录 在XML配置文件中开启 @AspectJ 支持 声明切面及切入点 声明通知 测试 结语 在XML配置文件中开启 @AspectJ 支持 要使用Spring的A ...
- Shiro入门之二 --------基于注解方式的权限控制与Ehcache缓存
一 基于注解方式的权限控制 首先, 在spring配置文件applicationContext.xml中配置自动代理和切面 <!-- 8配置自动代理 --> <bean cl ...
- Spring声明式事务管理(基于注解方式实现)
----------------------siwuxie095 Spring 声明式事务管理(基于注解方式实现) 以转 ...
- (转)使用Spring的注解方式实现AOP的细节
http://blog.csdn.net/yerenyuan_pku/article/details/52879669 前面我们已经入门使用Spring的注解方式实现AOP了,现在我们再来学习使用Sp ...
- (转)使用Spring的注解方式实现AOP入门
http://blog.csdn.net/yerenyuan_pku/article/details/52865330 首先在Eclipse中新建一个普通的Java Project,名称为spring ...
- Spring系列之aAOP AOP是什么?+xml方式实现aop+注解方式实现aop
Spring系列之aop aop是什么?+xml方式实现aop+注解方式实现aop 什么是AOP? AOP为Aspect Oriented Programming 的缩写,意识为面向切面的编程,是通过 ...
- Elasticsearch-mapper 基于注解方式生成mapping(2.0以上)
Elasticsearch生成mapping的方式上有多种方式,我们可以把mapping做成配置文件,也可以用spring-data-elasticsearch基于注解生成. 在基于注解生成这种方式上 ...
随机推荐
- RHCE认证过程
RHCE认证,即红帽认证工程师(Red Hat Certified Engineer)的简称,认证内容包括DNS.NFS.Samba.Sendmail.Postfix.Apache和关键安全功能的详细 ...
- 论林耐斯-Linux系统的重要性
Linux--LinNaiSi系统的重要性... ===================================================== 飞机的控制系统.银行的系统.手机的系统我们 ...
- vue-cli脚手架npm相关文件解读(2)webpack.prod.conf.js
系列文章传送门: 1.build/webpack.base.conf.js 2.build/webpack.prod.conf.js 3.build/webpack.dev.conf.js 4.bui ...
- JTAG基础知识
前言 本知识翻译收集来自http://www.fpga4fun.com,版权归原网站所有. 1.什么是JTAG:Joint Test Action Group:联合测试工作组 JTAG是一种IEEE标 ...
- DotNetCore跨平台~Quartz热部署的福音~监控文件夹的变化
在DotNetCore出来之后,同时也使用了quartz进行调度中心的设计,将它做到docker里方便部署,在之前的quartz版本里支持配置文件的方式,而现在不支持了,我们应该去想一下,为什么不去支 ...
- Coursera无法正常提交作业
如下图,upload服务貌似被墙了,想要提交作业的话需要临时科学上网.
- WEB前端规范命名
头部 header ----------------用于头部 主要内容 main ------------用于主体内容(中部) 左侧 main-left -------------左侧布局 右侧 ma ...
- XWPFDocument创建和读取Office Word文档基础篇(一)
注:有不正确的地方还望大神能够指出,抱拳了 老铁! 参考API:http://poi.apache.org/apidocs/org/apache/poi/xwpf/usermodel/XWPFDo ...
- POI设置excel某列值为文本格式
excel单元格格式默认为[常规],当某列限定必须从下拉框选择一个纯数字文本的时候,必须将单元格格式设置为[文本]. 否则即使输入的值和下拉框的一致,excel都认为输入的值是常规类型,而下拉框的值为 ...
- java TreeSet 应用
本文主要是介绍一下java集合中的比较重要的Set接口下的可实现类TreeSet TreeSet类,底层用二叉树的数据结构 * 集合中以有序的方式插入和抽取元素. * 添加到TreeSet中的元素必须 ...