Spring-AOP(Aspect-orented programming)
在业务流程中插入与业务无关的逻辑,这样的逻辑称为Cross-cutting concerns,将Crossing-cutting concerns独立出来为一个对象,这样的特殊对象称为Aspect
Aspect,即方面。所谓方面,就是将那些与业务无关,却为业务模块所共同调用的逻辑或责任封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可操作性和可维护性。
AOP代表的是一个横向的关系,如果说“对象”是一个空心的圆柱体,其中封装的是对象的属性和行为;那么面向方面编程的方法,就仿佛一把利刃,将这些空心圆柱体剖开,以获得其内部的消息。而剖开的切面,也就是所谓的“方面”了。然后它又以巧夺天功的妙手将这些剖开的切面复原,不留痕迹。

  方面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象。事务管理是J2EE应用中一个很好的横切关注点例子。方面用Spring的 Advisor或拦截器实现。

  通知(Advice): 在特定的连接点,AOP框架执行的动作。各种类型的通知包括“around”、“before”和“throws”通知。通知类型将在下面讨论。许多AOP框架包括Spring都是以拦截器做通知模型,维护一个“围绕”连接点的拦截器链。Spring中定义了四个advice: BeforeAdvice, AfterAdvice, ThrowAdvice和DynamicIntroductionAdvice

  切入点(Pointcut):指定一个通知将被引发的一系列连接点的集合。AOP框架必须允许开发者指定切入点:例如,使用正则表达式。Spring定义了Pointcut接口,用来组合MethodMatcher和ClassFilter,可以通过名字很清楚的理解, MethodMatcher是用来检查目标类的方法是否可以被应用此通知,而ClassFilter是用来检查Pointcut是否应该应用到目标类上

实现方式:


  1.使用Spring API实现
    通知类型:前置通知、异常通知、后置通知、环绕通知、最终通知
    后置通知:

 1 public class LogAfter implements AfterReturningAdvice{
 2                     /**
 3                      * 目标方法执行后执行的通知
 4                      * @param returnValue——返回值
 5                      * @param method 被调用方法对象
 6                      * @param args 被调用的方法的参数
 7                      * @param target 被调用的方法对象的目标对象
 8                      */
 9                     @Override
10                     public void afterReturning(Object returnValue, Method method, Object[] args,
11                             Object target) throws Throwable {
12                         System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行——后");
13                     }
14                 }

LogAfter.java

    前置通知:

 1                 public class LogBefore implements MethodBeforeAdvice{
 2                     /**
 3                      * @param method 被调用方法对象
 4                      * @param args 被调用的方法的参数
 5                      * @param target 目标对象
 6                      */
 7                     @Override
 8                     public void before(Method method, Object[] args, Object target)
 9                             throws Throwable {
10                         System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行——前");
11                     }
12                 }

LogBefore

 1 <beans
 2                     xmlns="http://www.springframework.org/schema/beans"
 3                     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4                     xmlns:aop="http://www.springframework.org/schema/aop"
 5                     xsi:schemaLocation=
 6                         "http://www.springframework.org/schema/beans
 7                         http://www.springframework.org/schema/beans/spring-beans.xsd
 8                         http://www.springframework.org/schema/aop
 9                         http://www.springframework.org/schema/aop/spring-aop.xsd">
10                     <!-- 声明一个切面 -->
11                     <bean id="logBefore" class="com.zhengbin.log.LogBefore" />
12                     <bean id="logAfter" class="com.zhengbin.log.LogAfter" />
13
14                     <bean id="userService" class="com.zhengbin.service.UserServiceImpl"/>
15                     <aop:config>
16                         <!-- pointcut切入点 -->
17                         <!-- execution(*[表示所有返回值] com.zhengbin.service[位置](..[表示所有参数])) -->
18                         <aop:pointcut id="pointcut" expression="execution(* com.zhengbin.service.*.*(..))" />
19                         <!--  -->
20                         <aop:advisor advice-ref="logAfter" pointcut-ref="pointcut"/>
21                         <aop:advisor advice-ref="logBefore" pointcut-ref="pointcut"/>
22                     </aop:config>
23                 </beans>

beans.xml

  2.自定义类实现

1 public class Log {
2     public void before(){
3         System.out.println("执行前");
4     }
5     public void after(){
6         System.out.println("执行后");
7     }
8 }

Log.java

 1 <beans
 2     xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xsi:schemaLocation=
 6         "http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/aop
 9         http://www.springframework.org/schema/aop/spring-aop.xsd">
10     <!-- 声明一个切面 -->
11     <bean id="log" class="com.zhengbin.log.Log"/>
12     <bean id="userService" class="com.zhengbin.service.UserServiceImpl"/>
13     <aop:config>
14         <aop:aspect ref="log">
15             <aop:pointcut expression="execution(* com.zhengbin.service.*.*(..))" id="pointCut"/>
16             <aop:before method="before" pointcut-ref="pointCut"/>
17             <aop:after method="after" pointcut-ref="pointCut"/>
18         </aop:aspect>
19     </aop:config>
20 </beans>

beans.xml

  3.通过注解实现

    启用Spring对@AspectJ的支持:
    <aop:aspectj-autoproxy/>

 1 @Aspect
 2 public class Log {
 3     @Before("execution(* com.zhengbin.service.*.*(..))")
 4     public void before(){
 5         System.out.println("执行前");
 6     }
 7     @After("execution(* com.zhengbin.service.*.*(..))")
 8     public void after(){
 9         System.out.println("执行后");
10     }
11 }

