spring 提供了 2 种 AOP 实现方式:(1)Schema-based ,(2)AspectJ

Schema-based:每个通知都需要实现接口或类,配置 spring 配置文件时在<aop:config>配置


AspectJ每个通知不需要实现接口或类,配置 spring 配置文件是在<aop:config>的子标签<aop:aspect>中配置

基于Schema-based实现的入门程序

(1)第一步:导入jar包,除了spring中必须的包,下面两个包

(2)第二步:新建通知类

如果要实现前置通知,后置通知,在Schema-based方式中需要实现  MethodBeforeAdvice与  AfterReturningAdvice两个接口,代码如下;

前置通知代码,before方法的参数:

arg0: 切点方法对象 Method 对象
arg1: 切点方法参数
arg2:切点在哪个对象中

import java.lang.reflect.Method;

import org.springframework.aop.BeforeAdvice;
import org.springframework.aop.MethodBeforeAdvice; public class MyBeforeAdvice implements MethodBeforeAdvice{ @Override
public void before(Method arg0, Object[] arg1, Object arg2) throws Throwable {
System.out.println("前置通知"); } }

后置通知代码:

afterReturning方法的参数:

arg0: 切点方法返回值
arg1:切点方法对象
arg2:切点方法参数
arg3:切点方法所在类的对象

package com.airplan.pojo;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

public class MyAfterAdvice implements AfterReturningAdvice{

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

第三步:aop配置

3.1 引入 aop 命名空间
3.2 配置通知类的<bean>
3.3 配置切面
3.4  * 通配符,匹配任意方法名,任意类名,任意一级包名
3.5   如果希望匹配任意方法参数 (..)

代表把返回值为任意类型,
且在PointCutClass类下面的,参数为任意的方法声明为切点
expression="* execution(void com.airplan.pojo.PointCutClass.*(..))"

下面的配置代表把通知和切入点进行对应
<aop:advisor advice-ref="beforeAdvice" pointcut-ref="mypoint"/>
<aop:advisor advice-ref="afterAdvice" pointcut-ref="mypoint"/>


<?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.1.xsd "> <!-- 配置通知所在的类 -->
<bean id="beforeAdvice" class="com.airplan.pojo.MyBeforeAdvice"></bean>
<bean id="afterAdvice" class="com.airplan.pojo.MyAfterAdvice"></bean> <!-- 配置切面 -->
<aop:config>
<!-- 配置切点 -->
<aop:pointcut expression="execution(void com.airplan.pojo.PointCutClass.testFun())" id="mypoint"/>
<!-- 配置通知 -->
<aop:advisor advice-ref="beforeAdvice" pointcut-ref="mypoint"/>
<aop:advisor advice-ref="afterAdvice" pointcut-ref="mypoint"/> </aop:config>
<!-- 配置切点所在的类 -->
<bean id="pointCutClass" class="com.airplan.pojo.PointCutClass"></bean> </beans>

第四步:测试代码

package com.airplan.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.airplan.pojo.PointCutClass; public class AopDemo {
public static void main(String[] args) {
ApplicationContext ac=new ClassPathXmlApplicationContext("applicationContext.xml");
PointCutClass pointCutClass = ac.getBean("pointCutClass",PointCutClass.class);
pointCutClass.testFun();
}
}

运行结果;

spring学习 十 schema-based 前置后后置通知的更多相关文章

  1. Spring学习十五----------Spring AOP API的Pointcut、advice及 ProxyFactoryBean相关内容

    © 版权声明:本文为博主原创文章,转载请注明出处 实例: 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4. ...

  2. spring学习十九 常用注解

    1. @Component 创建类对象,相当于配置<bean/>2. @Service 与@Component 功能相同. 2.1 写在 ServiceImpl 类上.3. @Reposi ...

  3. spring学习 十二 AspectJ-based的通知入门 带参数的通知

    第一步:编写通知类 package com.airplan.pojo; import org.aspectj.lang.ProceedingJoinPoint; public class Advice ...

  4. Spring学习十四----------Spring AOP实例

    © 版权声明:本文为博主原创文章,转载请注明出处 实例 1.项目结构 2.pom.xml <project xmlns="http://maven.apache.org/POM/4.0 ...

