什么是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简介的更多相关文章

  1. Spring中AOP简介与切面编程的使用

    Spring中AOP简介与使用 什么是AOP? Aspect Oriented Programming(AOP),多译作 "面向切面编程",也就是说,对一段程序,从侧面插入,进行操 ...

  2. AOP 与 Spring中AOP使用(上)

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

  3. Spring AOP 简介

    Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...

  4. Spring中的面向切面编程(AOP)简介

    一.什么是AOP AOP(Aspect-Oriented Programming, 面向切面编程): 是一种新的方法论, 是对传统 OOP(Object-Oriented Programming, 面 ...

  5. Spring AOP简介与底层实现机制——动态代理

    AOP简介 AOP (Aspect Oriented Programing) 称为:面向切面编程,它是一种编程思想.AOP 是 OOP(面向对象编程 Object Oriented Programmi ...

  6. Spring AOP 简介(三)

    Spring AOP 简介 如果说 IoC 是 Spring 的核心,那么面向切面编程就是 Spring 最为重要的功能之一了,在数据库事务中切面编程被广泛使用. AOP 即 Aspect Orien ...

  7. Spring Aop(一)——Aop简介

    转发地址:https://www.iteye.com/blog/elim-2394629 1 Aop简介 AOP的全称是Aspect Oriented Programming,翻译成中文是面向切面编程 ...

  8. Spring中Aop的扩展及剖析

    AOP简介: 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范 ...

  9. Spring(三)AOP面向切面编程

    原文链接:http://www.orlion.ga/205/ 一.AOP简介 1.AOP概念 参考文章:http://www.orlion.ml/57 2.AOP的产生 对于如下方法:     pub ...

  10. [原创]java WEB学习笔记105:Spring学习---AOP介绍,相关概念,使用AOP,利用 方法签名 编写 AspectJ 切入点表达式

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

随机推荐

  1. Mysql不知道默认密码情况下登录/重置/忘记密码

    场景一: 基础系统:linux 镜像:LAMP环境(Ubuntu 18.04 Apache PHP7.0) 问题:ERROR 1405 (28000): Access denied for user ...

  2. HC-05底层驱动

    INT8U BT_INIT(const char * pNAME, INT32U BAUD, INT8U ROLE, INT32U PSWD) { INT8U OS_ERR = OS_ERR_NONE ...

  3. Centos 7.4搭建es7.12.0+Skywalking7.8.5

    Skywalking整体架构图和分布式追踪系统原理:https://blog.csdn.net/weixin_39866487/article/details/111581322 软件包版本1.ela ...

  4. pre -regulator 前端稳压器

    regulator

  5. MyBatis 回顾 JDBC(一)

    引言 学过 Java 的童鞋都知道,在 Java 中只有 JDBC 可以访问数据库,但是只要使用过 JDBC 的同学肯定也感受到 JDBC 访问数据库的繁琐, 需要编写大量的代码,经历一系列的步骤. ...

  6. spring mvc下实现通过邮箱找回密码功能

    1功能分析 通过spring mvc框架实现通过邮箱找回密码. 2 实现分析 主要是借助某个邮箱的pop3/smtp服务实现的邮件代发功能. 3 源码分析 3.1首先在用户表对应的javabean中加 ...

  7. -bash: $'\201ccd': δ 的错误是linux编码问题(Centos7)

    如果目录是中文目录,你的编码为: [root@dbbd-api01 ~]# cat /etc/locale.conf LANG=zh_CN.GB18030 [root@dbbd-api01 ~]# 那 ...

  8. C语言编程 菜鸟练习100题(11-20)

    [练习11]计算 int, float, double 和 char 字节大小 0. 题目: 计算 int, float, double 和 char 字节大小 1. 分析: 使用 sizeof 操作 ...

  9. System Verilog MCDF(二)

    整形器的接口时序: reg,grant是维持了两个clk的. chid ,length在发送数据期间不可以变化. 第一个data数据必须在start上升沿的同一个clk发送. reg,grant两者之 ...

  10. leetcode -- 二进制

    leetcode -- 二进制 在学习编程语言的运算符时,大部分语言都会有与,或等二进制运算符,我在初期学习这些运算符的时候,并没有重点留意这些运算符,并且在后续的业务代码中也没有频繁的使用过,直到后 ...