Spring AOP梳理
一、Srping AOP
AOP(Aspect Oriented Programming)解释为面向切面编程,何为切面,用刀把一块面包切成两半,刀切下去形成的面就叫切面,那么面向切面的就是形成切面的这把刀,刀切在哪(切入点),怎么切(通知),切成什么样(通知实现方法),切的过程就是切面织入的过程。这种编程方式主要为了分离关注点并且能够增强类的辅助功能。比如做日志管理,权限控制等工作,在特定的时候和位置做特定的事情,而无需在原来方法中做特殊处理。
AOP概念:
- 切面:多个对象或方法要实现的交叉功能的集合。
- 连接点:被关注的方法,执行了该方法会触发通知。
- 切入点:一系列连接点的集合。
- 通知:切面的实际实现,在连接点方法被执行的时候在适当的地方插入通知。
- before:前置通知,在方法被调用之前调用
- after:后置通知,在方法执行结束之后调用,无论方法是否执行成功
- around:环绕通知,在目标方法执行前和执行结束之后分别执行自定义方法
- after-return:在方法成功执行之后调用,
- after-throwing:在方法抛出异常后调用
- 目标对象:被通知的对象,可以是自己的编写的类也可以是,第三方类对象。(连接点就是目标对象中的某些方法)
- 代理:将通知应用到目标对象后创建的对象。
- 织入:将切面应用到目标对象从而创建一个新的代理对象的过程。
- 引入:为类添加新的方法和属性。(<aop:declare-parents ../>)
二、AOP配置范例:
1.spring中的aop的xml配置方式简单实例
2.spring中aop的注解实现方式简单实例
3.利用切面为特定的类添加新功能:
<aop:config>
<!-- 两个切面-->
<aop:aspect ref="company">
<!-- 利用切面为特定的类添加新功能 -->
<aop:declare-parents types-matching="com.springAop.InstanceMine+" <!-- 实现了InstanceMine接口的实现类可以强转成InstanceOtheer类型的对象并使用他的实现类的方法的方法 -->
implement-interface="com.springAop.InstanceOther"
default-impl="com.springAop.OtherImpl"/> <!--InstanceOther的默认实现类,当InstanceMine对象强转成InstanceOther的对象时,默认实现类为OtherImpl-->
</aop:aspect>
</aop:config>
接口类:instanceMine和instanceOther
public interface InstanceMine {
public void speak1(String meg);
}
public interface InstanceOther {
public void speak2(String meg);
}
接口实现类: OtherImple和MineImpl
public class MineImpl implements instanceMine {
public void speak1(String meg) {
System.out.println("s1");
}
}
public class OtherImpl implements instanceOther {
public void speak2(String meg) {
System.out.println(meg);
}
}
测试类:Test
public class Test {
@org.junit.jupiter.api.Test
public void TestDemo(){
ApplicationContext app = new FileSystemXmlApplicationContext("src/main/java/com/springAop/bean.xml");
InstanceMine mine = app.getBean("mine",instanceMine.class);
((InstanceOther)mine).speak2("我可以说话吗");//强转成InstanceOther,并调用实现类OtherImpl的实现方法
}
}
结果:

