基于注解方式实现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基于注解生成. 在基于注解生成这种方式上 ...
随机推荐
- 卸载oracle 11g数据库
完全卸载oracle11g步骤:1. 开始->设置->控制面板->管理工具->服务 停止所有Oracle服务.2. 开始->程序->oracle - OraHome ...
- Flask01 初识flask、flask配置
1 什么是flask Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2 . 百度百科:点击前往 中文文档: ...
- mapper.xml是怎样实现Dao层接口
上午写了一个简单的 从xml读取信息实例化一个Bean对象.下午就开始想mybatis是怎么通过xml文件来实现dao层接口的,一开始想直接用Class.forName(String name)然后调 ...
- Django创建博客
拜读http://www.cnblogs.com/fnng/p/3737964.html 后自操作步骤,mark一下 我的想法: modles.py中只负责添加类,定义数据结构,至于将该类添加到adm ...
- AndroidTv Home界面实现原理(二)——Leanback 库的主页卡位缩放动画源码解析
先看个效果图: 上一篇中,我们留了问题,在 Tv Home 界面这种很常见聚焦卡位放大动画效果,我们这一篇就来看看 Leanback 库是怎么实现的. 如果要我们自己实现的话,思路应该不难,就是写个放 ...
- 又想起Solaris
想起曾几何时,学习的第一个UNIX-like操作系统.只可惜,从来都是在此操作系统上用C语言编程,而没有用过Sun公司的java. 又几何时,Sun公司慢慢不行了.再后来过了几年,Sun公司把Ultr ...
- html+css手记
----------------------html定义和基本结构---------------------- HTML是 HyperText Mark-up Language 的首字母简写,意思是超 ...
- 使用 Palette 让你的 UI 色彩与内容更贴合
版权声明: 本账号发布文章均来自公众号,承香墨影(cxmyDev),版权归承香墨影所有. 每周会统一更新到这里,如果喜欢,可关注公众号获取最新文章. 未经允许,不得转载. 一.前言 今天介绍一个 An ...
- 配置HAProxy支持https协议
author:JevonWei 版权声明:原创作品 实现http重定向到https HAProxy 创建CA证书 [root@HAProxy ~]# cd /etc/haproxy/ [root@HA ...
- Spring中ApplicationContextAware的用法
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt379 一.这个接口有什么用? 当一个类实现了这个接口(Application ...