本文是按照狂神说的教学视频学习的笔记,强力推荐,教学深入浅出一遍就懂!b站搜索狂神说或点击下面链接

https://space.bilibili.com/95256449?spm_id_from=333.788.b_765f7570696e666f.2

AOP

定义:

  • AOP (Aspect Oriented Programming) :面向切面编程

  • 个人理解其实就是spring下的动态代理模式,说白了就是不影响原代码的情况下,横向增加代码扩充功能的设计思想.

  • 通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。

  • AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容。是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

  • 官方文档定义个人理解说明:

  • 横切关注点:跨越应用程序多个模块的方法或功能。

  • 切面(ASPECT) :横切关注点被模块化的特殊对象。

    • 我们要自定义一个类,里面写上代理对象附加要实现的内容,这个类就是切面。

  • 通知(Advice) :切面必须要完成的工作。

    • 代理对象附加要实现的内容

  • 目标(Target) :被通知对象。

    • 真实对象。

  • 代理(Proxy) :向目标对象应用通知之后创建的对象。

    • 代理对象,Spring AOP封装了代理对象的生成,我们不用管怎么生成的。

  • 切入点(PointCut) :切面通知执行的“地点”的定义。

    • 什么地方要执行代理对象的方法,比如定义某个类,那么这个类的所有方法都会执行代理方法。

  • 连接点UointPoint) :与切入点匹配的执行点。

  • 导入依赖

<!-- https://mvnrepository.com/artifact/org.aspectj/aspectjweaver -->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.5</version>
</dependency>
​

方式一:使用原生Spring API接口

  • 需要注入aop配置

<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
  • 真实对象接口

package com.rzp.service;
​
public interface UserService {
public void add();
public void delete();
public void update();
public void query();
}
​
  • 真实对象

package com.rzp.service;
​
//真实对象
public class UserServiceImpl implements UserService {
public void add() {
System.out.println("增加用户");
}
​
public void delete() {
System.out.println("删除用户");
}
​
public void update() {
System.out.println("更新用户");
}
​
public void query() {
System.out.println("查询用户");
}
}
​
  • LOG类

package com.rzp.log;
​
import org.springframework.aop.MethodBeforeAdvice;
​
import java.lang.reflect.Method;
//继承MethodBeforeAdvice,重写的before方法会在真实对象方法执行前会执行
public class Log implements MethodBeforeAdvice {
​
//method 要执行的目标对象的方法
//Object[] 参数
//target 目标对象
public void before(Method method, Object[] objects, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"方法被执行了");
}
}
​
  • afterLog类

package com.rzp.log;
​
import org.springframework.aop.AfterReturningAdvice;
​
import java.lang.reflect.Method;
​
//继承AfterReturningAdvice,重写的afterReturning会在真实对象方法执行后执行
public class AfterLog implements AfterReturningAdvice {
​
//returnValue 返回值
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"返回结果为"+returnValue);
}
}
​
  • xml

<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
<bean id="userService" class="com.rzp.service.UserServiceImpl"/>
<bean id="log" class="com.rzp.log.Log"/>
<bean id="afterlog" class="com.rzp.log.AfterLog"/>
​
<!--方式一,使用原生Spring API接口-->
<!--配置aop-->
<aop:config>
<!--定义切入点,在什么地方切入 expression:表达式 executiion(要执行的位置)-->
<!--1、execution(): 表达式主体。
2、第一个*号:表示返回类型,*号表示所有的类型。
3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子孙包下所有类的方法。
4、第二个*号:表示类名,*号表示所有的类。
5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数-->
<aop:pointcut id="pointc" expression="execution(* com.rzp.service.UserServiceImpl.*(..))"/>
<!--执行环绕增加-->
<aop:advisor advice-ref="log" pointcut-ref="pointc"/>
<aop:advisor advice-ref="afterlog" pointcut-ref="pointc"/>
</aop:config>
​
</beans>
  • 测试

