*参考优质文档: https://www.cnblogs.com/xrq730/p/4919025.html

一.简介

  aop(Aspect Oriented Programming)是面向切面编程,即将一个方法插入到n个对象方法的指定位置

  aop分为五种织入通知方式:前置,后置,环绕,异常,引入(只需要修改配置文件)

  前置:在方法前执行

  后置:在方法后执行

  环绕:进入方法后,可以在所有语句前执行或者在所有语句后执行

  异常:如果方法内有语句错误,则进入异常处理方法

  

  引入:只修改配置文件,操作前几种织入方法,用于配置某个对象织入什么方法

  实现对应接口的类即可作为通知类

  

二.AOP核心概念

  1、横切关注点

    对哪些方法进行拦截,拦截后怎么处理,这些关注点称之为横切关注点

  2、切面(aspect)

    类是对物体特征的抽象,切面就是对横切关注点的抽象

  3、连接点(joinpoint)

    被拦截到的点,因为Spring只支持方法类型的连接点,所以在Spring中连接点指的就是被拦截到的方法,实际上连接点还可以是字段或者构造器

  4、切入点(pointcut)

    对连接点进行拦截的定义

  5、通知(advice)

    所谓通知指的就是指拦截到连接点之后要执行的代码,通知分为前置、后置、异常、最终、环绕通知五类

  6、目标对象

    代理的目标对象

  7、织入(weave)

    将切面应用到目标对象并导致代理对象创建的过程

  8、引入(introduction)

    在不修改代码的前提下,引入可以在运行期为类动态地添加一些方法或字段

三. 织入通知方法实现步骤

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

    4.1配置被代理对象=目标对象

    4.2配置通知

    4.3配置代理对象,是ProxyFactoryBean对象实例

      4.3.1配置代理接口集

      4.3.2织入通知

      4.3.3配置被代理对象

四.案例(前置)

  1.定义接口TestService

package com.ahd.aop;

public interface TestService {
public void sayHello();
}

  2.实现类Test1Service 

  

package com.ahd.aop;

public class Test1Service implements TestService {
private String name;
@Override
public void sayHello() {
// TODO Auto-generated method stub
System.out.println("******执行方法sayHello test1"+name+"******"); }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}

Test1Service

  3.前置织入类

package com.ahd.aop;

import java.lang.reflect.Method;

import org.springframework.aop.MethodBeforeAdvice;

public class MyMethodBeforeAdvice implements MethodBeforeAdvice {

    @Override
public void before(Method method, Object[] args, Object target)
throws Throwable {
// TODO Auto-generated method stub
System.out.println("前置通知,在方法执行前执行,写入日志,"+method.getName());
}
}

MyMethodBeforeAdvice

  4.beans.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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置被代理对象,即正常配置类 -->
<bean id="test1Service" class="com.ahd.aop.Test1Service">
<property name="name" value="爱华顿"></property>
</bean>
<!-- 配置前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.ahd.aop.MyMethodBeforeAdvice">
</bean>
<!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 配置代理接口集 -->
<property name="proxyInterfaces">
<list>
<value>com.ahd.aop.TestService</value>
</list>
</property>
<!-- 织入通知 -->
<property name="interceptorNames">
<list>
<value>myMethodBeforeAdvice</value>
</list>
</property>
<!-- 配置被代理对象,可以指定 -->
<property name="target" ref="test1Service"/>
</bean> </beans>

  5.测试文件

package com.ahd.aop;

import org.springframework.aop.framework.ProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class App1 { public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext ac=new ClassPathXmlApplicationContext("com/ahd/aop/beans.xml");
TestService ts=(TestService) ac.getBean("proxyFactoryBean");
ts.sayHello(); System.out.println("***********************************");
((TestService2)ts).sayBye();
} }

测试文件

五.引入通知案例(在上面案例的基础上,修改配置文件)

<?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:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 配置被代理对象,即正常配置类 -->
<bean id="test1Service" class="com.ahd.aop.Test1Service">
<property name="name" value="爱华顿"></property>
</bean>
<!-- 配置前置通知 -->
<bean id="myMethodBeforeAdvice" class="com.ahd.aop.MyMethodBeforeAdvice">
<!-- 配置后置通知 -->
<bean id="myBeforeReturningAdvice" class="com.ahd.aop.MyBeforeReturningAdvice"></bean> <!-- 配置环绕通知 -->
<bean id="myMethodInterceptor" class="com.ahd.aop.MyMethodInterceptor"></bean> <!-- 配置异常通知 -->
<bean id="myThrowsAdvice" class="com.ahd.aop.MyThrowsAdvice"></bean>
</bean>
<!-- 配置引入,配置接入点 -->
<bean id="myMethodBeforeAdviceFilter" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice" ref="myMethodBeforeAdvice"></property>
<property name="mappedNames">
<list>
<value>sayHello</value>
</list>
</property>
</bean> <!-- 配置代理对象 -->
<bean id="proxyFactoryBean" class="org.springframework.aop.framework.ProxyFactoryBean"> <!-- 配置代理接口集 -->
<property name="proxyInterfaces">
<list>
<value>com.ahd.aop.TestService</value>
<value>com.ahd.aop.TestService2</value>
</list>
</property>
<!-- 织入通知 -->
<property name="interceptorNames">
<list>
<value>myMethodBeforeAdviceFilter</value>
<value>myBeforeReturningAdvice</value>
<value>myMethodInterceptor</value>
<value>myThrowsAdvice</value>
</list>
</property>
<!-- 配置被代理对象,可以指定 -->
<property name="target" ref="test1Service"/>
</bean> </beans>

