Spring:Spring-AOP简介
什么是SpringAOP?
将一些相关的编程方法,独立提取出来,独立实现,然后动态地将代码切入到类的指定方法、指定位置上的编程方式就是AOP(面向切面编程)。
讲解一下AOP中的相关概念
Aspect(切面): Aspect 声明类似于 Java 中的类声明,在 Aspect 中会包含着一些 Pointcut 以及相应的 Advice。
Joint point(连接点):表示在程序中明确定义的点,典型的包括方法调用,对类成员的访问以及异常处理程序块的执行等等,它自身还可以嵌套其它 joint point。
Pointcut(切点):表示一组 joint point,这些 joint point 或是通过逻辑关系组合起来,或是通过通配、正则表达式等方式集中起来,它定义了相应的 Advice 将要发生的地方。
Advice(增强):Advice 定义了在 Pointcut 里面定义的程序点具体要做的操作,它通过 before、after 和 around 来区别是在每个 joint point 之前、之后还是代替执行的代码。
Target(目标对象):织入 Advice 的目标对象.。
Weaving(织入):将 Aspect 和其他对象连接起来, 并创建 Adviced object 的过程
实例:
接口:
package com.java.test6; /**
* @author nidegui
* @create 2019-06-23 9:40
*/
public interface Student {
public void addStudent(String name);
}
实现类:
package com.java.test6; /**
* @author nidegui
* @create 2019-06-23 9:41
*/
public class StudentImpl implements Student {
@Override
public void addStudent(String name) {
System.out.println("添加学生"+name);
}
}
切点增强类:
package com.java.test6; import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint; /**
* @author nidegui
* @create 2019-06-23 9:45
*/
public class StudentServiceAspect {
//前置通知,在方法之前通知
public void before(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("开始添加学生:"+jp.getArgs()[0]); System.out.println("开始添加学生");
}
//后置通知
public void doAfter(JoinPoint jp){
System.out.println("类名:"+jp.getTarget().getClass().getName());
System.out.println("方法名:"+jp.getSignature().getName());
System.out.println("学生添加完成:"+jp.getArgs()[0]);
}
//环绕通知
public Object doAround(ProceedingJoinPoint pjp) throws Throwable{
System.out.println("添加学生前");
Object retVal=pjp.proceed();
System.out.println(retVal);
System.out.println("添加学生后");
return retVal;
}
//返回通知
public void doAfterReturning(JoinPoint jp){
System.out.println("返回通知");
}
//异常通知
public void doAfterThrowing(JoinPoint jp,Throwable ex){
System.out.println("异常通知");
System.out.println("异常信息:"+ex.getMessage());
} }
Spring配置文件:
<?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="studentImpl" class="com.java.test6.StudentImpl"></bean>
<bean id="studentAcpect" class="com.java.test6.StudentServiceAspect"></bean> <aop:config>
<aop:aspect id="studentAcpect" ref="studentAcpect">
<!--定义一个切点-->
<aop:pointcut id="b" expression="execution(* com.java.test6.*.*(..))"></aop:pointcut>
<!--定义前置通知-->
<aop:before method="before" pointcut-ref="b"></aop:before>
<!--后置通知-->
<aop:after method="doAfter" pointcut-ref="b"></aop:after>
<!--环绕通知-->
<aop:around method="doAround" pointcut-ref="b"/>
<!--返回通知-->
<aop:after-returning method="doAfterReturning" pointcut-ref="b"/>
<!--异常通知-->
<aop:after-throwing method="doAfterThrowing" pointcut-ref="b" throwing="ex"/> </aop:aspect>
</aop:config>
</beans>
测试类:
package com.java.test6; import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; /**
* @author nidegui
* @create 2019-06-22 14:47
*/
public class Test {
public static void main(String[] args) {
ApplicationContext ac = new ClassPathXmlApplicationContext("beanss.xml"); Student people =(Student) ac.getBean("studentImpl");
people.addStudent("zhangsna"); }
}
控制台输出日志:

