基于注解方式实现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基于注解生成. 在基于注解生成这种方式上 ...
随机推荐
- github如何搜索资料
进入自己的主页,然后点击explore→trending; 或者google搜索,如github java 后端 请参考:http://mp.weixin.qq.com/s?__biz=MzA4NTQ ...
- windows 计划任务执行python脚本
1. 查找并打开Windows计划任务 2. 创建任务 3. 输入名称 4. 通过触发器设置运行时间或周期 5. 通过操作,设置运行的脚本 a. 操作选择'启动程序' b. 程序或脚本选择 pyth ...
- php精简完全小结(linux/laravel篇)
php官网:http://www.php.netphp版本: 查看:php -version说明:None-Thread Safe就是非线程安全,在执行时不进行线程(thread)安全检查:Threa ...
- angularJS+Ionic移动端图片上传的解决办法
前端开发中经常会碰到图片上传的问题,网上的解决办法很多,可是有些图片上传的插件会有一些附属的插件,因此因为一个图片上传的问题可能额需要引入其他插件到项目中,久而久之项目会不伦不类,有时候插件之间也会有 ...
- 3.修改第一个程序来点亮LED
在上一节中已经将驱动程序框架搭建好了 接下来开始写硬件的操作(控制LED): (1)看原理图,确定引脚 (2)看2440手册 (3)写代码(需要使用ioremap()函数映射虚拟地址,在linux中只 ...
- quartz源码分析——执行引擎和线程模型
title: quartz源码分析--执行引擎和线程模型 date: 2017-09-09 23:14:48 categories: quartz tags: [quartz, 源码分析] --- - ...
- React——组件
一.创建组件 在React中有两种创建组件的方式,分别是函数形式的组件和类形式的组件 //函数形式: function Welcome(props){ return <p>this is ...
- Window下SVN服务器搭建以及客户端使用
一.下载 上一篇博客是关于Jenkins的内容,在Jenkins自动化编译时可能会自动获取版本更新进行build,那就需要用到版本更新的工具.这里使用VisualSVN Server来作为搭建svn的 ...
- 软件工程(GZSD2015)第二次作业小结
第二次作业,从4月7号开始,陆续开始提交作业.根据同学们提交的作业报告,相比第一次作业,已经有了巨大改变,大家开始有了完整的实践,对那些抽象的名词也开始有了直观的感受,这很好.然后有一些普遍存在的问题 ...
- 团队作业9——展示博客(Beta版本)
展示博客 1.团队成员的简介和个人博客地址,团队的源码仓库地址. 何琴琴(http://www.cnblogs.com/vviane/): 领导项目进行,协调各队员之间的矛盾合作,负责测试与需求分析. ...