一、AOP简述

AOP全称是:aspect-oriented programming,它是面向切面编号的思想核心,

AOP和OOP既面向对象的编程语言,不相冲突,它们是两个相辅相成的设计模式型

AOP技术弥补了面向对象编程思想的不足,spring aop是实现aop的一种技术,srping aop是spring框架中某个子框架或者子功能所依赖的核心。

SPring的容器并不依赖于AOP

这意味着程序员可以自己选择是否使用aop技术,aop提供强大的中间件解决方案,这使用spring ioc容器更加的完善

二、一些术语

2.1、术语

  • Cross-cutting concern:系统层面上的服务穿插到业务逻辑的处理流程之中
  • Aspect:当需要时,将其放到应用程序之上,不需要时,将其从应用程序中脱离出来
  • Advcie:是Aspect具体的实现,advice包括cross-cutting conerns的行为或者所提供的服务
  •  Joinpoint:Aspect在应用程序执行时加入业务流程的时机
  • Pointcut:指定某个aspect在那些joinpoint时被穿插至应用程序之上
  • Target:一个advice被应用的对象或者目标对象
  • Instruction:为已经编写,编译完成的类,在执行时期动态的加入一些方法而不用修改或者增加任何代码
  • Weave:被应用 到对象之上的过程

2.2、Spring对AOP的支持

纯Java语言来编写

定义pointcutes可以使用配置文件

不支持属性成员的jointpoints.(spring 设计思想认为支持属性成员的jointpoints会破坏对象的封装性)

三、Spring创建Advice

3.1、Before Advice

目标对象的方法执行之前被调用

通过实现MethodBeforeAdvice接口来实现

MethodBeforeAdvice 继承自BeforeAdvice,而BeforeAdvice又继承Advice接口,before方法会在目标对象target所指定的方法执行之前被调用执行。before返回值为void 没有返回返回值,在before方法执行完成之后,才开始执行目标对象的方法.

3.2、实例

package com.pb;
/**
* 接口
* @author Administrator
*
*/
public interface IHello { public void sayHello(String str); }
package com.pb;
/**
* 接口实现类
* @author Administrator
*
*/
public class Hello implements IHello { @Override
public void sayHello(String str) {
System.out.println("Hello "+str); } }

代理类

package com.pb;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;
/**
* 在方法执行前调用
* @author Administrator
*实现了MethodBeforeAdvcie接口
*/
public class SayHelloBeforeAdvice implements MethodBeforeAdvice { @Override
public void before(Method arg0, Object[] arg1, Object arg2)
throws Throwable {
System.out.println("====在方法执行前调用======"); } }

applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 建立目标对象实例 -->
<bean id="hello" class="com.pb.Hello"/>
<!-- 设定Advice实例-->
<bean id="sayBeforeAdvice" class="com.pb.SayHelloBeforeAdvice" />
<!-- 建立代理对象 -->
<bean id="hellProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 设置代理接口 -->
<property name="proxyInterfaces">
<value>com.pb.IHello</value>
</property>
<!-- 设置目标对象实例-->
<property name="target">
<ref local="hello"/>
</property>
<!-- 设定Advice实例-->
<property name="interceptorNames">
<list>
<value>sayBeforeAdvice</value>
</list>
</property>
</bean> </beans>

测试类

package com.pb;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* 测试类
* @author Administrator
*
*/
public class Test { public static void main(String[] args) {
ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
IHello iHello=(IHello) context.getBean("hellProxy");
iHello.sayHello("AOP"); } }

结果:

====在方法执行前调用======
Hello AOP

3.3、After Advice

在目标对象方法执行之后被调用

通过实现AfterReturningAdvice接口来实现

在以上代码上增加

package com.pb;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;
/*
* 在方法执行完后调用
* 实现 AfterRetruningAdvice接口
*/
public class SayAfterAdvice implements AfterReturningAdvice { @Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2,
Object arg3) throws Throwable {
System.out.println("=========在方法执行完后调用======="); } }

applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 建立目标对象实例 -->
<bean id="hello" class="com.pb.Hello"/>
<!-- 设定beforAdvice实例-->
<bean id="sayBeforeAdvice" class="com.pb.SayHelloBeforeAdvice" />
<!-- 设定AfterAdvice实例 -->
<bean id="sayAfterAdvice" class="com.pb.SayAfterAdvice" />
<!-- 建立代理对象 -->
<bean id="hellProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 设置代理接口 -->
<property name="proxyInterfaces">
<value>com.pb.IHello</value>
</property>
<!-- 设置目标对象实例-->
<property name="target">
<ref local="hello"/>
</property>
<!-- 设定Advice实例-->
<property name="interceptorNames">
<list>
<value>sayBeforeAdvice</value>
<value>sayAfterAdvice</value>
</list>
</property>
</bean>
</beans>

测试类不变,结果:

====在方法执行前调用======
Hello AOP
=========在方法执行完后调用=======

3.4、Around Advice

在方法执行之间和之后来执行相应的操作

要实现接口MethodInterceptor接口

package com.pb;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation; public class SayAroundAdvice implements MethodInterceptor { @Override
public Object invoke(MethodInvocation arg0) throws Throwable {
System.out.println("=========在方法执行之前做点事情");
Object result=arg0.proceed();
System.out.println("在方法执行之后做点事情=========");
return result;
} }

配置文件

