Spring(三)--AOP【面向切面编程】、通知类型及使用、切入点表达式
继承机制
封装方法
动态代理
……
3.情景举例
①数学计算器接口[MathCalculator]
int add(int i,int j);
int sub(int i,int j);
int mul(int i, int j);
public interface MathCaculator {
public int add(int i,int j);
public int sub(int i,int j);
public int mul(int i,int j);
public int div(int i,int j);
}
@Component
public class CacultorEasyImpl implements MathCaculator{
@Override
public void add(int i, int j) {
System.out.println("[日志],【参数:】"+i+","+j);
int result = i + j;
System.out.println("[日志],【参数:】"+i+","+j+"--"+result);
} @Override
public void sub(int i, int j) {
System.out.println("[日志],【参数:】"+i+","+j);
int result = i - j;
System.out.println("[日志],【参数:】"+i+","+j+"--"+result);
} @Override
public void mul(int i, int j) {
System.out.println("[日志],【参数:】"+i+","+j);
int result = i * j;
System.out.println("[日志],【参数:】"+i+","+j+"--"+result);
} @Override
public void div(int i, int j) {
System.out.println("[日志],【参数:】"+i+","+j);
int result = i / j;
System.out.println("[日志],【参数:】"+i+","+j+"--"+result);
}
}
<context:component-scan base-package="com.neuedu.aop"></context:component-scan>
③在简单实现的基础上让每一个计算方法都能够打印日志[LoginImpl]
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void test() {
CacultorEasyImpl bean = ioc.getBean(CacultorEasyImpl.class);
bean.add(10, 2);
bean.sub(10, 2);
bean.mul(10, 2);
bean.div(10, 2);
}
④缺陷
[1]手动添加日志繁琐,重复
[2]统一修改不便
[3]对目标方法本来要实现的核心功能有干扰,使程序代码很臃肿,不易于开发维护
⑤使用动态代理实现
[1]创建一个类,让这个类能够提供一个目标对象的代理对象
[2]在代理对象中打印日志
AOP概述
●AOP(Aspect-Oriented Programming,面向切面编程):是一种新的方法论
●Spring的AOP既可以使用xml配置的方式实现,也可以使用注解的方式来实现!
5.在Spring中使用AOP实现日志功能
①Spring中可以使用注解或XML文件配置的方式实现AOP。
②导入jar包
com.springsource.net.sf.cglib -2.2.0.jar
com.springsource.org.aopalliance-1.0.0 .jar
com.springsource.org.aspectj.weaver-1.6.8 .RELEASE.jar
commons-logging-1.1.3. jar
spring-aop-4.0.0.RELEASE.jar
spring-aspects-4.0.0.RELEASE.jar
spring-beans-4.0.0.RELEASE.jar
spring-context-4.0.0.RELEASE.jar
spring-core-4.0.0.RELEASE.jar
spring-expression-4.0.0.RELEASE. jar
③开启基于注解的AOP功能 < aop:aspectj-autoproxy />
<context:component-scan base-package="com.neuedu.aop"></context:component-scan>
<aop:aspectj-autoproxy/>
④声明一个切面类,并把这个切面类加入到 IOC容器中
在类上加以下两个注解
@Aspect :表示这是一个切面类
@Component :加入IOC容器
⑤在切面类中声明通知方法
[1] 前置通知:@Before
[2] 返回通知:@AfterReturning
[3] 异常通知:@AfterThrowing
[4] 后置通知:@After
[5] 环绕通知:@Around :环绕通知是前面四个通知的集合体!
@Component
@Aspect
public class CaculatorAspect { @Before(value="execution(public void com.neuedu.aop.RawCaculatorImpl.add(int, int))")
public void showBeginLog(){
System.out.println("日志开始");
} @After(value="execution(public void com.neuedu.aop.RawCaculatorImpl.add(int, int))")
public void showReturnLog(){
System.out.println("日志正常返回");
} @AfterThrowing(value="execution(public void com.neuedu.aop.RawCaculatorImpl.add(int, int))")
public void showExceptionLog(){
System.out.println("日志有错");
} @AfterReturning(value="execution(public void com.neuedu.aop.RawCaculatorImpl.add(int, int))")
public void showAfterLog(){
System.out.println("日志最终结束");
}
}
⑥被代理的对象也需要加入IOC容器
@Component
public class RawCaculatorImpl implements MathCaculator{ @Override
public void add(int i, int j) {
int result = i + j;
System.out.println(i+"+"+j+"="+result);
} @Override
public void sub(int i, int j) {
int result = i - j;
System.out.println(i+"-"+j+"="+result);
} @Override
public void mul(int i, int j) {
int result = i * j;
System.out.println(i+"*"+j+"="+result);
} @Override
public void div(int i, int j) {
int result = i / j;
System.out.println(i+"/"+j+"="+result);
}
}
Test 中 用 id 查找,通过强转,调用加减乘除四个方法
private ApplicationContext ioc = new ClassPathXmlApplicationContext("applicationContext.xml");
@Test
public void test() {
MathCaculator bean = (MathCaculator) ioc.getBean("rawCaculatorImpl");
bean.add(10, 2);
bean.sub(10, 2);
bean.mul(10, 2);
bean.div(10, 2);
}
@Component
@Aspect
public class CaculatorAspect { @Before(value="execution(public void com.neuedu.aop.RawCaculatorImpl.*(int, int))")
public void showBeginLog(){
System.out.println("日志开始");
} @After(value="execution(public void com.neuedu.aop.RawCaculatorImpl.*(int, int))")
public void showReturnLog(){
System.out.println("日志正常返回");
} @AfterThrowing(value="execution(public void com.neuedu.aop.RawCaculatorImpl.*(int, int))")
public void showExceptionLog(){
System.out.println("日志有错");
} @AfterReturning(value="execution(public void com.neuedu.aop.RawCaculatorImpl.*(int, int))")
public void showAfterLog(){
System.out.println("日志最终结束");
} }
如果方法中的参数类型不一致,可以用 (..) 代替 (int,int)
@Component
@Aspect
public class CaculatorAspect { @Pointcut(value="execution(* com.neuedu.aop.RawCaculatorImpl.*(..))")
public void showLog(){} @Before(value="showLog()")
public void showBeginLog(JoinPoint point){
Object[] args = point.getArgs();//获取参数
List<Object> asList = Arrays.asList(args);//转为list类型
Signature signature = point.getSignature();//获取签名
String name = signature.getName();//获取方法名字
System.out.println("【前置通知】目标方法名:"+name+",参数为:"+asList);
} @After(value="showLog()")
public void showReturnLog(){
System.out.println("【后置通知】日志最终返回");
} @AfterThrowing(value="showLog()",throwing="ex")
public void showExceptionLog(JoinPoint point,Exception ex){
System.out.println("【异常通知】异常信息为:"+ex.getMessage());
} @AfterReturning(value="showLog()",returning="result")
public void showAfterLog(JoinPoint point,Object result){
System.out.println("【返回通知】方法的返回值:"+result);
System.out.println();
}
}
参见第5章AOP细节:演示验证
1.任意参数,任意类型
2.任意返回值
3.用@PointCut注解统一声明,然后在其它通知中引用该统一声明即可!
需要注意的是:权限是不支持写通配符的,当然你可以写一个*表示所有权限所有返回值!
最详细的切入点表达式:
execution(public int com.neuedu.aop.target.MathCalculatorImpl.add(int, int))
最模糊的切入点表达式:
@Pointcut(value= "execution(public int com.atguigu.aop.target.EazyImpl.add(int,int))")
public void myPointCut(){}
8.通知方法的细节
①在通知中获取目标方法的方法名和参数列表
[3]调用JoinPoint对象的getArgs()方法获取目标方法的实际参数列表
@Before(value="showLog()")
public void showBeginLog(JoinPoint point){
Object[] args = point.getArgs();//获取参数
List<Object> asList = Arrays.asList(args);//转为list类型
Signature signature = point.getSignature();//获取签名
String name = signature.getName();//获取方法名字
System.out.println("目标方法名:"+name+",参数为:"+asList);
System.out.println("日志开始");
}
@AfterReturning(value="showLog()",returning="result")
public void showAfterLog(JoinPoint point,Object result){
System.out.println("方法的返回值:"+result);
System.out.println("日志正常结束");
System.out.println();
}
@AfterThrowing(value="showLog()",throwing="ex")
public void showExceptionLog(JoinPoint point,Exception ex){
System.out.println("异常信息为:"+ex.getMessage());
System.out.println("日志有错");
}
10.环绕通知:@Around
1.环绕通知需要在方法的参数中指定JoinPoint的子接口类型ProceedingJoinPoint为参数
@Around(value="pointCut()")
public void around(ProceedingJoinPoint joinPoint){
}
2.环绕通知会将其他4个通知能干的,自己都给干了!
@Around(value="execution(public * com.neuedu.aop.RawCaculatorImpl.*(..))")
public Object showLog(ProceedingJoinPoint point){
Object[] args = point.getArgs();
List<Object> asList = Arrays.asList(args);
Signature signature = point.getSignature();//获取签名
String name = signature.getName();
Object result=null;
try {
try{
System.out.println("【前置通知】目标方法名:"+name+",参数为:"+asList);
result = point.proceed(args);
}finally{
System.out.println("【后置通知】日志最终返回");
}
System.out.println("【返回通知】方法的返回值:"+result);
} catch (Throwable e) {
System.out.println("【异常通知】异常信息为:"+e.getMessage());
}
System.out.println();
return result;
}
对于同一个代理对象,可以同时有多个切面共同对它进行代理。
@Component
@Aspect
@Order(value=20)
public class BAspect { @Around(value="execution(* com.neuedu.aop.RawCaculatorImpl.*(..))")
public Object showLog(ProceedingJoinPoint point){
Object[] args = point.getArgs();
List<Object> asList = Arrays.asList(args);
Signature signature = point.getSignature();//获取签名
String name = signature.getName();
Object result=null;
try {
try{
System.out.println("【前置】目标方法名:"+name+",参数为:"+asList);
result = point.proceed(args);
}finally{
System.out.println("【后置】日志最终返回");
}
System.out.println("【返回】方法的返回值:"+result);
} catch (Throwable e) {
System.out.println("【异常】异常信息为:"+e.getMessage());
}
System.out.println();
return result;
}
}
<!-- 将需要加载到IOC容器中的bean配置好 -->
<bean id="caculatorAspect" class="com.neuedu.aop.CaculatorAspect"></bean>
<bean id="bAspect" class="com.neuedu.aop.BAspect"></bean>
<bean id="rawCaculatorImpl" class="com.neuedu.aop.RawCaculatorImpl"></bean> <!-- 配置AOP,需要导入AOP名称空间 -->
<aop:config>
<!-- 声明切入点表达式 -->
<aop:pointcut expression="execution(* com.neuedu.aop.RawCaculatorImpl.*(..))" id="myPointCut"/>
<!-- 配置日志切面类,引用前面的类 ,通过order属性控制优先级-->
<aop:aspect ref="caculatorAspect" order="20">
<!-- 通过method属性指定切面类的切面方法,通过pointcut-ref指定切入点表达式 -->
<aop:before method="showBeginLog" pointcut-ref="myPointCut"/>
<aop:after method="showAfterLog" pointcut-ref="myPointCut"/>
<aop:after-returning method="showReturnLog" pointcut-ref="myPointCut" returning="result"/>
<aop:after-throwing method="showExceptionLog" pointcut-ref="myPointCut" throwing="ex"/>
</aop:aspect>
<!-- 配置事务切面类,引用前面的类 -->
<aop:aspect ref="bAspect" order="25">
<aop:around method="showLog" pointcut-ref="myPointCut"/>
</aop:aspect> </aop:config>
需要知道的是:事务的管理是和AOP是有很大关系的,即声明式事务的底层是用AOP实现的!
Spring(三)--AOP【面向切面编程】、通知类型及使用、切入点表达式的更多相关文章
- Spring(三)AOP面向切面编程
原文链接:http://www.orlion.ga/205/ 一.AOP简介 1.AOP概念 参考文章:http://www.orlion.ml/57 2.AOP的产生 对于如下方法: pub ...
- Spring:AOP面向切面编程
AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...
- Spring 08: AOP面向切面编程 + 手写AOP框架
核心解读 AOP:Aspect Oriented Programming,面向切面编程 核心1:将公共的,通用的,重复的代码单独开发,在需要时反织回去 核心2:面向接口编程,即设置接口类型的变量,传入 ...
- spring框架学习(三)——AOP( 面向切面编程)
AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...
- Spring框架 AOP面向切面编程(转)
一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...
- spring:AOP面向切面编程02
参考: https://blog.csdn.net/jeffleo/article/details/54136904 一.AOP的核心概念AOP(Aspect Oriented Programming ...
- Spring注解 - AOP 面向切面编程
基本概念: AOP:Aspect Oriented Programming,即面向切面编程 指在程序运行期间动态的将某段代码切入到指定方法指定位置进行运行的编程方式 前置通知(@Before):在目标 ...
- Spring的AOP面向切面编程
什么是AOP? 1.AOP概念介绍 所谓AOP,即Aspect orientied program,就是面向方面(切面)的编程. 功能: 让关注点代码与业务代码分离! 关注点: 重复代码就叫做关注点: ...
- Spring框架——AOP面向切面编程
简介 AOP练习 使用动态代理解决问题 Spring AOP 用AspectJ注解声明切面 前置后置通知 利用方法签名编写AspectJ切入点表达式 指定切面的优先级 基于XML的配置声明切面 Spr ...
- Spring之AOP(面向切面编程)_入门Demo
AOP是OOP的延续,是Aspect Oriented Programming的缩写,意思是面向切面编程.AOP实际是GoF设计模式的延续,设计模式孜孜不倦追求的是调用者和被调用者之间的解耦,AOP可 ...
随机推荐
- redhat设置开机自动连接网络
一.设置开机自动连接网络1.用root账号登录2.打开etcsysconfignetwork-scrpts目录3.vi ifcfg-eth04.将ONBOOT改为yes 二.没有图形界面如何连接网络1 ...
- 谈谈localhost与127.0.0.1
localhost意为本地主机,指这台计算机,是给回路网络接口的标准主机名,对应的IP地址为127.0.0.1,可访问本地服务器的web项目(http://localhost). 那么它们有什么区别呢 ...
- npm介绍与cnpm介绍
npm介绍 说明:npm(node package manager)是nodejs的包管理器,用于node插件管理(包括安装.卸载.管理依赖等) 使用npm安装插件:命令提示符执行npm instal ...
- jmeter问题处理随笔1 - 自动遍历用例(一次)
背景: 弄了个无业务关联的巡检接口方案,需要循环获取csv中的数据,每一条数据,运行一次,直到全部运行完,但是使用后发现要通过[线程组]中设置循环变量的数据为来读取csv中的数据,这样每次修改csv用 ...
- (转)ORACLE中SID和SERVICE_NAME的区别
背景:之前一直分不清plsql和程序中配置文件url之间的连接,想当然的认为service_name 和jdburl后面的实例相对应,直到出错的这一天,通过这篇博客,彻底扫除了盲点. 1 问题 1.1 ...
- (转)Linux下查看文件和文件夹大小 删除日志
场景:在sts中执行自动部署时候maven提示No space left on device错误,后来经检查发现是磁盘空间满了,用下面的方法分析发现tomcat下面的logs目录占用了很大的空间,删除 ...
- MySQL学习笔记(六):索引
本文主要介绍MySQL 中关于索引的一些问题,例如:索引的作用:怎么创建索引:设计索引的原则:怎么优化索引等等. 一:索引概述 索引一般是通过排序,然后查找时可以二分查找,这一特点来达到加速查找的目的 ...
- Vue项目搭建完整剖析全过程
Vue项目搭建完整剖析全过程 项目源码地址:https://github.com/ballyalex 有帮助的话就加个星星呗~! 项目技术栈:vue+webpack+bower+sass+axios ...
- javascript基础知识3#引用类
引用类 引用类型的只是引用类型的一个实例,在ecmascript当中,引用类型是一种数据结构用于将数据和功能组织在一起,也常被称做类. object类型 构造函数[var o = new object ...
- 带着SMART原则重新出发
很久以来,对分布式系统都比较感兴趣,但工作中却接触不到,“虽不能至,心向往之”,于是打算自己学.分布式系统领域概念很多,错综复杂,一些理论也比较难以理解,要想学习的话还是得下一番功夫.于是在来两个月前 ...