Log.java

 1 <beans
 2     xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:aop="http://www.springframework.org/schema/aop"
 5     xsi:schemaLocation=
 6         "http://www.springframework.org/schema/beans
 7         http://www.springframework.org/schema/beans/spring-beans.xsd
 8         http://www.springframework.org/schema/aop
 9         http://www.springframework.org/schema/aop/spring-aop.xsd">
10     <!-- 声明一个切面 -->
11     <bean id="log" class="com.zhengbin.log.Log"/>
12
13     <bean id="userService" class="com.zhengbin.service.UserServiceImpl"/>
14
15     <!-- 启用Spring对@AspectJ的支持 -->
16     <aop:aspectj-autoproxy/>
17 </beans>

beans.xml

Spring学习之AOP相关推荐

  1. Spring 学习二-----AOP的原理与简单实践

    一.Spring  AOP的原理 AOP全名Aspect-Oriented Programming,中文直译为面向切面(方面)编程.何为切面,就比如说我们系统中的权限管理,日志,事务等我们都可以将其看 ...

  2. Spring学习之AOP(面向切面编程)

    动态代理 代理模式是常用的java设计模式,他的特征是代理类与委托类有同样的接口,代理类主要负责为委托类预处理消息.过滤消息.把消息转发给委托类,以及事后处理消息等.代理类与委托类之间通常会存在关联关 ...

  3. 【Spring学习】AOP实现日志记录

    AOP知识点 AOP,面向切面编程.通过预编译方式和运行时动态代理实现在不修改源代码的情况下给程序动态统一添加功能的一种技术. AOP编程思想就是把很多类对象中的横切问题点,从业务逻辑中分离出来,减少 ...

  4. Spring 学习 day3 : AOP,Spring中JdbcTemplate的使用

    1.AOP 1.1 什么是AOP 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方 式和运行期动态代理实现程序功能的统一维护的一种技术. ...

  5. Spring学习之AOP的切点表达式详解

    1.背景知识 在Sping中使用AOP的主要工作是配置织入关系,使用<aop:config>标签代表这一段是织入关系的配置 首先,用<aop:aspect>标签声明一个切面,这 ...

  6. Spring学习,AOP术语

    连接点(Joinpoint),切点(Poincut),增强(Advice),目标对象(Tatget),引介(Introduction),织入(Weaving),代理(Proxy),切面(Aspect)

  7. Spring 学习笔记----->AOP

    Spring 学习笔记----->AOP 代理模式 为什么学代理模式? 因为这就是Spring Aop的底层 代理模式的分类: 静态代理 动态代理 静态代理 生活用的例子: 房东 public ...

  8. 动态代理——》AOP —— Spring 中的 AOP||AOP 相关术语||学习 spring 中的 AOP 要明确的事

    AOP 概述 什么是 AOP       AOP:全称是 Aspect Oriented Programming 即:面向切面编程 AOP 的作用及优势 作用: 在程序运行期间,不修改源码对已有方法进 ...

  9. Spring学习总结——Spring实现AOP的多种方式

    目录 一.基于XML配置的Spring AOP 二.使用注解配置AOP 三.AspectJ切点函数 四.AspectJ通知注解 五.零配置实现Spring IoC与AOP 六.示例下载 AOP(Asp ...

最新文章

  1. 第一个简单的DEMO
  2. 【转】SAP LIST 画框的FORM
  3. Python数据类型判断常遇到的坑
  4. 技术管理规划-如何规划团队的架构
  5. 真正的mybatis_redis二级缓存
  6. gtp文件服务器,GTP中文网吉它谱吉他谱guitar网站
  7. windows7中安装jdk1.8
  8. java开源springboot项目_使用Spring Boot的10多个免费开源项目
  9. SAP License:SAP的2021关键词:协作
  10. 原来 Kylin 的增量构建,大有学问! | 原力计划
  11. Zabbix Lack of free swap space
  12. 基于python的随机森林回归实现_python实现随机森林
  13. 【图像加密】基于matlab GUI混沌系统图像加密解密【含Matlab源码 147期】
  14. Google Code Review在代码审查中寻找什么
  15. Java快递物流运输管理系统源码
  16. 旧手机改文件储存服务器,旧手机改成云服务器
  17. 国内比较好的云服务提供商有哪些?
  18. 蓝桥杯试题——随意组合
  19. Google推出网页加速工具 - Page Speed (Firefox插件)
  20. JAVA实验三(南邮)

热门文章

  1. LINUX下启动多个MYSQL服务
  2. 思科交换机2950 强制恢复出厂设置(清密码)
  3. 开始新的学习之旅--PHP开发学习--基础部分笔记
  4. 重装64位WIN7之后再装KUBUNTU遇到的问题
  5. 组建优秀的团队-实现目标的开始
  6. c语言strTrimed函数用法介绍,c语言对字符串实现高效trim函数
  7. 深度学习推荐模型-WideDeep
  8. 大数据分析平台具备什么功能特点
  9. python select模块_深入理解python中的select模块
  10. C# 笔记2 - 数组、集合与与文本文件处理