17Spring_AOP编程(AspectJ)_AspectJ的注解编程
前面的各种Aop编程,都是基于XML的,这篇文章讲的是把XML方式改为注解方式来做。
Spring注解开发和xml开发所需要的包是一样的,所以只要把xml开发方式的包复制到以注解为开发方式的包的项目下就可以了。
第一步:导入相应的jar包
第二步:需要在applicationContext.xml中引入aop的名称空间
开启注解自动代理:
- <!-- 自动注解 -->
- <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
第三步:AOP编程:
第(一)步.目标对象:
- //编写目标类(真正被代理的)
- public class UserDaoImpl implements UserDao {
- public int delete() {
- System.out.println("删除了");
- return 1;
- }
- }
第(二)步:编写切面类和Advice方法
- //这个注解表明这个类就是切面类,在这个类里面可以写Advice(通知)方法。
@Aspect- public class Myspect {
- //我们配置一个前置方法的Advice方法,这是一个Advice方法,当然要配置切点了。配了切点就表示在这个切点执行这个Advice方法。
- @Before("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
- public void before1(JoinPoint joinPoint)
- {
- System.out.print("this is 前置方法");
- }
- //后置通知,这是一个Advice方法,当然要配置切点了。配了切点就表示在这个切点执行这个Advice方法。
- @AfterReturning(value="execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))",returning="returnVal")
- public void afterReturning(JoinPoint joinPoint,Object returnVal)
- {
- System.out.println("this is afterReturning 方法");
- System.out.println(returnVal);
- }
- }
第四步:在Spring配置文件中配置目标Bean和切面Bean
- <!-- xml配置目标bean和切面bean -->
- <bean id="UserDao" class="com.guigu.shen.anotion.UserDaoImpl"></bean>
- <bean id="Myspect" class="com.guigu.shen.anotion.Myspect"></bean>
接下来给出Aspect5种通知的注解形式的具体实例
前置通知@Before
- //前置通知
@Before("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")- public void before1(JoinPoint joinPoint)
- {
- System.out.print("this is 前置方法");
- }
后置通知:
- //后置通知
- @AfterReturning(value="execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))",returning="returnVal")
- public void afterReturning(JoinPoint joinPoint,Object returnVal)
- {
- System.out.println("this is afterReturning 方法");
- System.out.println(returnVal);
- }
环绕通知:
- //环绕通知,在方法的前后执行,在工作中可以计算方法执行的时间,性能的监控,权限的设置,缓存的实行等
- @Around("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
- public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
- {
- long begintime=System.currentTimeMillis();
- Object result=proceedingJoinPoint.proceed();
- long endtime=System.currentTimeMillis();
- System.out.println("方法执行了"+(endtime-begintime)+"时间");
- return result;
- }
异常通知:指的是当发生一些异常时会输出异常信息(如果方法没有异常的话,这个通知不会被执行的,比如如果.UserDaoImpl中的delete()方法会产生一个异常,
那么这个切点通知方法会执行,如果UserDaoImpl中的delete()方法没哟异常就不会执行这个方法)
- @AfterThrowing(value="execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))",throwing="e")
- public void afterThrowing(JoinPoint joinPoint,Exception e)
- {
- System.out.print(joinPoint.toLongString()+"方法发生了异常"+e.getMessage());
- }
情形一:
被代理类的方法:
- //编写目标类(真正被代理的)
- public class UserDaoImpl implements UserDao {
- public int delete() {
- System.out.println("删除了");
- //int a=1/0;
- return 1;
- }
- }
测试方法:
- @Test
- public void deletetest()
- {
- userDao.delete();
- }
结果:不会输出异常的信息。
情形二:
被代理类的方法:
- //编写目标类(真正被代理的)
- public class UserDaoImpl implements UserDao {
- public int delete() {
- System.out.println("删除了");
- int a=1/0;
- return 1;
- }
- }
测试方法:
- @Test
- public void deletetest()
- {
- userDao.delete();
- }
结果:execution(public abstract int com.guigu.shen.anotion.UserDao.delete())方法发生了异常/ by zero18:36:25,064 INFO GenericApplicationContext:1042 - Closing org.springframework.context.support.GenericApplicationContext@24f9fdc: startup date [Mon Aug 01 18:36:07 CST 2016]; root of context hierarchy
最终通知:
- @After("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
- public void after()
- {
- System.out.print("最终通知,释放资源");
- }
结果:
this is 前置方法
删除了
方法执行了0时间
最终通知,释放资源
this is afterReturning 方法
下面给出整个案例的结构图以及代码

- //定义一个切面类。以及Advice方法
- //这个注解表明这是切面类
- @Aspect
- public class Myspect {
- //我们配置一个前置方法的Advice方法,这是一个Advice方法,当然要配置切点了
- @Before("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
- public void before1(JoinPoint joinPoint)
- {
- System.out.print("this is 前置方法");
- }
- //后置通知
- @AfterReturning(value="execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))",returning="returnVal")
- public void afterReturning(JoinPoint joinPoint,Object returnVal)
- {
- System.out.println("this is afterReturning 方法");
- System.out.println(returnVal);
- }
- //环绕通知,在方法的前后执行,在工作中可以计算方法执行的时间,性能的监控,权限的设置,缓存的实行等
- @Around("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
- public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable
- {
- long begintime=System.currentTimeMillis();
- Object result=proceedingJoinPoint.proceed();
- long endtime=System.currentTimeMillis();
- System.out.println("方法执行了"+(endtime-begintime)+"时间");
- return result;
- }
- @AfterThrowing(value="execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))",throwing="e")
- public void afterThrowing(JoinPoint joinPoint,Exception e)
- {
- System.out.print(joinPoint.toLongString()+"方法发生了异常"+e.getMessage());
- }
- @After("execution(* com.guigu.shen.anotion.UserDaoImpl.*(..))")
- public void after()
- {
- System.out.print("最终通知,释放资源");
- }
- }
UserDao接口类:
- public interface UserDao {
- public int delete();
- }
UserDao接口的实现类:
- //编写目标类(真正被代理的)
- public class UserDaoImpl implements UserDao {
- public int delete() {
- System.out.println("删除了");
- //int a=1/0;
- return 1;
- }
- }
Junit测试类:
- //JUnit与Spring的整合 (用注解)
@RunWith(SpringJUnit4ClassRunner.class)- @ContextConfiguration(locations="classpath:applicationContext.xml")
- public class TestMyspect {
- @Autowired
- private UserDao userDao;
- @Test
- public void deletetest()
- {
- userDao.delete();
- }
- public void aroundTest()
- {
- userDao.delete();
- }
- }
ApplicationContext.xml文件:
- <?xml version="1.0" encoding="UTF-8"?>
- <beans xmlns="http://www.springframework.org/schema/beans"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xmlns:context="http://www.springframework.org/schema/context"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
- <!-- AspectJ AOP -->
- <!-- 自动注解 -->
- <aop:aspectj-autoproxy></aop:aspectj-autoproxy>
- <!-- xml配置目标bean和切面bean -->
- <bean id="UserDao" class="com.guigu.shen.anotion.UserDaoImpl"></bean>
- <bean id="Myspect" class="com.guigu.shen.anotion.Myspect"></bean>
- <!--
- 流程解释:
- xml配置目标bean和切面bean,这样一来bean就会被加载,对象就创建好了,然后我们在之前不是配置了一个<aop:aspectj-autoproxy></aop:aspectj-autoproxy>了吗
- 这样就能给我们创建的UserDao和Myspect对象做代理了
- -->
- </beans>
17Spring_AOP编程(AspectJ)_AspectJ的注解编程的更多相关文章
- 18Spring_AOP编程(AspectJ)_AspectJ的各种通知总结
小结: 前置通知(权限控制). 后置通知 ---- 不怎么用 环绕通知(权限控制. 性能监控. 缓存技术 ) 异常通知 (发生异常后, 记录错误日志 ) 最终通知 (释放资源 ) 环绕通知 是取代任何 ...
- Spring 注解(一)Spring 注解编程模型
Spring 注解(一)Spring 注解编程模型 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 注解系列 ...
- Spring 注解编程之注解属性别名与覆盖
前两篇文章咱聊了深入了解了 Spring 注解编程一些原理,这篇文章我们关注注解属性方法,聊聊 Spring 为注解的带来的功能,属性别名与覆盖. 注解属性方法 在进入了解 Spring 注解属性功能 ...
- 理解 Spring 注解编程模型
理解 Spring 注解编程模型 Spring 中有一个概念叫「元注解」(Meta-Annotation),通过元注解,实现注解的「派生性」,官方的说法是「Annotation Hierarchy」. ...
- 跟Evan学Sprign编程思想 | Spring注解编程模式【译】
Spring注解编程模式 概况 多年来,Spring Framework不断发展对注解.元注解和组合注解的支持. 本文档旨在帮助开发人员(Spring的最终用户以及Spring Framework和S ...
- [.net 面向对象编程基础] (2) 关于面向对象编程
[.net 面向对象编程基础] (2) 关于面向对象编程 首先是,面向对象编程英文 Object-Oriented Programming 简称 OOP 通俗来说,就是 针对对象编程的意思 那么问 ...
- 5天玩转C#并行和多线程编程 —— 第五天 多线程编程大总结
5天玩转C#并行和多线程编程系列文章目录 5天玩转C#并行和多线程编程 —— 第一天 认识Parallel 5天玩转C#并行和多线程编程 —— 第二天 并行集合和PLinq 5天玩转C#并行和多线程编 ...
- AspectJ获取方法注解的信息
在使用Aspectj获取方法注解信息的时候,可以使用下面的代码片段: /** * Get value of annotated method parameter */ private <T ex ...
- linux tcp/ip编程和windows tcp/ip编程差别以及windows socket编程详解
最近要涉及对接现有应用visual c++开发的tcp客户端,花时间了解了下windows下tcp开发和linux的差别,从开发的角度而言,最大的差别是头文件(早期为了推广尽可能兼容,后面越来越扩展, ...
随机推荐
- ubuntu下nagios配置
参考文献: http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html http://my.oschina.net/duangr/blo ...
- vsphere vcenter server下安装ubuntu的vmwaretools
0.参考文献 百度经验:这里面是以redhat桌面版为实例进行介绍的,我的环境是ubuntu-server,虽然不一样,也可以参考 http://jingyan.baidu.com/article/2 ...
- view.performClick()触发点击事件
1.主要作用 自动触发控件的点击事件 2.界面的布局文件 activity_main.xml <RelativeLayout xmlns:android="http://schema ...
- 【原】iOSCoreAnimation动画系列教程(二):CAKeyFrameAnimation【包会】
在上一篇专题文章[原]iOSCoreAnimation动画系列教程(一):CABasicAnimation[包会]中我们学习了iOS核心动画CoreAnimation中CABasicAnimation ...
- XML学习总结(一)——XML介绍
一.XML概念 Extensible Markup Language,翻译过来为可扩展标记语言.Xml技术是w3c组织发布的,目前推荐遵循的是W3C组织于2000发布的XML1.0规范. 二.学习XM ...
- yum安装mariadb
安装mysql yum install mariadb mariadb-server MySQL-python mysql-devel Package MySQL-python-1.2.5-1.ibm ...
- ubuntu下安装wireshark
ubuntu下安装wireshark download: http://www.wireshark.org/download.html choose source code 安装编译工具: $s ...
- ASP.NET Session的共享
注: 在ashx文件中使用Session 首先添加引用 using System.Web.SessionState; 实现接口 public class XXXX: IHttpHandler ==&g ...
- redis客户端--jedis
一.jedis jedis 是 redis推荐的java客户端.通过Jedis我们可以很方便地使用java代码的方式,对redis进行操作.jedis使用起来比较简单,它的操作方法与redis命令相类 ...
- java代码调用oracle存储过程
一.简介 与调用mysql存储过程类型过程都是如下 1.创建连接 Connection conn = DriverManager.getConnection(url, user, password); ...