基于注解方式实现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基于注解生成. 在基于注解生成这种方式上 ...
随机推荐
- linux 系统状态分析
查看操作系统类型---返回结果Linux 适合与字符比较 cat /proc/sys/kernel/ostype cat /proc/version shell : www.jb51.net/arti ...
- TortoiseSVN使用经验
TortoiseSVN使用经验.. SVN新下载的项目(有感叹号和对勾)正常的-- 没有标识--重启一下电脑, ---------------ipConfig----- SVN是用哪个IP地址chec ...
- Node.js之操作文件系统(二)
Node.js之操作文件系统(二) 1.创建与读取目录 1.1 创建目录 在fs模块中,可以使用mkdir方法创建目录,该方法的使用方法如下: fs.mkdir(path,[mode],callbca ...
- 学习CSS记录:选择符优先级
1.标有!important 关键字声明属性. 2.HTML中的CSS样式属性. 3.作者编辑的CSS文件模式属性. 4.用户设置的样式. 5.浏览器默认的样式. ------------------ ...
- 【Linux】修改Linux字符集
一.查看字符集 常见的几种方法: (1) [root@devhxyw03 ~]# echo $LANG zh_CN.GBK (2) [root@devhxyw03 ~]# env | grep LAN ...
- 网络协议TFTP
TFTP(Trivial File Transfer Protocol,简单文件传输协议)是TCP/IP协议族中的一个用来在客户端与服务器之间进行简单文件传输的协议.和使用TCP的文件传输协议(FTP ...
- json对象和json字符串之间的转化
json对象和json字符串之间的转化 json字符串----->json对象 使用JSON.parse()函数 var jsonStr = '{"name":"z ...
- Selenium八种基本定位方式---基于python
from selenium import webdriver driver=webdriver.Firefox() driver.get("https://www.baidu.com&qu ...
- java开发网易电话面试 一面总结
晚上八点多自己在看视频的时候突然接到杭州来的一个电话,当时觉得很奇怪,突兀,接通之后被告知是杭州网易打来的,没有简单的自我介绍,没有多余的废话,直接入主题,吓得我心里怪紧张的,完全没有准备,但是也没有 ...
- linux为用户配置java环境变量
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt226 一. 解压安装jdk 在shell终端下进入jdk-6u14-linu ...