import com.rzp.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
​
public class Mytest {
public static void main(String[] args) {
ApplicationContext contest = new ClassPathXmlApplicationContext("beans.xml");
UserService user = contest.getBean("userService", UserService.class);
user.add();
}
}
​

方式二:使用自定义类来实现AOP

  • 自定义类

package com.rzp.diy;
​
public class DiyPointCut {
public void before(){
System.out.println("方法执行前");
}
​
public void after(){
System.out.println("方法执行后");
}
}
​
  • xml

<?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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd">
​
​
<bean id="userService" class="com.rzp.service.UserServiceImpl"/>
<bean id="log" class="com.rzp.log.Log"/>
<bean id="afterlog" class="com.rzp.log.AfterLog"/>
​
<!--方式二,使用自定义类-->
<bean id="diy" class="com.rzp.diy.DiyPointCut"/>
<aop:config>
<!--自定义切面,ref引用的类-->
<aop:aspect ref="diy">
<aop:pointcut id="point" expression="execution(* com.rzp.service.UserServiceImpl.*(..))"/>
<!--通知-->
<aop:before method="before" pointcut-ref="point"/>
<aop:after method="after" pointcut-ref="point"/>
</aop:aspect>
</aop:config>
​
​
​
</beans>
  • 测试

import com.rzp.service.UserService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
​
public class Mytest {
public static void main(String[] args) {
ApplicationContext contest = new ClassPathXmlApplicationContext("beans.xml");
UserService user = contest.getBean("userService", UserService.class);
user.add();
}
}
​
  • 没有方式一好,因为方式一还能通过反射获取到真实对象的信息。

方式三:使用注解实现

<?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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
​
​
<bean id="userService" class="com.rzp.service.UserServiceImpl"/>
<bean id="log" class="com.rzp.log.Log"/>
<bean id="afterlog" class="com.rzp.log.AfterLog"/> <!--方式三:使用注解开发-->
<context:component-scan base-package="com.rzp.*"/>
<context:annotation-config/>
<!--开启注解AOP支持,JDK5 默认proxy-target-class="false" Cglib proxy-target-class="true"-->
<aop:aspectj-autoproxy proxy-target-class="true"/>
​
</beans>
  • 自定义类

package com.rzp.diy;
​
​
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;
​
@Aspect //标注这个类是一个切面
@Component
public class AnnotationPointCut {
@Before("execution(* com.rzp.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("before");
}
​
@After("execution(* com.rzp.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("after");
}
​
//在环绕增强中,我们可以给定一个参数,代表我们要获取处理切入的点
@Around("execution(* com.rzp.service.UserServiceImpl.*(..))")
public void around(ProceedingJoinPoint jp) throws Throwable {
System.out.println("环绕前");
​
//获得签名--其实就是执行了真实对象的什么方法
Signature signature = jp.getSignature();
System.out.println("sign"+signature);
//执行方法
Object proceed = jp.proceed();
​
System.out.println("环绕后");
System.out.println(proceed);
}
}
​
  • 其他不变,测试结果

