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的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的 ...
随机推荐
- cisco模拟器GNS3和虚拟机VMware的整合
微软和思科环境: 在思科认证的学习中,我们需要用到许多类的模拟器,但这些模拟器并不能够更真实的模拟我们的用户机在应用中所出现的现象.因此,我们借由微软的环境来更真实地体现我们所搭建的网络中的一些应用. ...
- CentOS7 安装 Tomcat
安装 JDK Tomcat 的安装依赖 JDK,在安装 Tomcat 之前需要先安装 Java JDK.输入命令 java -version,如果显示 JDK 版本,证明已经安装了 JDK java ...
- Java导出freemarker的三种方法
在上一篇呢,我将导出word文档的想法与思路以及实现功能的代码分享了一下,在这里, 我想说的是我对导出freemarker模板路径的三种方法的理解和认知. 有错误的话希望大家帮忙指正 在接下来我会使 ...
- 在Ubuntu上安装PHPStudy组件
phpStudy for Linux (lnmp+lamp一键安装包) phpStudy Linux版&Win版同步上线 支持Apache/Nginx/Tengine/Lighttpd/IIS ...
- nyoj161 取石子 (四) 威佐夫博弈
思路:详细证明见博弈总结 如何判断威佐夫博弈的奇异局势? 对于状态(a, b),c = b - a,如果是奇异局势必定满足 a == c * (1+√5)/ 2. AC代码 #include < ...
- java网络编程(2)——UDP与TCP
首先,先介绍这两种协议: UDP:UDP 是User Datagram Protocol的简称, 中文名是用户数据报协议,是OSI(Open System Interconnection,开放式系统互 ...
- scrapy安装的问题
Found existing installation: six 1.4.1 DEPRECATION: Uninstalling a distutils installed project (six) ...
- JVM笔记6-垃圾回收概述
JVM进行垃圾回收时要考虑哪的问题如下: 1.如何判定对象为垃圾对象? 1.引用计数法:在对象中添加一个引用计数器,当有地方引用这个对象的时候,引用计数器的值就+1,引用失效的时候,计数器的值就-1, ...
- Linux定时及mysql远程
(1)crontab crontab使用方法: (1)使用命令 crontab -e 然后直接编辑定时脚本. 这样执行以后,属于用户自定义的,会被写到 /var/spool/cron 目录下,生成一个 ...
- 遍历对象属性(for in、Object.keys、Object.getOwnProperty)
js中几种遍历对象的方法,包括for in.Object.keys.Object.getOwnProperty,它们在使用场景方面各有不同. for in 主要用于遍历对象的可枚举属性,包括自有属性. ...