一、概述

Aop(aspect oriented programming面向切面编程),是spring框架的另一个特征。AOP包括切面、连接点、通知(advice)、切入点(pointCut) 。

1.aop几个概念:

  • 横切关注点: 对哪些方面进行拦截,拦截后怎么处理。
  • 切面(aspect):切面是横切关注点的抽象。
  • 连接点(joinpoint):被拦截的方法
  • 切入点(pointcut):对连接点进行拦截的定义。
  • 通知(advice):拦截到连接点之后要执行的代码
  • 目标对象:代理的目标对象
  • 织入
  • 引入

2.主要功能:

  • 日志记录
  • 性能统计
  • 安全控制
  • 事物处理
  • 异常处理

3.advice类型:

  • 前置通知(before advice)
  • 返回后通知(after returning advice)
  • 抛出异常后通知(after throwing advice)
  • 后通知(after advice)
  • 环绕通知(around advice)

4.Spring对AOP的支持

Spring中AOP代理由Spring的IOC容器负责生成、管理,其依赖关系也由IOC容器负责管理。因此,AOP代理可以直接使用容器中的其它bean实例作为目标,这种关系可由IOC容器的依赖注入提供。

二、基于xml配置的aop

在spring基于schemel中,aop需要声明一个切面aspect,一个pointcut,一个advisor.

举个例子:

切面:

public class MoocAspect {

   public void before() {
System.out.println("MoocAspect before.");
} }

切点:

public class AspectBiz {

   public void biz() {
System.out.println("AspectBiz biz.");
// throw new RuntimeException();
}

配置文件:


<?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-4.0.xsd"> <bean id="moocAspect" class="com.imooc.aop.schema.advice.MoocAspect"></bean> <bean id="aspectBiz" class="com.imooc.aop.schema.advice.biz.AspectBiz"></bean> <aop:config>
<aop:aspect id="moocAspectAOP" ref="moocAspect">
<aop:pointcut expression="execution(* com.imooc.aop.schema.advice.biz.*Biz.*(..))" id="moocPiontcut"/>
<aop:before method="before" pointcut-ref="moocPiontcut"/>
</aop:aspect>
</aop:config> </beans>

单元测试:

@RunWith(BlockJUnit4ClassRunner.class)
public class TestAOPSchemaAdvice extends UnitTestBase { public TestAOPSchemaAdvice() {
super("classpath:spring-aop-schema-advice.xml");
} @Test
public void testBiz() {
AspectBiz biz = super.getBean("aspectBiz");
biz.biz();
}
}

运行:

MoocAspect before.

AspectBiz biz.

这是 机遇 schemel配置使用aop,其实在spring 1.2版本是有api的,基于api配置aop很麻烦,但是也也应该了解下

三、基于spring api方式配置aop

直接上代码:

接口中有两个方法,一个基于aop,会被拦截 ,另外一个不会被拦截。

public interface IAopService {

	public void withAop() throws Exception;

	public void withoutAop() throws Exception;

}

实现类:

public class AopServiceImpl implements IAopService {

	private String name="forezp";

	public void withAop() throws Exception {

		System.out.println("with aop run: " + name);

		if (name.trim().length() == 0)
throw new AccountException("name cannot be null");
} public void withoutAop() throws Exception {
System.out.println("without aop");
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} }

方法前拦截器,实现MethodBeforeAdvice,在制定方法前会被调用。


public class MethodBeforeInterceptor implements MethodBeforeAdvice { public void before(Method method, Object[] args, Object instance)
throws Throwable { System.out.println("method invoke:" + method.getName()); if (instance instanceof AopServiceImpl) { String name = ((AopServiceImpl) instance).getName(); if (name == null)
throw new NullPointerException("name cannot be null");
} } }

返回后拦截器,执行指定方法后会被调用。

public class MethodAfterInterceptor implements AfterReturningAdvice {

