Spring

1、AOP:中文名称面向切面编程

2、英文名称:(Aspect Oriented Programming)

3、正常程序执行流程都是纵向执行流程

  3.1 又叫面向切面编程,在原有纵向执行流程中添加横切面

  3.2 不需要修改原有程序代码

    3.2.1 高扩展性

    3.2.2 原有功能相当于释放了部分逻辑,让职责更加明确

4、面向切面编程是什么??

  4.1 在程序原有纵向流程中,针对某一个或某一些方法添加 通知,形成横切面过程,就叫做面向切面编程。

5、常用概念

  5.1 原有功能:切点,pointcut

  5.2 前置通知:在切点之前执行的功能 before advice

  5.3 后置通知:在切点之后执行的功能 after advice

  5.4 如果切点执行过程中出现异常,会触发异常通知 throws advice

  5.5 所有功能总称叫做切面

  5.6 织入:把切面嵌入到原有功能的过程叫做织入

6、spring提供了2种AOP实现方式

  6.1 Schema-based :

    6.1 每个通知都需要实现接口或类

    6.2 配置spring配置文件时 在<aop:config>配置

  6.2 Aspectj

    6.2.1 每个通知不需要实现接口或类

    6.2.2 配置spring配置文件是在<aop:config>的子标签

    <aop:aspect>中配置

Schema-based实现步骤:

  1、导入包

  2、新建通知类

    2.1 新建前置通知类

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

      2.1.2 arg1:切点方法参数

      2.1.3 arg2:切点在哪个对象中

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

    2.2 新建后置通知类

      2.2.1 arg0:切点方法返回值

      2.2.2 arg1:切点方法对象

      2.2.3 arg2:切点方法参数

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

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

  3、配置spring配置文件

    3.1 引入aop命名空间

    3.2 配置通知类的<bean>

    3.3 配置切面

<?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.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.Test.demo2())" id="mypoint"/>      * 通配符,匹配任意方法名,任意类名,任意一级包名
<!-- 通知 -->
<aop:advisor advice-ref="mybefore" pointcut-ref="mypoint"/>
<aop:advisor advice-ref="myafter" pointcut-ref="mypoint" />
</aop:config>
<!-- 配置Demo类,测试使用 -->
<bean id="test" class="com.bjsxt.test.Test"></bean>
</beans>

  4、编写测试代码

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

  5、运行结果

 

AOP 和 前置通知,后置通知的更多相关文章

  1. PHP通过__call实现简单的AOP(主事务后的其他操作)比如前置通知,后置通知

    /** * person class */ class Person { /** * person class -> function say */ public static function ...

  2. spring学习 十 schema-based 前置后后置通知

    spring 提供了 2 种 AOP 实现方式:(1)Schema-based ,(2)AspectJ Schema-based:每个通知都需要实现接口或类,配置 spring 配置文件时在<a ...

  3. spring的AspectJ基于XML和注解(前置、后置、环绕、抛出异常、最终通知)

    1.概念 (1)AspectJ是一个基于Java语言的AOP框架 (2)Spring2.0以后新增了对AspectJ切入点表达式的支持 (3)AspectJ是AspectJ1.5的新增功能,通过JDK ...

  4. [原创]java WEB学习笔记106:Spring学习---AOP的通知 :前置通知,后置通知,返回通知,异常通知,环绕通知

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  5. Spring AOP前置通知和后置通知

    Spring AOP AspectJ:Java社区里最完整最流行的AOP框架 在Spring2.0以上的版本中,可以使用基于AspectJ注解或基于XML配置的AOP 在Spring中启用Aspect ...

  6. [转载] Spring框架——AOP前置、后置、环绕、异常通知

    通知类型: 步骤: 1. 定义接口 2. 编写对象(被代理对象=目标对象) 3. 编写通知(前置通知目标方法调用前调用) 4. 在beans.xml文件配置 4.1 配置 被代理对象=目标对象 4.2 ...

  7. Spring(十八):Spring AOP(二):通知(前置、后置、返回、异常、环绕)

    AspectJ支持5种类型的通知注解: @Before:前置通知,在方法执行之前执行: @After:后置通知,在方法执行之后执行: @AfterRunning:返回通知,在方法返回结果之后执行(因此 ...

  8. Spring初学之xml实现AOP前置通知、后置通知、返回通知、异常通知等

    实现两个整数的加减乘除,在每个方法执行前后打印日志. ArithmeticCalculator.java: package spring.aop.impl.xml; public interface ...

  9. Spring初学之annotation实现AOP前置通知、后置通知、返回通知、异常通知。

    实现两个整数的加减乘除.在执行每个方法之前打印日志. ArithmeticCalculator.java: package spring.aop.impl; public interface Arit ...

随机推荐

  1. Centos7 下的NTP-server(Chorny) 部署及客户端时间同步配置

    一.介绍 1.本博客以 ceph 集群搭建时的NTP-server 为例. 2.hosts # vim /etc/hosts 10.6.32.20    ceph1     (作为时间服务器) 10. ...

  2. 抛弃WebService,在.NET4中用 jQuery 调用 WCF

    在我们之前的开发中,对于ajax程序,都是通过jQuery调用标记为[System.Web.Script.Services.ScriptService]的WebService,然后在WebServic ...

  3. [codeforces_597B] Restaurant(贪心)

    题目链接 http://codeforces.com/problemset/problem/597/B 题意 输入:区间数目n.及n个区间的起止(左闭右闭). 输出:最多不重叠的区间有多少个. 思路 ...

  4. C++ 中的RTTI机制详解

    前言 RTTI是”Runtime Type Information”的缩写,意思是运行时类型信息,它提供了运行时确定对象类型的方法.RTTI并不是什么新的东西,很早就有了这个技术,但是,在实际应用中使 ...

  5. SSH异常“Failed to start OpenSSH Server daemon”

    [root@bogon yum]# systemctl status sshd.service● sshd.service - OpenSSH server daemon   Loaded: load ...

  6. DevExpress XPO 开发指南 简要

    最近在看devexpress   安装程序中的代码Demos ..  C:\Users\Public\Documents\DevExpress Demos 16.1\Components\WinFor ...

  7. Spring 监听

    Spring 中的事件监听的实现 这里我们不讨论事件监听的机制的原理,我们只讨论如何在项目中实现时间监听. spring的事件监听是基于观察者模式.设计开发中.如下类与接口是我们必须要使用的. App ...

  8. 使用PreparedStatement时,输出完整的SQL语句

    使用psstmt时不能打印出完整的sql语句,挺不方便的,找到一个实现方法,记录下来. package com.zhh.function.util; import java.io.InputStrea ...

  9. hdu 1429 (bfs+状态压缩) 胜利大逃亡续

    http://acm.hdu.edu.cn/showproblem.php?pid=1429 典型的状压搜索,在普通的搜索基础上,利用二进制的特性记录钥匙与门, 二进制的每一位代表一把钥匙,比如说拿到 ...

  10. Tools.Eclipse.HowToImportAnAndroidLibraryProjectIntoWorkspace

    1. File->New->Other Picture-1 2. Select "Android Project from Existing Code", and cl ...