文章转载至:https://www.cnblogs.com/nidegui/p/11072014.html
Spring:Spring-AOP简介的更多相关文章
- Spring中AOP简介与切面编程的使用
Spring中AOP简介与使用 什么是AOP? Aspect Oriented Programming(AOP),多译作 "面向切面编程",也就是说,对一段程序,从侧面插入,进行操 ...
- AOP 与 Spring中AOP使用(上)
AOP简介 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程, 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. AOP是OOP的延续 ...
- Spring AOP 简介
Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...
- Spring中的面向切面编程(AOP)简介
一.什么是AOP AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面 ...
- Spring AOP简介与底层实现机制——动态代理
AOP简介 AOP (Aspect Oriented Programing) 称为:面向切面编程,它是一种编程思想.AOP 是 OOP(面向对象编程 Object Oriented Programmi ...
- Spring AOP 简介(三)
Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...
- Spring Aop(一)——Aop简介
转发地址:https://www.iteye.com/blog/elim-2394629 1 Aop简介 AOP的全称是Aspect Oriented Programming,翻译成中文是面向切面编程 ...
- Spring中Aop的扩展及剖析
AOP简介: 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范 ...
- Spring(三)AOP面向切面编程
原文链接:http://www.orlion.ga/205/ 一.AOP简介 1.AOP概念 参考文章:http://www.orlion.ml/57 2.AOP的产生 对于如下方法: pub ...
- [原创]java WEB学习笔记105:Spring学习---AOP介绍,相关概念,使用AOP,利用 方法签名 编写 AspectJ 切入点表达式
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
随机推荐
- C++知识点案例 笔记-4
1.纯虚函数 2.抽象类 3.内部类 4.运算符重载 5.类的函数重载 6.友元的函数重载 1.纯虚函数 ==纯虚函数== //有时基类中无法给出函数的具体体现,定义纯虚函数可以为派生函数保留一个函数 ...
- Linux useradd 命令介绍
Linux useradd 命令介绍 作者: Alan Formy-duval 译者: LCTT Brooke Lau | 2020-01-06 22:58 使用 useradd 命令来添加用户(并且 ...
- 标准Gitlab命令行操作指导
gitlab是一个分布式的版本仓库,总比只是一个本地手动好些,上传你的本地代码后后还能web GUI操作,何乐不为? 贴上刚刚搭建的gitlab,看看git 如何操作标准命令行操作指导 1.命令行操作 ...
- MySQL 通过.frm文件和.ibd文件实现InnoDB引擎的数据恢复
起因是这样的,公司的领导表示说服务器崩了,修理好之后,只剩下数据库目录下的物理文件(即.frm文件与.ibd文件).然后,整了一份压缩包给我,叫我瞅一下能不能把数据恢复出来.我当场愣了一下,这都啥文件 ...
- SparkCore之业务操作逻辑
在上spark的时候,一开始需要虚拟机模拟真实环境,而spark主要的三种模式:local.standalone.yarn 均可以通过虚拟机模拟. 这里要讨论的是业务逻辑如何和 spark 结合,具体 ...
- springboot打包上线
发布到线上的包结构 runtime是发布到线上的目录结构 1.项目pom.xml添加打包配置 <build> <plugins> <plugin> <grou ...
- 华为MDC软件架构
华为MDC软件架构 平台软件零层逻辑架构如下图,由基础层.功能层.应用层和服务层组成. 零层逻辑架构 从平台软件一层逻辑架构可以看出,MDC用了华为自研的越影操作系统.兼容Autosar标准的软件中间 ...
- 人工智能训练云燧T10
人工智能训练云燧T10 基于邃思芯片打造的面向云端数据中心的人工智能训练加速产品,具有高性能.通用性强.生态开放等优势,可广泛应用于互联网.金融.教育.医疗.工业及政务等人工智能训练场景. 超强算力 ...
- TVM中的调度原语
TVM中的调度原语 TVM是一种用于高效内核构造的领域专用语言. 本文将展示如何通过TVM提供的各种原语来调度计算. from __future__ import absolute_import, p ...
- python_selenium_键盘事件
引言 ----在实际的web测试工作中,需要配合键盘按键来操作,webdriver的 keys()类提供键盘上所有按键的操作,还可以模拟组合键Ctrl+a,Ctrl+v等. 举例: #cording ...