Spring (五):AOP的更多相关文章

  1. Spring(五)AOP简述

    一.AOP简述 AOP全称是:aspect-oriented programming,它是面向切面编号的思想核心, AOP和OOP既面向对象的编程语言,不相冲突,它们是两个相辅相成的设计模式型 AOP ...

  2. 跟着刚哥学习Spring框架--AOP(五)

    AOP AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.OOP引入 ...

  3. Spring框架第五篇之Spring与AOP

    一.AOP概述 AOP(Aspect Orient Programming),面向切面编程,是面向对象编程OOP的一种补充.面向对象编程是从静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运 ...

  4. Spring 学习十五 AOP

    http://www.hongyanliren.com/2014m12/22797.html 1: 通知(advice): 就是你想要的功能,也就是安全.事物.日子等.先定义好,在想用的地方用一下.包 ...

  5. Spring学习之旅(五)--AOP

    什么是 AOP AOP(Aspect-OrientedProgramming,面向方面编程),可以说是 OOP(Object-Oriented Programing,面向对象编程)的补充和完善. OO ...

  6. Spring实现AOP的4种方式

    了解AOP的相关术语:1.通知(Advice):通知定义了切面是什么以及何时使用.描述了切面要完成的工作和何时需要执行这个工作.2.连接点(Joinpoint):程序能够应用通知的一个“时机”,这些“ ...

  7. spring的AOP

    最近公司项目中需要添加一个日志记录功能,就是可以清楚的看到谁在什么时间做了什么事情,因为项目已经运行很长时间,这个最初没有开来进来,所以就用spring的面向切面编程来实现这个功能.在做的时候对spr ...

  8. Spring 实践 -AOP

    Spring 实践 标签: Java与设计模式 AOP引介 AOP(Aspect Oriented Programing)面向切面编程采用横向抽取机制,以取代传统的纵向继承体系的重复性代码(如性能监控 ...

  9. Spring实现AOP的4种方式(转)

    转自:http://blog.csdn.net/udbnny/article/details/5870076 Spring实现AOP的4种方式 先了解AOP的相关术语:1.通知(Advice):通知定 ...

  10. spring(二) AOP之AspectJ框架的使用

    前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...

随机推荐

  1. 结题报告--hih0CoderP1041

    题目:点此 描述 小Hi和小Ho准备国庆期间去A国旅游.A国的城际交通比较有特色:它共有n座城市(编号1-n):城市之间恰好有n-1条公路相连,形成一个树形公路网.小Hi计划从A国首都(1号城市)出发 ...

  2. vue基础----自定义组件directive ,bind,update,insert

    <div id="app"> <input type="text" v-limit.3="msg" v-focus> ...

  3. MySQL InnoDB表的碎片量化和整理(data free能否用来衡量碎片?)

    网络上有很多MySQL表碎片整理的问题,大多数是通过demo一个表然后参考data free来进行碎片整理,这种方式对myisam引擎或者其他引擎可能有效(本人没有做详细的测试).对Innodb引擎是 ...

  4. Natas24 Writeup(strcmp绕过漏洞)

    Natas24: 一个登录页面,查看源码,发现关键代码: if(array_key_exists("passwd",$_REQUEST)){ if(!strcmp($_REQUES ...

  5. angular6 增加webpack配置 亲测可用

    核心 Angular Cli 6 禁用了webpack的自定义配置,官方似乎并未提供自定义配置webpack的方法. 在此之前,可以使用ng eject把默认的webpack提取到代码中,进行自定义. ...

  6. org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException: sendDefaultImpl call timeout 和 RocketmqRemoting closeChannel: close the connection to remote address[] result: true

    org.apache.rocketmq.remoting.exception.RemotingTooMuchRequestException: sendDefaultImpl call timeout ...

  7. 矩阵快速幂-QuickPow

    矩阵快速幂引入: 1.整数快速幂: 为了引出矩阵的快速幂,以及说明快速幂算法的好处,我们可以先求整数的幂.如果现在要算X^8:则 XXXXXXXX 按照寻常思路,一个一个往上面乘,则乘法运算进行7次. ...

  8. iOS开发:Swift/Objective-C高效生成随机字符串

    原文连接 Objective-C版 // 随机生成字符串(由大小写字母.数字组成) + (NSString *)random: (int)len { char ch[len]; for (int in ...

  9. Selenium IDE安装及简介

    一.Selenium IDE安装 Selenium IDE是Firefox浏览器的一个插件,依附于Firefox浏览器.在网上搜了Selenium IDE的安装教程,大部分都是说在官网下载安装,其实最 ...

  10. Java并发编程之CAS第三篇-CAS的缺点及解决办法

    Java并发编程之CAS第三篇-CAS的缺点 通过前两篇的文章介绍,我们知道了CAS是什么以及查看源码了解CAS原理.那么在多线程并发环境中,的缺点是什么呢?这篇文章我们就来讨论讨论 本篇是<凯 ...