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 切入点表达式
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
随机推荐
- Centos 7.4搭建es7.12.0+Skywalking7.8.5
Skywalking整体架构图和分布式追踪系统原理:https://blog.csdn.net/weixin_39866487/article/details/111581322 软件包版本1.ela ...
- 【Java】Files.readAllBytes(Path) 遇见的坑
Files.readAllBytes(Path)方法把整个文件读入内存,此方法返回一个字节数组,还可以把结果传递给String的构造器,以便创建字符串输出. 在针对大文件的读取的时候,可能会出现内存不 ...
- springboot项目部署docker服务器提供api
1.先将springboot项目打包,我这里用的是IDEA工具打包,打包完成后的jar包在 项目目录/target 中 2.打包完成后进入服务器器终端,将jar包上传到自己设置的目录中,这个目录需要跟 ...
- IOS 小组件(7):小组件点击交互
引言 前面我们似乎掌握了实现一个小组件所需要的一切技能,默认情况下桌面点击小组件,也正常跳转到了App中.接下来我们一起来看看,小组件是怎么做到点击跳转到App的. 点击交互方式 点击Widget ...
- 多边形游戏——区间dp
题目描述 多边形(Polygon)游戏是单人玩的游戏,开始的时候给定一个由N个顶点构成的多边形(图1所示的例子中,N=4),每个顶点被赋予一个整数值,而每条边则被赋予一个符号:+(加法运算)或者*(乘 ...
- 分布式调度任务-ElasticJob
一:问题的引出与复现 在一个风和日丽的工作日,公司运营发现系统的任务数据没有推送执行,整个流程因此停住了.我立马远程登陆服务器,查看日志,好家伙,系统在疯狂的打印相同的一段日志:c.d.d.j.i.e ...
- AI 芯片的分类及技术
AI 芯片的分类及技术 人工智能芯片有两种发展路径:一种是延续传统计算架构,加速硬件计算能力,主要以 3 种类型的芯片为代表,即 GPU. FPGA. ASIC,但 CPU依旧发挥着不可替代的作用:另 ...
- 基于ARM Cortex-M的SoC存储体系结构和实战
基于ARM Cortex-M的SoC存储体系结构和实战 System on Chip Architecture Tutorial Memory Architecture for ARM Cortex- ...
- SpringBoot+SpringDataJpa快速上手(基本CRUD)
以及表结构和数据 依赖 <!-- 如果有SpringBoot启动器,就不加--> <parent> <groupId>org.springframework.boo ...
- python+requests接口用例
本实例通过请求接口登录系统,获取了配置项的ID,并最终实现了对配置项的默认值进行修改 使用到的接口请求方法有:get(查询) ,post(新增),put(修改) 遇到的阻碍点见下面具体代码处的详解 编 ...