<?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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd"> <!-- 建立目标对象实例 -->
<bean id="hello" class="com.pb.Hello"/>
<!-- 设定beforAdvice实例-->
<bean id="sayBeforeAdvice" class="com.pb.SayHelloBeforeAdvice" />
<!-- 设定AfterAdvice实例 -->
<bean id="sayAfterAdvice" class="com.pb.SayAfterAdvice" />
<!-- 设定AroundAdvice实例 -->
<bean id="sayRoundAdvice" class="com.pb.SayAroundAdvice" />
<!-- 建立代理对象 -->
<bean id="hellProxy" class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 设置代理接口 -->
<property name="proxyInterfaces">
<value>com.pb.IHello</value>
</property>
<!-- 设置目标对象实例-->
<property name="target">
<ref local="hello"/>
</property>
<!-- 设定Advice实例-->
<property name="interceptorNames">
<list>
<!-- <value>sayBeforeAdvice</value>
<value>sayAfterAdvice</value> -->
<value>sayRoundAdvice</value>
</list>
</property>
</bean> </beans>

3.5、THrowsAdvice

异常发生的时候,通知某个服务对象做处理

实现ThrowsAdvice接口

package com.pb;

import java.lang.reflect.Method;

import org.springframework.aop.ThrowsAdvice;

public class SayThowsAdvice implements ThrowsAdvice {

    public void afterThrowing(Method method,Object[] objs,Object target,Throwable ta){
System.out.println("异常发生: "+ta+" 抛出异常的是: "+method);
}
}

四、基于XML Schema

简化代码实现

容易对应程序进行维护

所有元素都定义在<aop:config>中

五、基于Annotation

以注解的方式对Java普通类进行标注

Spring(五)AOP简述的更多相关文章

  1. 跟着刚哥学习Spring框架--AOP(五)

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  2. Spring框架第五篇之Spring与AOP

    一.AOP概述 AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运 ...

  3. Spring 学习十五 AOP

    http://www.hongyanliren.com/2014m12/22797.html 1: 通知(advice): 就是你想要的功能,也就是安全.事物.日子等.先定义好,在想用的地方用一下.包 ...

  4. Spring学习之旅(五)--AOP

    什么是 AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是 OOP(Object-Oriented Programing,面向对象编程)的补充和完善. OO ...

  5. Spring5.0源码学习系列之Spring AOP简述

    前言介绍 附录:Spring源码学习专栏 在前面章节的学习中,我们对Spring框架的IOC实现源码有了一定的了解,接着本文继续学习Springframework一个核心的技术点AOP技术. 在学习S ...

  6. Spring实现AOP的4种方式

    了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个“时机”,这些“ ...

  7. spring的AOP

    最近公司项目中需要添加一个日志记录功能,就是可以清楚的看到谁在什么时间做了什么事情,因为项目已经运行很长时间,这个最初没有开来进来,所以就用spring的面向切面编程来实现这个功能.在做的时候对spr ...

  8. Spring 实践 -AOP

    Spring 实践 标签: Java与设计模式 AOP引介 AOP(Aspect Oriented Programing)面向切面编程采用横向抽取机制,以取代传统的纵向继承体系的重复性代码(如性能监控 ...

  9. Spring实现AOP的4种方式(转)

    转自:http://blog.csdn.net/udbnny/article/details/5870076 Spring实现AOP的4种方式 先了解AOP的相关术语:1.通知(Advice):通知定 ...

随机推荐

  1. 【.Net底层剖析】2.stfld指令-给对象的字段赋值

    .Net底层剖析目录章节 1.[深入浅出.Net IL]1.一个For循环引发的IL 2.[.Net底层剖析]2.stfld指令-给对象的字段赋值 3.[.Net底层剖析]3.用IL来理解属性 引言: ...

  2. 在Eclipse中进行HotSpot的源码调试--转

    原文地址:http://www.linuxidc.com/Linux/2015-05/117250.htm 在阅读OpenJDK源码的过程中,经常需要运行.调试程序来帮助理解.我们现在已经可以编译出一 ...

  3. LeetCode——Find Median from Data Stream

    Median is the middle value in an ordered integer list. If the size of the list is even, there is no ...

  4. CentOS6.5菜鸟之旅:U盘安装CentOS64位

    一.前言 之前下载了个CentOS7 32位版,一下就安装成功了,但由于其目录结构等与之前的CentOS版本有很大的不同,加上教程不多不利于我这种菜鸟学习,于是决定重装CentOS6.5来学习.本篇用 ...

  5. 一起Polyfill系列:让Date识别ISO 8601日期时间格式

    一.什么是ISO 8601日期时间格式 ISO 8601是国际标准化组织制定的日期时间表示规范,全称是<数据存储和交换形式·信息交换·日期和时间的表示方法>. 示例: 1. 2014-12 ...

  6. IOS开发UI基础UIPikerView的属性

    UIPikerView的属性 1. numberOfComponents:返回UIPickerView当前的列数NSInteger num = _pickerView.numberOfComponen ...

  7. 以神经网络使用为例的Matlab和Android混合编程

    由于需要在一个Android项目中使用神经网络,而经过测试发现几个Github上开源项目的训练效果就是不如Matlab的工具箱好,所以就想在Android上使用Matlab神经网络代码(可是...) ...

  8. 软件工程---阅读《构建之法》P384~391

    -阅读<构建之法>P384~391后,我充分认识到软件工程师的职业道德的重要性,具体有: 原则1:公众 原则2:客户与雇主 原则3:产品 原则4:判断 原则5:管理 原则6:职业 原则7: ...

  9. 【C#】线程之Task

    Task开启线程 有两种启动方式: 1.构造创建线程,然后启动 var taskForAction = new Task(() => { //do something }); taskForAc ...

  10. jquery实现表格内容筛选

    对于表格来说,当数据比较多的时候,我们无法一页一页的查找,这时可以通过一个搜索框来实现搜索. 对于这个搜素框,我们为了更好的体验可以利用keyup事件实现在用户输入的时候就开始筛选,而不是填完以后点击 ...