	public void afterReturning(Object value, Method method, Object[] args,
Object instance) throws Throwable { System.out.println("method " + method.getName() + "had finished and return value-" + value); } }

异常拦截器,当出现异常时拦截:


public class ThrowsInterceptor implements ThrowsAdvice { public void afterThrowing(Method method, Object[] args, Object instance,
AccountException ex) throws Throwable { System.out.println("method" + method.getName() + " throws exception:" + ex);
} public void afterThrowing(NullPointerException ex) throws Throwable { System.out.println("the exception:" + ex);
} }

三个拦截器和Servce实现类需要配置到spring中。实际上,spring无法组装,需要借助代理类,把拦截器安装到NameMatchMethodPointcutAdvisor中,把自定义的bean安装到ProxyFactoryBean中,然后组装在一起:


<?xml version="1.0" encoding="UTF-8"?>
<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-2.0.xsd"> <!-- 拦截器 在 withAop() 方法前运行 -->
<bean id="aopMethodBeforeInterceptor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice">
<bean
class="com.imooc.aop.example2.MethodBeforeInterceptor" />
</property>
<property name="mappedName" value="withAop"></property>
</bean> <!-- 拦截器 在 withAop() 返回后运行 -->
<bean id="aopMethodAfterInterceptor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice">
<bean
class="com.imooc.aop.example2.MethodAfterInterceptor" />
</property>
<property name="mappedName" value="withAop"></property>
</bean> <!-- 拦截器 在异常抛出后运行 -->
<bean id="aopThrowsInterceptor"
class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice">
<bean
class="com.imooc.aop.example2.ThrowsInterceptor" />
</property>
<property name="mappedName" value="withAop"></property>
</bean> <bean id="aopService"
class="org.springframework.aop.framework.ProxyFactoryBean">
<!-- 拦截器 -->
<property name="interceptorNames">
<list>
<value>aopMethodBeforeInterceptor</value>
<value>aopMethodAfterInterceptor</value>
<value>aopThrowsInterceptor</value>
</list>
</property>
<!-- 被拦截的对象 -->
<property name="target">
<bean
class="com.imooc.aop.example2.AopServiceImpl"> <property name="name" value="forezp"></property> </bean>
</property>
</bean> </beans>

单元测试:



method invoke:withAop

with aop run: forezp

method withAophad finished and return value-forezp

without aop




扫码关注公众号有惊喜

(转载本站文章请注明作者和出处 方志朋的博客

Spring详解篇之 AOP面向切面编程的更多相关文章

  1. Spring详解(五)------面向切面编程

    .AOP 什么? AOP(Aspect Oriented Programming),通常称为面向切面编程.它利用一种称为"横切"的技术,剖解开封装的对象内部,并将那些影响了多个类的 ...

  2. Spring框架(四)AOP面向切面编程

    一.前言 在以前的项目中,很少去关注spring aop的具体实现与理论,只是简单了解了一下什么是aop具体怎么用,看到了一篇博文写得还不错,就转载来学习一下,博文地址:http://www.cnbl ...

  3. Spring Boot笔记九:AOP面向切面编程

    我参考的这篇文章,以验证身份为例讲解了什么是AOP AOP 这里只讲解一下怎么去实现AOP 新建一个类,叫HttpAspect用来切面 package com.vae.springboot.study ...

  4. Spring:AOP面向切面编程

    AOP主要实现的目的是针对业务处理过程中的切面进行提取,它所面对的是处理过程中的某个步骤或阶段,以获得逻辑过程中各部分之间低耦合性的隔离效果. AOP是软件开发思想阶段性的产物,我们比较熟悉面向过程O ...

  5. Spring Boot2(六):使用Spring Boot整合AOP面向切面编程

    一.前言 众所周知,spring最核心的两个功能是aop和ioc,即面向切面和控制反转.本文会讲一讲SpringBoot如何使用AOP实现面向切面的过程原理. 二.何为aop ​ aop全称Aspec ...

  6. 详细解读 Spring AOP 面向切面编程(二)

    本文是<详细解读 Spring AOP 面向切面编程(一)>的续集. 在上篇中,我们从写死代码,到使用代理:从编程式 Spring AOP 到声明式 Spring AOP.一切都朝着简单实 ...

  7. 浅谈Spring AOP 面向切面编程 最通俗易懂的画图理解AOP、AOP通知执行顺序~

    简介 我们都知道,Spring 框架作为后端主流框架之一,最有特点的三部分就是IOC控制反转.依赖注入.以及AOP切面.当然AOP作为一个Spring 的重要组成模块,当然IOC是不依赖于Spring ...

  8. Spring 08: AOP面向切面编程 + 手写AOP框架

    核心解读 AOP:Aspect Oriented Programming,面向切面编程 核心1:将公共的,通用的,重复的代码单独开发,在需要时反织回去 核心2:面向接口编程,即设置接口类型的变量,传入 ...

  9. Javascript aop(面向切面编程)之around(环绕)

    Aop又叫面向切面编程,其中“通知”是切面的具体实现,分为before(前置通知).after(后置通知).around(环绕通知),用过spring的同学肯定对它非常熟悉,而在js中,AOP是一个被 ...

随机推荐

  1. Android NDK开发 Jni中打日志LOG(二)

    HelloJni.c文件中,加入头文件和函数声明.最终文件如下: #include <jni.h> #include <string.h> #include<androi ...

  2. Barty's Computer 字典树

    https://nanti.jisuanke.com/t/17122 Barty have a computer, it can do these two things. Add a new stri ...

  3. thinkphp3.2 删除Runtime目录里的缓存文件,标记一下,以下好找。

    操作如下: utility::clearCache("Data"); 或 utility::clearCache("Data-Logs"); class uti ...

  4. [一点一滴.NET]进程和线程的区别

    进程是“执行中的程序”,是一个动态的概念.我们使用IDE编写的程序是静态的,静态程序经过编译形成EXE文件,运行起来之后就形成了一个进程.进程不仅仅是程序的代码,还包含了程序运行时的活动信息,通常由程 ...

  5. windows下自动更改IP的小工具(bat批处理文件)

    每次上线,都要先上灰度环境再上到正式环境.在上到灰度环境时,访问灰度环境通过自动获取IP和更改指定dns来实现.具体如何实现,大家可自行百度. 新建一个文本文档,将其后缀改为bat,打开该文件,拷贝一 ...

  6. JEECMS站群管理系统-- Jeecms项目导入myeclipse

    1.在myeclipse中新建一个项目jeecms,将服务器中jeecms项目下web-inf文件夹下内容拷到新建项目中 解压缩jeecms-3.0.2-final-src,在src文件夹下会看到有三 ...

  7. Spring Boot集成Reactor事件处理框架的简单示例

    1. Reactor简介 Reactor 是 Spring 社区发布的基于事件驱动的异步框架,不仅解耦了程序之间的强调用关系,而且有效提升了系统的多线程并发处理能力. 2. Spring Boot集成 ...

  8. HDU 5360——Hiking——————【贪心+优先队列】

    Hiking Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Su ...

  9. Java Knowledge series 7

    Pepole who make a greate contribution on common libaraies deserve our respect. Component(Widget) / S ...

  10. iOS 谓词(NSPredicate)的应用

    Cocoa中谓词(Predicate)提供了一个通用的查询方式处理数据,可以获取和指定数据的过滤形式,Cocoa实际开发中可以是使用NSPredicate及其父类NSComparisonPredica ...