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 切入点表达式
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
随机推荐
- Autofac 集成测试 在 ConfigureContainer 之后进行 Mock 注入
在使用 Autofac 框架进行开发后,编写集成测试时,需要用 Mock 的用于测试的模拟的类型去代替容器里面已注入的实际类型,也就需要在 Autofac 完全收集完成之后,再次注入模拟的对象进行覆盖 ...
- XShell本地上传文件到Ubuntu上及从Ubuntu下载文件到本地
使用XShell本地上传文件到Ubuntu上及从Ubuntu下载文件到本地. 1.第一种方法是最常用的 :如果下载了Xshell和Xftp,Ctrl+Alt+F就可以选择文件的互传了!(虚拟机/云服务 ...
- 【Azure 环境】Azure Key Vault (密钥保管库)中所保管的Keys, Secrets,Certificates是否可以实现数据粒度的权限控制呢?
问题描述 Key Vault (密钥保管库) 能不能针对用户授权实现指定用户只能访问某个或某些特定的key? 如当前有两个用户(User1, User2),在Key Vault中有10个Key,Use ...
- Nginx 配置实例-配置动静分离
Nginx 配置实例-配置动静分离 1. 静态资源的创建 2. nginx 动静分离的配置 3. 验证 1. 静态资源的创建 这里使用的静态资源主要为 HTML 静态文件和图片. mkdir -vp ...
- Java如何利用for循环在控制台输出正方形对角线图形
1 /* 2 利用循环在控制台输出如下正方形对角线图形 3 * * * * * * * * * * * 4 * * * * 5 * * * * 6 * * * * 7 * * * * 8 * * * ...
- fiddler操作详情
1.设置fiddler请求过滤 2.请求与响应的格式内容 3.拦截请求操作 a.按F11开始拦截,发送请求 b.修改请求数据 c.SHIFT+F11关闭拦截 d.run to complete,把修 ...
- Appium UI自动化测试
#coding:utf-8 from appium import webdriver import time,traceback ''' desired_capabilities:这些键告诉appiu ...
- 二、Nginx 服务器升级
1,编译新版本的nginx 软件 [root@client lnmp_soft]# tar -xf nginx-1.12.2.tar.gz -C .. [root@client lnmp_soft ...
- CENTOS7 安装 SYNCTHING
本地电脑需要同步远程数据,安装syncthing 测试 1:下载 wget https://github.com/syncthing/syncthing/releases/download/v1.5. ...
- 【NX二次开发】NX对象类型及基本操作
说明:NX中的所有对象都是通过唯一的tag_t值进行标识的,这些对象大致可以分为部件对象.UF对象.表达式.链表对象和属性对象等. 部件对象的操作: 基本操作函数: 1. UF_PART_new() ...