实现效果:

======================================================================================

我们都一样,我们都不一样!

=======================================================================================

spring_08aop原理及案例的更多相关文章

  1. 【MySQL】排序原理与案例分析

    前言 排序是数据库中的一个基本功能,MySQL也不例外.用户通过Order by语句即能达到将指定的结果集排序的目的,其实不仅仅是Order by语句,Group by语句,Distinct语句都会隐 ...

  2. Office 2010 KMS激活原理和案例分享

    Office 2010 KMS激活原理和案例分享     为了减低部署盗版(可能包含恶意软件.病毒和其他安全风险)的可能性,Office 2010面向企业客户推出了新的批量激活方式:KMS和MAK.这 ...

  3. 《大型网站技术架构:核心原理与案例分析》【PDF】下载

    <大型网站技术架构:核心原理与案例分析>[PDF]下载链接: https://u253469.pipipan.com/fs/253469-230062557 内容简介 本书通过梳理大型网站 ...

  4. 11 K-Means 原理及案例

    11 K-Means 原理及案例 非监督学习 unsupervised learning (非监督学习) ,只有特征值,没有目标值 聚类: 主要方法 - k-means (K - 需要分成的类别数) ...

  5. MySQL排序原理与案例分析

    前言      排序是数据库中的一个基本功能,MySQL也不例外.用户通过Order by语句即能达到将指定的结果集排序的目的,其实不仅仅是Order by语句,Group by语句,Distinct ...

  6. [转]MySQL排序原理与案例分析

    这篇文章非常好,就把他转过来 前言      排序是数据库中的一个基本功能,MySQL也不例外.用户通过Order by语句即能达到将指定的结果集排序的目的,其实不仅仅是Order by语句,Grou ...

  7. Office 2010 KMS激活原理和案例分享 - Your Office Solution Here - Site Home - TechNet Blogs

    [作者:葛伟华.张玉工程师 ,  Office/Project支持团队, 微软亚太区全球技术支持中心 ] 为了减低部署盗版(可能包含恶意软件.病毒和其他安全风险)的可能性,Office 2010面向企 ...

  8. (转)MySQL排序原理与案例分析

    前言      排序是数据库中的一个基本功能,MySQL也不例外.用户通过Order by语句即能达到将指定的结果集排序的目的,其实不仅仅是Order by语句,Group by语句,Distinct ...

  9. Spring_day01--课程安排_Spring概念_IOC操作&IOC底层原理&入门案例_配置文件没有提示问题

    Spring_day01 Spring课程安排 今天内容介绍 Spring概念 Spring的ioc操作 IOC底层原理 IOC入门案例 配置文件没有提示问题 Spring的bean管理(xml方式) ...

随机推荐

  1. 安装easygui

    1.下载0.96的easygui 官网: http://easygui.sourceforge.net/ 2.解压后得到文件夹,里面有两个文件分别为,setup.py和easygui.py 3.在py ...

  2. day18_雷神_django第一天

    # django_day01 1.http 协议 超文本传输协议,HTTP有很多应用,但最著名的是用于web浏览器和web服务器之间的双工通信. 协议概述 HTTP是一个客户端终端(用户)和服务器端( ...

  3. Python小练习之判断一个日期是一年的第几天

    python练手遇到的一个问题写了个统一公式,不用麻烦的分各种类,如果有人测试出错误请评论通知. #分单双月 def dayNum(month,day,isLeap): if month % 2 != ...

  4. 包建强的培训课程(4):App测试深入学习和研究

    @import url(http://i.cnblogs.com/Load.ashx?type=style&file=SyntaxHighlighter.css);@import url(/c ...

  5. vue模式

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  6. 小程序页面跳转传参-this和that的区别-登录流程-下拉菜单-实现画布自适应各种手机尺寸

    小程序页面跳转传参 根目录下的 app.json 文件 页面文件的路径.窗口表现.设置网络超时时间.设置多 tab { "pages": [ "pages/index/i ...

  7. 一个需求认识CSS3 的transform-origin属性

    最近遇到一个需求,是以前做PHP的同事问我的问题    下面是他在百度发的问题截图 根据上面的截图,我稍微梳理了一下 问题:现在有个div,旋转45度后,这个div的宽度会动态改变,并且要向右上方偏移 ...

  8. 原生Ajax GET+POST请求无刷新实现文本框用户名是否被注册

    实现Ajax需要使用一个核心对象XMLHttpRequest XMLHttpRequest对象可以在不向服务器提交整个页面的情况下,实现局部更新网页.当页面全部加载完毕后,客户端通过该对象向服务器请求 ...

  9. [部署]CentOS安装MariaDB

    环境 虚拟机:VMWare10.0.1 build-1379776 操作系统:CentOS7 64位 步骤 1.添加MariaDB的yum仓库源,在/etc/yum.repos.d/ 下建立 Mari ...

  10. 性能瓶颈之Mapping

    如果Source和Target都不存在性能上的瓶颈,则问题可能会出在Mapping 如何判定Mapping存在性能瓶颈 1)  在session log中读取thread statistics和wor ...