基于注解方式实现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基于注解生成. 在基于注解生成这种方式上 ...
随机推荐
- 关于如何在highchart上获取后台返回的值一些问题。
项目开发过程中有用到highchart图表进行项目的开发.一个比较常规的需求就是通过点击图表上的模块进行明细的查看. 1.比如坐标一月.二月.三月.四月.....有对应的值01,02,03,04... ...
- JavaScript练习题 全局变量 局部变量 作用域
前沿:大家好~我是阿飞~本次 任何简单的事情都可以复杂化,本次让我们来做下搞事情的练习题吧 例题1: var a = 1; function fn1(){ var a = 2; alert(a); / ...
- Java入门(5)——类和对象还有构造方法
类 类和对象的概念: 类是对一群具有相同属性.行为的事物的统称. 类是抽象的. 人以类聚 物以群分 对象: 对象是现实生活中的1个具体存在. 看得 ...
- html:table属性cellpadding
cellpadding:单元格边距(空白区域) colspan:可以横跨的列数(td/th都算一列) 详细:http://www.dreamdu.com/xhtml/attribute_cellpad ...
- idea 远程调试
Idea 远程在线测试 描述:在window下开发,部署到Linux服务器上,往往会遇到在windows下正常运行,在Linux服务器下异常,这是需要本地调试远程代码: 操作步骤: 一.代码已知 保证 ...
- 【笔记】shellcode相关整理
0x01:shellcode定义 Shellcode实际是一段代码(也可以是填充数据),是用来发送到服务器利用特定漏洞的代码,一般可以获取权限.另外,Shellcode一般是作为数据发送给受攻击服务器 ...
- Mysql介绍和实践总结
本文首先介绍mysql的安装和基本使用.进阶操作.讲解mysql的导入导出和自动备份,然后介绍安全模式修改密码和mysql的全文本搜索功能,最后记录了个人使用mysql中遇到的问题集. 开始安装: s ...
- 三,ESP8266 SPI
重点是说SPI通信协议,,,, 不要害怕协议因为协议是人规定的,,刚好我也是人......规定的协议既然能成为规范让所有人所接受,那么必然有它的优势和优点,必然值得学习,, 害怕协议的人是因为当初碰到 ...
- 定宽块状元素居中 1记(text-align/margin:0 auto)
对于text-align:center的用法只是针对文本相对于父元素的居中,例如: #jz2{ width:300px; margin: 10px auto; border:2px solid red ...
- java中synchronized的使用
synchronized是Java中的关键字,是一种同步锁. synchronized分对象锁和类的锁两种. (一)通常synchronized 方法和synchronized(this){}都是属于 ...