前面的各种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 方法

下面给出整个案例的结构图以及代码

Myspect.java是切面类
//定义一个切面类。以及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的注解编程的更多相关文章

  1. 18Spring_AOP编程(AspectJ)_AspectJ的各种通知总结

    小结: 前置通知(权限控制). 后置通知 ---- 不怎么用 环绕通知(权限控制. 性能监控. 缓存技术 ) 异常通知 (发生异常后, 记录错误日志 ) 最终通知 (释放资源 ) 环绕通知 是取代任何 ...

  2. Spring 注解(一)Spring 注解编程模型

    Spring 注解(一)Spring 注解编程模型 Spring 系列目录(https://www.cnblogs.com/binarylei/p/10198698.html) Spring 注解系列 ...

  3. Spring 注解编程之注解属性别名与覆盖

    前两篇文章咱聊了深入了解了 Spring 注解编程一些原理,这篇文章我们关注注解属性方法,聊聊 Spring 为注解的带来的功能,属性别名与覆盖. 注解属性方法 在进入了解 Spring 注解属性功能 ...

  4. 理解 Spring 注解编程模型

    理解 Spring 注解编程模型 Spring 中有一个概念叫「元注解」(Meta-Annotation),通过元注解,实现注解的「派生性」,官方的说法是「Annotation Hierarchy」. ...

  5. 跟Evan学Sprign编程思想 | Spring注解编程模式【译】

    Spring注解编程模式 概况 多年来,Spring Framework不断发展对注解.元注解和组合注解的支持. 本文档旨在帮助开发人员(Spring的最终用户以及Spring Framework和S ...

  6. [.net 面向对象编程基础] (2) 关于面向对象编程

    [.net 面向对象编程基础]  (2)  关于面向对象编程 首先是,面向对象编程英文 Object-Oriented Programming 简称 OOP 通俗来说,就是 针对对象编程的意思 那么问 ...

  7. 5天玩转C#并行和多线程编程 —— 第五天 多线程编程大总结

    5天玩转C#并行和多线程编程系列文章目录 5天玩转C#并行和多线程编程 —— 第一天 认识Parallel 5天玩转C#并行和多线程编程 —— 第二天 并行集合和PLinq 5天玩转C#并行和多线程编 ...

  8. AspectJ获取方法注解的信息

    在使用Aspectj获取方法注解信息的时候,可以使用下面的代码片段: /** * Get value of annotated method parameter */ private <T ex ...

  9. linux tcp/ip编程和windows tcp/ip编程差别以及windows socket编程详解

    最近要涉及对接现有应用visual c++开发的tcp客户端,花时间了解了下windows下tcp开发和linux的差别,从开发的角度而言,最大的差别是头文件(早期为了推广尽可能兼容,后面越来越扩展, ...

随机推荐

  1. oracle10g 统计信息查看、收集

      1. 统计信息查看 1.1 单个表的全局统计信息.统计效果查看 2. 统计信息分析(收集) 2.1 分析工具选择 2.2 分析前做index重建 2.3 分析某数据表,可以在PL/SQL的comm ...

  2. Mybatis学习记录(七)----Mybatis查询缓存

    1. 什么是查询缓存 mybatis提供查询缓存,用于减轻数据压力,提高数据库性能. mybaits提供一级缓存,和二级缓存. 一级缓存是SqlSession级别的缓存.在操作数据库时需要构造 sql ...

  3. vc6.0连接mysql数据库

    一.MySQL的安装 Mysql的安装去官网下载就可以...最新的是5.7版本.. 二.VC6.0的设置 (1)打开VC6.中选0 工具栏Tools菜单下的Options选项,在Directories ...

  4. App开发流程之字符串处理工具类

    记录字符串的处理,不是一个简单的工作. NSString是代码中随处可见的类型,也是应用和处理繁多的对象,在此只记录需要常备的方法,并且加以说明. #pragma mark -- [计算字符串尺寸 + ...

  5. iOS程序启动的过程及原理

    iOS程序启动的过程及原理 文字部分 先执行main函数,main内部会调用UIApplicationMain函数 UIApplicationMain函数里面做了什么事情??? 1> 创建UIA ...

  6. .NET下金额大小写转换

    说明:金额转换可以转换50位的数值,单位从分到级.对于中间部分是否显示零,可以根据修改TranslateJInen()函数修改.中间数值为零的去掉不显示 public string GetChCapi ...

  7. 多线程在iOS开发中的应用

    多线程基本概念 01 进程 进程是指在系统中正在运行的一个应用程序.每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内. 02 线程 2-1 基本概念 1个进程要想执行任务,必须得有线程 ...

  8. Cornerstone无法上传静态库文件(.a文件)

    在用Cornerstone同步文件时出现一个错误 检查后发现是缺少了一个文件 查询了网上的资料后发现是Cornerstone自动忽略了.a文件,所以上传到svn服务器时.a文件不会跟随工程一起传上去, ...

  9. 谷歌浏览器 模拟微信浏览器user-agent

    1.F12    2.Elments->Emulation Media: Other Network:Mozilla/5.0 (Linux; Android 4.4.4; HM NOTE 1LT ...

  10. 利用 cos 组件实现jsp中上传附件

    需求:在web功能中附件上传功能为最基本的功能之一,所以用cos组件做了一个附件上传的demo.附件上传功能的实现可以利用其它的java组件实现,相关资料网上比较多. 说明步骤:下载组件并安装 --& ...