  5. spring学习 十 schema-based 异常通知,和环绕通知

    一 schema-based异常通知 第一步:创建通知类 :新建一个类实现 throwsAdvice 接口,throwsAdvice接口只是标记接口里面并没有任何方法,必须自己写方法,且必须叫 aft ...

  6. Spring学习(十八)----- Spring AOP+AspectJ注解实例

    我们将向你展示如何将AspectJ注解集成到Spring AOP框架.在这个Spring AOP+ AspectJ 示例中,让您轻松实现拦截方法. 常见AspectJ的注解: @Before – 方法 ...

  7. Spring学习(十六)----- Spring AOP实例(Pointcut(切点),Advisor)

    在上一个Spring AOP通知的例子,一个类的整个方法被自动拦截.但在大多数情况下,可能只需要一种方式来拦截一个或两个方法,这就是为什么引入'切入点'的原因.它允许你通过它的方法名来拦截方法.另外, ...

  8. Spring学习(十五)----- Spring AOP通知实例 – Advice

    Spring AOP(面向方面编程)框架,用于在模块化方面的横切关注点.简单得说,它只是一个拦截器拦截一些过程,例如,当一个方法执行,Spring AOP 可以劫持一个执行的方法,在方法执行之前或之后 ...

  9. Spring学习(十二)-----Spring @PostConstruct和@PreDestroy实例

    实现 初始化方法和销毁方法3种方式: 实现标识接口 InitializingBean,DisposableBean(不推荐使用,耦合性太高) 设置bean属性 Init-method destroy- ...

随机推荐

  1. 【Scheme】符号求导

    思路: 定义一个求导算法, 令其在抽象对象上执行求导操作. 可以由以下规约规则完成: dc/dx=0 dx/dx=1 d(u+v)/dx=du/dx+dv/dx d(uv)/dx=u(dv/dx)+v ...

  2. poj1308(并查集)

    题目链接:http://poj.org/problem;jsessionid=436A34AE4BE856FB2DF9B264DCA9AA4E?id=1308 题意:给定一些边让你判断是否构成数. 思 ...

  3. HDU 6081 度度熊的王国战略(全局最小割堆优化)

    Problem Description度度熊国王率领着喵哈哈族的勇士,准备进攻哗啦啦族.哗啦啦族是一个强悍的民族,里面有充满智慧的谋士,拥有无穷力量的战士.所以这一场战争,将会十分艰难.为了更好的进攻 ...

  4. EOJ Monthly 2018.7 B.锐角三角形(数学几何+思维)

    描述 是否存在面积为S/2的整点锐角三角形?存在输出Yes并输出三个整点坐标,否则输出No. 注意如果存在输出的坐标必须在long long范围内. Input 第一行一个整数S(1<=S< ...

  5. Jedis cluster命令执行流程剖析

    Jedis cluster命令执行流程剖析 在Redis Cluster集群模式下,由于key分布在各个节点上,会造成无法直接实现mget.sInter等功能.因此,无论我们使用什么客户端来操作Red ...

  6. 865. Smallest Subtree with all the Deepest Nodes 有最深节点的最小子树

    [抄题]: Given a binary tree rooted at root, the depth of each node is the shortest distance to the roo ...

  7. day15 json,os,sys,hashlib

     序列化模块 import json # json 序列化模块 是所有语言通用的一种标准(数据转化格式). # str int bool dict list(tuple) None import pi ...

  8. RxJava笔记

    网上搜索了一些关于 RxJava 的东西,对RxJava的定义自己理解如下: RxJava是要一种逻辑简洁的,通过一种扩展的观察者模式,来实现异步的一种链式编程.

  9. C语言常用标准库函数

    数学函数: 在math.h中 abs(x) :求整型数x的绝对值 cos(x):x(弧度)的余弦 fabs(x):求浮点数x的绝对值 ceil(x):求不小于x的最小整数 floor(x):求不大于x ...

  10. IntelliJ IDEA return null with ClassLoader.getSystemResourceAsStream(“configFilename”));

    参考https://stackoverflow.com/questions/49470053/intellij-idea-return-null-with-classloader-getsystemr ...