.AOP [知识点详解]

AOP:中文名称面向切面编程
英文名称:(Aspect Oriented Programming)
正常程序执行流程都是纵向执行流程
3.1 又叫面向切面编程,在原有纵向执行流程中添加横切面 3.2 不需要修改原有程序代码 3.2.1 高扩展性 3.2.2 原有功能相当于释放了部分逻辑.让职责更加明确. 面向切面编程是什么?
4.1 在程序原有纵向执行流程中,针对某一个或某一些方法添加通知,形成横切面过程就叫做面向切面编程. 常用概念
5.1 原有功能: 切点, pointcut
5.2 前置通知: 在切点之前执行的功能. before advice 5.3 后置通知: 在切点之后执行的功能, after advice 5.4 如果切点执行过程中出现异常,会触发异常通知.throws advice 5.5 所有功能总称叫做切面. 5.6 织入: 把切面嵌入到原有功能的过程叫做织入 5.6.1 spring 提供了 2 种AOP 实现方式 5.6.2 Schema-based 5.6.3 每个通知都需要实现接口或类 5.6.4 配置spring 配置文件时在<aop:config>配置 5.6.5 AspectJ 5.6.6 每个通知不需要实现接口或类 5.6.7 配置spring 配置文件是在<aop:config>的子标签 ,<aop:aspect>中配置

.Schema-based 实现步骤

1. 导入jar

2. 新建通知类

  1.1 新建前置通知类

  1.1.1 arg0: 切点方法对象Method 对象

  1.1.2 arg1: 切点方法参数

  1.1.3 arg2:切点在哪个对象中

import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class MyBeforeAdvice implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] objects, Object o) throws Throwable {
System.out.println("+++++输出执行前置通知");
}
}

    2 新建后置通知类

  2.1.1 arg0: 切点方法返回值

  2.1.2 arg1:切点方法对象

  2.1.3 arg2:切点方法参数

  2.1.4 arg3:切点方法所在类的对象

public class MyAfterAdvice implements
AfterReturningAdvice {
@Override
public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
System.out.println("执行后置通知");
}
}

3.配置spring 配置文件

  1.1 引入aop 命名空间

  1.2 配置通知类的<bean>

  1.3 配置切面

  1.4 * 通配符,匹配任意方法名,任意类名,任意一级包名

  1.5  如果希望匹配任意方法参数 (..)

<?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/sc hema/beans
http://www.springframework.org/schema/beans/spring-be ans.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop. xsd">
<!-- 配置通知类对象,在切面中引入 -->
<bean id="mybefore" class="com.bjsxt.advice.MyBeforeAdvice"></bean>
<bean id="myafter" class="com.bjsxt.advice.MyAfterAdvice"></bean> <!-- 配置切面 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(* com.bjsxt.test.Demo.demo2())" id="mypoint"/>
<!-- 通知 -->
<aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/>
<aop:advisor advice-ref="myafter" pointcut-ref="mypoint"/>
</aop:config>
<!-- 配置Demo 类,测试使用 -->
<bean id="demo" class="com.bjsxt.test.Demo"></bean>
</beans>

4.编写测试代码

public class Test {
public static void main(String[] args) {
demo.demo3();
ApplicationContext ac = new
ClassPathXmlApplicationContext("applicationContext.xm
l");
Demo demo = ac.getBean("demo",Demo.class);
demo.demo1();
demo.demo2();
demo.demo3();
}
}

运行结果:

Spring-05 -AOP [面向切面编程] -Schema-based 实现aop的步骤的更多相关文章

  1. javascript AOP(面向切面编程)

    var func = function () { console.log("2") } Function.prototype.before = function (beforefn ...

  2. Spring框架系列(4) - 深入浅出Spring核心之面向切面编程(AOP)

    在Spring基础 - Spring简单例子引入Spring的核心中向你展示了AOP的基础含义,同时以此发散了一些AOP相关知识点; 本节将在此基础上进一步解读AOP的含义以及AOP的使用方式.@pd ...

  3. Spring:AOP面向切面编程

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

  4. spring:AOP面向切面编程02

    参考: https://blog.csdn.net/jeffleo/article/details/54136904 一.AOP的核心概念AOP(Aspect Oriented Programming ...

  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. 谈一谈AOP面向切面编程

    AOP是什么 : AOP面向切面编程他是一种编程思想,是指在程序运行期间,将某段代码动态的切入到指定方法的指定位置,将这种编程方式称为面向切面编程 AOP使用场景 : 日志 事务 使用AOP的好处是: ...

  10. AOP面向切面编程的四种实现

     一.AOP(面向切面编程)的四种实现分别为最原始的经典AOP.代理工厂bean(ProxyFacteryBean)和默认自动代理DefaultAdvisorAutoProxyCreator以及Bea ...

随机推荐

  1. 源码分析之Handler

    Handler是Android中的消息机制实现,可以实现UI线程和子线程的消息传递,这里就来深入了解Android的消息机制,来分析Handler的源代码 入手实例 在Android开发中,子线程与主 ...

  2. Vue利用搜狐获取公网ip地址

    在index.html中添加代码: <script src="https://pv.sohu.com/cityjson?ie=utf-8"></script> ...

  3. ubuntu12下安装unixODBC(mysql)

    转自:https://blog.51cto.com/dreamylights/1321678 1. 需要的包 unixODBC源码包unixODBC-2.2.14.tar.gz mysql 驱动 my ...

  4. 【剑指offer】面试题 22. 链表中倒数第 K 个节点

    面试题 22. 链表中倒数第 K 个节点

  5. hdfs、tfs、fastdfs、Tachyon

    hdfs 架构设计 HDFS按照Master和Slave的结构.分NameNode.SecondaryNameNode.DataNode这几个角色. NameNode:是Master节点,是管理者.. ...

  6. C++ 把数组数据存入 CSV 文件,以及读取 CSV 文件的数据

    1. CSV-百度百科 2. 代码 #pragma once //Microsoft Visual Studio 2015 Enterprise #include<iostream> #i ...

  7. WAMP集成环境虚拟路径修改

    只需要改httpd.conf这一个文件就好了. 1.单击右下角wamp图标如下图打开httpd.conf,或者从文件夹打开httpd.conf.

  8. CI中如何配置BootStrap

  9. MGR并行复制从节点复制线程死锁

    1.故障现象 20191113-22:32 datax全量同步t_shop_info表到 eorder所在的实例,t_shop_info表有两个唯一约束.总数据量不超过1w行,同步完成后MGR从库复制 ...

  10. 解决pyspider框架web预览框过小问题

    解决pyspider框架web预览框过小问题 Chrome 使用pyspider框架时,web预览框只有一小条: 解决办法: 找到debug.min.css文件,替换为如下内容: body{margi ...