Spring中关于AOP的实践之Scheme方式实现通知
(刚开始写东西,不足之处还请批评指正)
关于AOP的通知编写方式有两种,使用Schema-baesd或者使用AspectJ方式,本篇主要介绍Schema-baesd方式的代码实现。
(注:代码中没有添加任何业务逻辑,只是单纯的输出语句,若读者大人有什么业务逻辑希望本人实现作为参考的可以给我留言)
一. 实现通知方法
1.前置通知需要实现:MethodBeforeAdvice接口,并重写before方法
import java.lang.reflect.Method;
import org.springframework.aop.MethodBeforeAdvice; public class BeforAdvice implements MethodBeforeAdvice { @Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("准备设备,准备武器弹药,组织救援人员");
}
}
其中的几个参数:
1)Method method:切点方法
2)Object[] objects:切点方法参数(可能有多个或者没有)
3)Object o:切点所在类的对象
2.后置通知的实现需要实现:AfterReturningAdvice接口,并重写afterReturning方法
import java.lang.reflect.Method;
import org.springframework.aop.AfterReturningAdvice; public class AfterAdvice implements AfterReturningAdvice { @Override
public void afterReturning(Object o, Method method, Object[] objects, Object o1)
throws Throwable {
System.out.println("人员已救出,生命状态良好,无生命危险");
}
}
其中的几个参数解释:
1)Object o:切点方法的返回值
2)Method method:切点方法
3)Object[] onjects:切点方法参数
4)Object o1:切点所在的类的对象
3.异常通知的实现需要实现:ThrowsAdice接口,由于ThrowsAdvice接口没有方法可重写,可以自己自定义一个方法,唯一要注意的是方法参数是切点将会抛出的异常名称,如果嫌麻烦可以使用Exception作为方法参数
import org.springframework.aop.ThrowsAdvice;
public class ErrorAdvice implements ThrowsAdvice {
public void afterThrowing(Exception e)throws Throwable{
System.out.println("任务执行异常,任务失败");
}
}
4.环绕通知的实现需要实现:MethodInterceptor接口,并重写invoke方法
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class ArroundAdvice implements MethodInterceptor { @Override
public Object invoke(MethodInvocation methodInvocation) throws Throwable {
//前置通知
System.out.println("环绕通知:准备设备,准备武器弹药,组织救援人员");
//放行,调用切点方法
Object result=methodInvocation.proceed();
//后置通知
System.out.println("环绕通知:人员已救出,生命状态良好,无生命危险");
return result;
}
}
二. 实现切点所在的类
package com.xkx.pojo;
public class People {
private int age;
private String name;
private String sexual;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSexual() {
return sexual;
}
public void setSexual(String sexual) {
this.sexual = sexual;
}
@Override
public String toString() {
return this.getAge()+"--"+this.getName()+"--"+this.getSexual();
}
public People() {
}
public People(int age, String name, String sexual) {
this.age = age;
this.name = name;
this.sexual = sexual;
}
public void crraped(){
System.out.println(this.getName()+"已被绑架");
}
public void show(){
System.out.println("切点执行:解救"+this.getName()+"任务");
}
public void covert(){
System.out.println("解救成功,召开新闻发布会");
}
}
三. 在Spring的配置文件:applicationContext.xml中进行配置
1.添加aop依赖,可以去spring-framework中的Core technologies中搜索文档,搜索关键字:xmlns:aop
2.添加配置
<?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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd"> <bean id ="beforeAdvice" class="com.xkx.adviceDemo.BeforAdvice"></bean>
<bean id="afterAdvice" class="com.xkx.adviceDemo.AfterAdvice"></bean>
<bean id="errorAdvice" class="com.xkx.adviceDemo.ErrorAdvice"></bean>
<bean id="arroundAdvice" class="com.xkx.adviceDemo.ArroundAdvice"></bean> <aop:config>
<aop:pointcut expression="execution(* com.xkx.pojo.People.show())" id="mypeople" ></aop:pointcut>
<aop:advisor advice-ref="beforeAdvice" pointcut-ref="mypeople"></aop:advisor>
<aop:advisor advice-ref="afterAdvice" pointcut-ref="mypeople"></aop:advisor>
<aop:advisor advice-ref="errorAdvice" pointcut-ref="mypeople"></aop:advisor>
<aop:advisor advice-ref="arroundAdvice" pointcut-ref="mypeople"></aop:advisor>
</aop:config> <bean id ="people" class="com.xkx.pojo.People">
<constructor-arg index="0" name="age" type="int" value="22"></constructor-arg>
<constructor-arg index="1" name="name" type="java.lang.String" value="吾先生"></constructor-arg>
<constructor-arg index="2" name="sexual" type="java.lang.String" value="male"></constructor-arg>
</bean>
</beans>
四. 测试
package com.xkx.demotest; import com.xkx.pojo.People;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test2 { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
People people = ac.getBean("people",People.class);
people.crraped();
people.show();
people.covert(); } }
Spring中关于AOP的实践之Scheme方式实现通知的更多相关文章
- Spring中关于AOP的实践之AspectJ方式实现通知
(本文中如有不当之处,恳请批评指正) AspectJ方式的简化了通知的出现复杂度.但是对配置文件的操作复杂度有了一定的提升 一. 配置通知 package com.xkx.adviceDemo; im ...
- Spring中关于AOP的实践之概念
一.什么是AOP AOP:也称作面向切面编程 在分享几个概念执行我想先举个栗子(可能例子举得并不是特别恰当): 1.假如路人A走在大街上,被一群坏人绑架了: 2.警察叔叔接到报警迅速展开行动:收集情报 ...
- Spring官网阅读(十八)Spring中的AOP
文章目录 什么是AOP AOP中的核心概念 切面 连接点 通知 切点 引入 目标对象 代理对象 织入 Spring中如何使用AOP 1.开启AOP 2.申明切面 3.申明切点 切点表达式 excecu ...
- Spring中的AOP
什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...
- Spring中的AOP 专题
Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...
- spring中的AOP 以及各种通知 配置
理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...
- Spring学习笔记(四)—— Spring中的AOP
一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...
- 2018.12.24 Spring中的aop演示(也就是运用aop技术实现代理模式)
Aop的最大意义是:在不改变原来代码的前提下,也不对源代码做任何协议接口要求.而实现了类似插件的方式,来修改源代码,给源代码插入新的执行代码. 1.spring中的aop演示 aop:面向方面编程.不 ...
- JavaWeb_(Spring框架)认识Spring中的aop
1.aop思想介绍(面向切面编程):将纵向重复代码,横向抽取解决,简称:横切 2.Spring中的aop:无需我们自己写动态代理的代码,spring可以将容器中管理对象生成动态代理对象,前提是我们对他 ...
随机推荐
- ASP.NET Core2.1 你不得不了解的GDPR(Cookie处理)
前言 时间一晃 ASP.NET Core已经迭代到2.1版本了. 迫不及待的的下载了最新的版本,然后生成了一个模版项目来试试水. ...然后就碰到问题了... 我发现..cookie竟然存不进去了.. ...
- JAVA基础第五章-集合框架Map篇
业内经常说的一句话是不要重复造轮子,但是有时候,只有自己造一个轮子了,才会深刻明白什么样的轮子适合山路,什么样的轮子适合平地! 我将会持续更新java基础知识,欢迎关注. 往期章节: JAVA基础第一 ...
- 了解Scala反射
本篇文章主要让大家理解什么是Scala的反射, 以及反射的分类, 反射的一些术语概念和一些简单的反射例子. 什么是反射 我们知道, Scala是基于JVM的语言, Scala编译器会将Scala代码编 ...
- pytest进阶之配置文件
前言 pytest配置文件能够改变pytest框架代码的运行规则.比如修改pytest收集用例的规则,添加命令行参数等等!下面我们来一一讲解常用的一些配置项 Help 通过命令pytest --hel ...
- 吴恩达深度学习笔记1-神经网络的编程基础(Basics of Neural Network programming)
一:二分类(Binary Classification) 逻辑回归是一个用于二分类(binary classification)的算法.在二分类问题中,我们的目标就是习得一个分类器,它以对象的特征向量 ...
- Python函数小节
定义函数时,默认参数必须指向不变的对象 参数为可变对象时,正常调用的时候,结果没有问题,但是当使用默认参数的时候,结果就会和理想的有差距. In [78]: def add(L=[]): ...: L ...
- java提高(15)---java深浅拷贝
#java深浅拷贝 一.前言 为什么会有深浅拷贝这个概念? 我觉得主要跟JVM内存分配有关,对于基本数据类型,只存在栈内存,所以它的拷贝不存在深浅拷贝这个概念.而对于对象而言,一个对象的创建会在内存中 ...
- Python中的那些“坑”
1.哪个是True,哪个是False? 这里要看三组代码: # 第一组: >>>a=256 >>>b = 256 >>>a is b # 第二组: ...
- Python调用ansible API系列(一)获取资产信息
你想让ansible工作首先就需要设置资产信息,那么我们如何通过使用Python调取Ansible的API来获取资产信息呢? 要提前准备一个hosts文件 获取组或者主机 #!/usr/bin/env ...
- VS2017中使用组合项目_windows服务+winform管理_项目发布_测试服务器部署
前言:作为一名C#开发人员,避免不了常和windows服务以及winform项目打交道,本人公司对服务的管理也是用到了这2个项目的组合方式进行:因为服务项目是无法直接安装到计算器中,需要使用命令借助微 ...