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的差别,从开发的角度而言,最大的差别是头文件(早期为了推广尽可能兼容,后面越来越扩展, ...
随机推荐
- Q:解决每天第一次打开MSCRM系统展示慢的问题
问题:第天第一次打开系统时,需要加载很长时间,基本为1分多钟,而第二次打开只需5秒. 解决方案:利用IIS中的Session. 一.打开IIS,选择打开服务器功能中“Session State”. 二 ...
- Java或Android开发中,去掉块注释格式化后每行出现的星号(*)的解决方案。(Eclipse)
找到子项,在 Window->Prefrences->Java->Code Style->Formatter,点击New新建 Active profile,然后在Comment ...
- JAVA基础学习day19--IO流一、FileWrite与FileReader
一.IO简述 1.1.简述 IO:input/output IO流用来处理设备之间的数据传输 Java对数据的操作是通过流的方式 Java用于操作流的对象都在IO包中. 1.2.结构 字节流抽象类: ...
- ubuntu15.10 给解压版的eclipse安装桌面快捷方式
在桌面用vi 建立eclipse.desktop文件,并赋予权限 sudo chmod u+x /home/liujl/Desktop/eclipse.desktop [Desktop Entry ...
- Git基本使用命令
整理Git的一些基本使用命令. # 1)克隆代码 boldseas@lian-PC MINGW64 /d/TestGroup $ git clone ssh://git@code.boldseas ...
- OOD沉思录 --- 类和对象的关系 --- 包含关系1
4.5 如果类包含另一个类的对象,那么包含类应当向被包含的对象发送消息(调用方法). 也就是说,所有的包含关系都应当是使用关系. 如果不是这样,那么包含的类有什么用处呢?当然,面向过程的开发人员会想 ...
- Effective Java 74 Implement Serializable judiciously
Disadvantage of Serializable A major cost of implementing Serializable is that it decreases the flex ...
- centos7 新手基本命令
1. yum update 安装系统后,更新yum到最新版本 提示错误 :cannot find a valid baseurl for repo: base/7/x86_64 解决:修改/etc/s ...
- Support for AMD usage of jwplayer (require js)
使用require js 模块化代码时,其中播放器用的是jwplayer7.x 然后载入jwplayer.js后总是报license无效(license已经加入),最后在jwplayer官网论坛里找到 ...
- Android Listener侦听的N种写法
Android中,View的Listener方法,在是否使用匿名类匿名对象时,有各种不同的写法. OnClickListener和其他Listener方法一样,都是View类的接口,重载实现后就能使用 ...