三.以代理对象的方式实现AOP:
首先理解一下三种代理模式:理解三种代理模式
1.前置通知:
/**
* 前置通知:实现MethodBeforeAdvice接口,在目标方法执行之前执行
* 相当于aop配置<aop:before method="before" pointcut-ref="当前类的bean"/>
*/
public class BeforeNotify implements MethodBeforeAdvice { public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("=========== 前置通知 =========");
}
}
2.后置通知:
/**
* 后置(返回)通知:在方法结束返回时调用,一般包含在环绕通知结束前执行,
* 方法成功执行有效,异常结束则该方法无效。
* 相当于aop配置:<aop:after-returning method="afterReturning" pointcut-ref="当前类的bean"/>
*/
public class AfterNotify implements AfterReturningAdvice {
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("=========== 后置通知 =========");
}
}
3.环绕通知:
/**
* 环绕通知:在方法执行前和执行后均可以执行自定义的方法,
* 相当于aop配置:<aop:around method="invoke" pointcut-ref="当前类的Bean"/>
*/
public class AroundNotify implements MethodInterceptor { public Object invoke(MethodInvocation methodInvocation) throws Throwable {
System.out.println("=========== 环绕通知 =========");
Object obj = methodInvocation.proceed();//执行目标方法
System.out.println("=========== 环绕通知 =========");
return obj;
}
}
4.异常通知:
/**
* 异常通知:在目标方法执行异常时执行,
* 注意:ThrowsAdvice是个空接口,里面未定义任何待实现类
* 相当于aop配置:<aop:after-throwing method="afterThrowing" pointcut-ref="当前类的bean"/>
*/
public class ExceptionNotify implements ThrowsAdvice {
public void afterThrowing(Method method, Object[] args, Object target, Exception ex){
System.out.println("方法:"+method.getName()+"出错了\n原因:"+ex.getMessage());
}
}
此处的ThrowAdvice接口是没有实现方法的,但是又不允许随便定义,在源码中我们看到了规定的几个方法,因为此接口中,没有任何实现方法,当被利用反射机制调用的时候,必须实现一下方法中的一种,这是源码注释的说明:


5.bean配置
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- 被代理对象 -->
<bean class="com.springAop.proxyAOP.UserDao" id="userDao"/> <!-- 关联通知 -->
<bean class="com.springAop.proxyAOP.BeforeNotify" id="before"/>
<bean class="com.springAop.proxyAOP.AroundNotify" id="aroundNotify"/>
<bean class="com.springAop.proxyAOP.ExceptionNotify" id="exceptionNotify"/>
<bean class="com.springAop.proxyAOP.AfterNotify" id="afterNotify"/> <!-- 代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 代理对象接口,接口方法就是连接点 -->
<property name="proxyInterfaces">
<list>
<!-- 目标对象实现的接口类,实现了那些接口都可以补充进去,接口的方法就是一个个连接点 -->
<value>com.springAop.proxyAOP.IUserDao</value>
<value>com.springAop.proxyAOP.BookDao</value>
</list>
</property> <!-- 关联通知类型 -->
<property name="interceptorNames">
<list>
<value>before</value><!-- 前置通知织入 -->
<value>aroundNotify</value><!-- 环绕通知织入 -->
<value>exceptionNotify</value><!-- 异常通知织入 -->
<value>afterNotify</value><!-- 后置通知织入 -->
</list>
</property> <!-- 关联被代理对象(aop的目标对象) -->
<property name="target" ref="userDao"/>
</bean>
</beans>
6.目标对象:(实现了两个接口)
public class UserDao implements IUserDao,BookDao{
public void save() {
System.out.println("保存用户信息中。。。。");
// int i=1/0;
}
public void getBook() {
System.out.println("拿到一本书");
}
}
7.测试类:
public class Test{
@org.junit.jupiter.api.Test
public void testDemo(){
ApplicationContext app = new FileSystemXmlApplicationContext("file:G:\\DevelopSoftware\\IDEA\\workspace\\springDemo\\src\\main\\java\\com\\springAop\\proxyAOP\\bean.xml");
/**
* 获取的bean是目标对象的代理对象,可以将代理对象强转成任何目标对象实现的接口对象
*/
IUserDao userDao = app.getBean("proxyFactoryBean",IUserDao.class);
userDao.save();
System.out.println("=========================");
/**
* 将代理对象强转成BookDao接口对象,并调用该接口方法。
*/
((BookDao)userDao).getBook();
}
}
运行结果:

