18-spring学习-利用Annotation配置AOP
之前是通过配置完成aop操作,如果自己写的话,太麻烦了,可以使用基于annotation的配置完成。
第一步:打开AOP的annotation支持
加上一句话:
- <context:annotation-config/>
- <context:component-scan base-package="com.Spring"/>
<aop:aspectj-autoproxy proxy-target-class="true"/> //使用了jdk的自动动态代理,需要加上这句话,否则报错- <aop:aspectj-autoproxy/>
随后需要在ServiceAspect类中编写所需要使用的annotation。
范例:修改serviceAspect类。
- package com.Spring.aop;
- import java.lang.reflect.Array;
- import java.util.Arrays;
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.AfterReturning;
- import org.aspectj.lang.annotation.AfterThrowing;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.springframework.stereotype.Component;
- import com.Spring.Vo.Member;
- @Component //用来取代在xml中配置bean初始化
- @Aspect //用来代替在xml中配置AOP操作
- public class ServiceAspect {
- @Before(value="execution(* com.Spring..*.*(..)))")
- public void serviceBefore()
- {
- System.out.println("AOP切面执行日志记录操作");
- }
- @Before(value="execution(* com.Spring..*.*(..)) and args(param)))",argNames="param")
- public void serviceBefore2(Object arg)
- {
- System.out.println("AOP切面执行增加前操作,参数=" +arg);
- }
- @After(value="execution(* com.Spring..*.*(..)))")
- public void serviceAfter()
- {
- System.out.println("AOP切面执行事务处理操作");
- }
- @AfterReturning(value="execution(* com.Spring..*.*(..)))",argNames="ret",returning="ret")
- public void serviceAfterReturn(Object val) //表示操作结果
- {
- System.out.println("AOP切面操作完成,返回结果:"+val);
- }
- @AfterThrowing(value="execution(* com.Spring..*.*(..)))",argNames="e",throwing="e")
- public void serviceAfterThrow(Exception e) //表示操作结果
- {
- System.out.println("AOP切面操作出现异常:"+e);
- }
- @Around(value="execution(* com.Spring..*.*(..)))")
- public Object serviceAround(ProceedingJoinPoint point) throws Throwable
- {
- System.out.println("AOP切面数据层方法调用之前,参数:"+Arrays.toString(point.getArgs()));
- Member vo=new Member();
- vo.setMid("TestAOP");
- vo.setName("测试AOP");
- Object retVal=point.proceed(new Object[]{ vo });
- System.out.println("AOP切面数据层方法调用之后,返回值:"+retVal);
- return true;
- }
- }
运行结果:
可以对照之前用注解的xml配置:
- <context:annotation-config/>
- <context:component-scan base-package="com.Spring"/>
- <aop:aspectj-autoproxy proxy-target-class="true"/>
- <aop:config>
- <!-- 定义程序的切入点 -->
- <aop:pointcut expression="execution(* com.Spring..*.*(..)) and args(vo))" id="pointcut"/>
- <!-- 这里ref的对象是通过annotation配置@Component出来的, -->
- <!-- 定义面向方面的处理类 -->
- <aop:aspect ref="serviceAspect">
- <!--
- <aop:before method="serviceBefore2" pointcut-ref="pointcut" arg-names="vo"/>
- <aop:after method="serviceAfter" pointcut="execution(* com.Spring..*.*(..)))"/>
- <aop:after-returning method="serviceAfterReturn" pointcut="execution(* com.Spring..*.*(..)))" returning="haha" arg-names="haha"/>
- <aop:after-throwing method="serviceAfterThrow" pointcut="execution(* com.Spring..*.*(..)))" arg-names="e" throwing="abc"/>
- -->
- <aop:around method="serviceAround" pointcut="execution(* com.Spring..*.*(..)))" />
- </aop:aspect>
- </aop:config>
实际操作中,需要进行一些辅助性功能编写的时候(比如日志记录),建议使用annotation的配置操作,这样的代码是最简化的,也是最直观的。
18-spring学习-利用Annotation配置AOP的更多相关文章
- Spring学习记录(十二)---AOP理解和基于注解配置
Spring核心之二:AOP(Aspect Oriented Programming) --- 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...
- Spring学习笔记IOC与AOP实例
Spring框架核心由两部分组成: 第一部分是反向控制(IOC),也叫依赖注入(DI); 控制反转(依赖注入)的主要内容是指:只描述程序中对象的被创建方式但不显示的创建对象.在以XML语言描述的配置文 ...
- Spring学习资料以及配置环境
一.Spring4 1.介绍 新特性 SpringIDE 插件 IOC DI 在 Spring 中配置 Bean 自动装配 Bean 之间的关系(依赖.继承) Bean 的作用域 使用外部属性文件 S ...
- Spring学习之第一个AOP程序
IOC和AOP是Spring的两大基石,AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对 ...
- Spring 学习(三)AOP
(1)AOP概述 - AOP:面向切面编程,扩展功能不修改源代码实现 - AOP采取横向抽取机制,取代了传统的纵向继承体系重复性代码 (2)AOP底层原理 原始方法------->纵向继承体系 ...
- 【JavaEE】SSH+Spring Security基础上配置AOP+log4j
Spring Oauth2大多数情况下还是用不到的,主要使用的还是Spring+SpringMVC+Hibernate,有时候加上SpringSecurity,因此,本文及以后的文章的example中 ...
- Spring学习(八)AOP详解
文章更新时间:2020/04/06 一.一个例子 在上面的例子中,包租婆的核心业务就是签合同,收房租,那么这就够了,灰色框起来的部分都是重复且边缘的事,交给中介商就好了,这就是 AOP 的一个思想:让 ...
- Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。
实现两个整数的加减乘除.在执行每个方法之前打印日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...
- Spring学习之旅(五)--AOP
什么是 AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是 OOP(Object-Oriented Programing,面向对象编程)的补充和完善. OO ...
随机推荐
- Codeforces Round #346 (Div. 2) A. Round House 水题
A. Round House 题目连接: http://www.codeforces.com/contest/659/problem/A Description Vasya lives in a ro ...
- IndiaHacks 2016 - Online Edition (Div. 1 + Div. 2) A. Bear and Three Balls 水题
A. Bear and Three Balls 题目连接: http://www.codeforces.com/contest/653/problem/A Description Limak is a ...
- PYPY_GC
Author:Jin Date: 2014-7-8 http://doc.pypy.org/en/latest/windows.html http://www.pypy.org/download.ht ...
- 微信小程序的坑
虽然官方文档,可以在.json中给页面设置背景颜色,用backgroundColor,但是实际上并不好使,所以设置背景颜色只能在wxss中设置 <import src="../comm ...
- CentOS 6.9开启iptables的日志实现调试
系统日志配置在CentOS 5上叫syslog,而在CentOS 6上叫rsyslog(增强版的syslog),CentOS 5上的配置文件在/etc/syslog.conf下,而CentOS 6在/ ...
- How to use transparent PNG icons with Delphi ImageList
http://www.aha-soft.com/faq/delphi-imagelist-png.htm Query: "Embarcadero Delphi ImageList does ...
- C# WebHelper-CookieHelper,CacheHelper,SessionHelper
常用web操作工具类,记录一下,本文记录的工具类,都要求引用 System.Web 1.CookieHelper /// <summary> /// Cookie工具类 /// </ ...
- java反射机制简单介绍
1.字节码.所谓的字节码就是当java虚拟机载入某个类的对象时,首先须要将硬盘中该类的源码编译成class文件的二进制代码(字节码),然后将class文件的字节码载入到内存中,之后再创建该类的对象 2 ...
- ffmpeg的IO操作
ffmpeg 可以通过IO操作将数据读取和存储在文件或网络中 作为数据的读取和写入地址,数据被存放在file,http, ffmpeg 不仅可以编解常用的音视频格式,还可以将数据导入/导出到各种媒介中 ...
- fdopen()和fileno()函数
转:http://book.2cto.com/201212/11763.html 文件描述字函数是流函数的初等函数,每一个流都与一个描述字相连.给定一个打开的文件描述字,可以用fdopen()函数为它 ...