Spring AOP梳理的更多相关文章
- Spring Aop 梳理
Aspect Oriented Programming 面向切面编程.解耦是程序员编码开发过程中一直追求的.AOP也是为了解耦所诞生. 具体思想是:定义一个切面,在切面的纵向定义处理方法,处理完成之 ...
- [转]彻底征服 Spring AOP 之 理论篇
基本知识 其实, 接触了这么久的 AOP, 我感觉, AOP 给人难以理解的一个关键点是它的概念比较多, 而且坑爹的是, 这些概念经过了中文翻译后, 变得面目全非, 相同的一个术语, 在不同的翻译下, ...
- [Spring框架]Spring AOP基础入门总结一.
前言:前面已经有两篇文章讲了Spring IOC/DI 以及 使用xml和注解两种方法开发的案例, 下面就来梳理一下Spring的另一核心AOP. 一, 什么是AOP 在软件业,AOP为Aspect ...
- 转载:Spring AOP (下)
昨天记录了Spring AOP学习的一部分(http://www.cnblogs.com/yanbincn/archive/2012/08/13/2635413.html),本来是想一口气梳理完的.但 ...
- (转)spring aop(下)
昨天记录了Spring AOP学习的一部分(http://www.cnblogs.com/yanbincn/archive/2012/08/13/2635413.html),本来是想一口气梳理完的.但 ...
- 彻底征服 Spring AOP 之 理论篇
基本知识 其实, 接触了这么久的 AOP, 我感觉, AOP 给人难以理解的一个关键点是它的概念比较多, 而且坑爹的是, 这些概念经过了中文翻译后, 变得面目全非, 相同的一个术语, 在不同的翻译下, ...
- Spring AOP 学习例子
http://outofmemory.cn/code-snippet/3762/Spring-AOP-learn-example 工作忙,时间紧,不过事情再多,学习是必须的.记得以前的部门老大 ...
- Spring AOP 知识点入门
一.基本知识点 1.AOP概念 AOP(Aspect-Oriented Programming), 即 面向切面编程, 它与 OOP( Object-Oriented Programming, 面向对 ...
- Spring AOP概述
一.AOP的基本概念: 首先先给出一段比较专业的术语: 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的 ...
随机推荐
- css绘制倒三角
<style> i{ border-left: 5px solid transparent; border-right: 5px solid transparent; border-top ...
- Python基本格式化输出
什么叫格式化输出? 数据按照某种特殊的要求输出 假如输入一个整数,希望整数按照十六进制,八进制输出,如果输入一个小数,希望小数保留后面2位数然后输出,或者以科学计数法的方式来输出小数.字符串的输出希望 ...
- 【ASP.NET Core】解决“The required antiforgery cookie "xxx" is not present”的错误
当你在页面上用 form post 内容时,可能会遇到以下异常: The required antiforgery cookie "????????" is not present ...
- ubuntu16.04 python3 安装selenium及环境配置
环境 ubuntu16.04 python3 安装selenium sudo pip3 install seleium 默认安装完是支持firefox,但是更新得太慢对于较新的firefox已经不支持 ...
- IM开发基础知识补课:正确理解前置HTTP SSO单点登陆接口的原理
1.前言 一个安全的信息系统,合法身份检查是必须环节.尤其IM这种以“人”为中心的社交体系,身份认证更是必不可少. 一些PC时代小型IM系统中,身份认证可能直接做到长连接中(也就是整个IM系统都是以长 ...
- SpringBoot 中常用注解
本篇博文将介绍几种SpringBoot 中常用注解 其中,各注解的作用为: @PathVaribale 获取url中的数据 @RequestParam 获取请求参数的值 @GetMapping 组合注 ...
- iOS原生和H5的相互调用
为什么现在越来越多的APP中开始出现H5页面? 1,H5页面开发效率更高,更改更加方便: 2,适当缩小APP安装包的大小: 3,蹭热点更加方便,比如五一,十一,双十一搞活动: 那么为什么说H5无法取代 ...
- hihocoder 1054 滑动解锁 dfs
详细分析见滑动解锁分析 AC代码 #include <cstdio> #include <cmath> #include <cctype> #include < ...
- LOJ6277~6285 数列分块入门
Portals 分块需注意的问题 数组大小应为,因为最后一个块可能会超出的范围. 当操作的区间在一个块内时,要特判成暴力修改. 要清楚什么时候应该+tag[t] 数列分块入门 1 给出一个长为的数列, ...
- mysql 导出每张表中的100条数据..............
windows下配好MYSQL 环境变量,cmd 然后: mysqldump -uroot -p123 [数据库名]--where "1=1 limit 100" --lock-a ...