6、AOP实现切入

  • AOP为Aspect Oriented Programming的缩写,意为:面向切面编程
  • 通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术
  • AOP是OOP的延续,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型
  • 利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。

6.1实现切入的三种方式

6.1.1方式一,spring接口实现

  1. 定义一个实现切入的接口
public interface BookService {

    void addBook();

    void deleteBook();

    void updateBook();

    void queryBook();

}
  1. 实现该接口的类
public class BookServiceImpl implements BookService{
@Override
public void addBook() {
System.out.println("增加了一本图书");
} @Override
public void deleteBook() {
System.out.println("删除了一本图书");
} @Override
public void updateBook() {
System.out.println("更新了一本图书");
} @Override
public void queryBook() {
System.out.println("查询了一本图书");
}
}
  1. 定义切入前操作,实现MethodBeforeAdvice这个接口
//第一个切入点
public class BeforeLog implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
if(target != null){
System.out.println(target.getClass().getName() + "执行力"+ method.getName() + "方法!");
} }
}
//第二个切入点
public class BeforeAfterLog implements MethodBeforeAdvice {
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println("最开始的");
}
}
  1. 定义切入之后操作,实现AfterReturningAdvice接口
public class AfterLog implements AfterReturningAdvice {
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
if(target != null){
System.out.println(target.getClass().getName() + "执行力"+ method.getName() + "方法!");
}
System.out.println("returnValue: "+ returnValue);
}
}

注:可以实现多个切入操作

  1. spring的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="bookServiceImpl" class="com.mhy.aop.service.BookServiceImpl"/>
<bean id="beforeLog" class="com.mhy.aop.Log.BeforeLog"/>
<bean id="afterLog" class="com.mhy.aop.Log.AfterLog"/>
<bean id="beforeAfterLog" class="com.mhy.aop.Log.BeforeAfterLog"/> <aop:config>
<!-- 切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.mhy.aop.service.BookServiceImpl.*(..))"/>
<!-- 执行环绕增加-->
<aop:advisor advice-ref="beforeAfterLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
</aop:config> </beans>
  1. 测试
    @Test
public void test1(){
ApplicationContext context = new ClassPathXmlApplicationContext("bookbeans.xml"); BookService bookService = context.getBean("bookServiceImpl", BookService.class); bookService.deleteBook();
} //结果
/*
最开始的
com.mhy.aop.service.BookServiceImpl执行力deleteBook方法!
删除了一本图书
com.mhy.aop.service.BookServiceImpl执行力deleteBook方法!
returnValue: null
*/

6.1.2方式二,自定义切入面实现

  1. 自定义一个切入点的类 DiyPointcut
public class DiyPointcut {
public void before(){
System.out.println("方法执行前");
} public void after(){
System.out.println("方法执行后");
}
}
  1. 在配置文件中配置
    <aop:config>
<aop:aspect ref="diyPointcut">
<!-- 切入点-->
<aop:pointcut id="pointcut" expression="execution(* com.mhy.aop.service.BookServiceImpl.*(..))"/>
<!-- 通知-->
<aop:before method="before" pointcut-ref="pointcut"/>
<aop:after method="after" pointcut-ref="pointcut"/>
</aop:aspect>
</aop:config>
  1. 测试结果
方法执行前
删除了一本图书
方法执行后

6.1.3方式三,注解实现

  1. 开启注解驱动
<!--开启注解注册的IOC容器的驱动-->
<context:component-scan base-package="com.mhy.aop"/>
<context:annotation-config/>
<!-- AOP方式三注解-->
<!-- 开启AOP注解驱动-->
<aop:aspectj-autoproxy />
<!--
proxy-target-class这个默认是false表示使用JDK实现
<aop:aspectj-autoproxy proxy-target-class="false"/>
proxy-target-class这个是true表示使用cglib实现
<aop:aspectj-autoproxy proxy-target-class="true"/>
-->
  1. 切面类 AnnotationPointcut

    • @Aspect:定义切面注解
    • @After("execution( com.mhy.aop.service.BookServiceImpl.*(..))")*:定义切入点前的注解
    • @After("execution( com.mhy.aop.service.BookServiceImpl.*(..))")*:定义切入点后的注解
    • @Around("execution( com.mhy.aop.service.BookServiceImpl.*(..))")*:定义环绕切入点注解
@Component(value = "annotationPointcut")
//注解定义切入面
@Aspect
public class AnnotationPointcut { //注解定义方法前切入点
@Before("execution(* com.mhy.aop.service.BookServiceImpl.*(..))")
public void before(){
System.out.println("========方法执行前=========");
} //注解定义方法后切入点
@After("execution(* com.mhy.aop.service.BookServiceImpl.*(..))")
public void after(){
System.out.println("========方法执行后=========");
} //注解定义环绕切入
@Around("execution(* com.mhy.aop.service.BookServiceImpl.*(..))")
public void around(ProceedingJoinPoint point) throws Throwable {
//ProceedingJoinPoint被切入对象的信息的获取
System.out.println("环绕前");
//执行被切入对象的方法
point.proceed();
System.out.println("环绕后"); System.out.println(point.getSignature());
System.out.println(point); } }
  1. 测试结果
环绕前
========方法执行前=========
删除了一本图书
========方法执行后=========
环绕后
void com.mhy.aop.service.BookService.deleteBook()
execution(void com.mhy.aop.service.BookService.deleteBook())

AOP实现切入的更多相关文章

  1. spring框架整合springMVC时关于AOP无法切入的问题

    最开始springMVC的配置为: spring的配置为: 分析可知道spring的配置正确,由于在springmvc中已经扫描了@Controller相关的注解,所以就不需要再次扫描了,由于spri ...

  2. 关于AOP无法切入同类调用方法的问题

    一.前言 Spring AOP在使用过程中需要注意一些问题,也就是平时我们说的陷阱,这些陷阱的出现是由于Spring AOP的实现方式造成的.每一样技术都或多或少有它的局限性,很难称得上完美,只要掌握 ...

  3. Spring学习之Spring中AOP方式切入声明式事务

    mybatis-spring官方文档说明 一个使用 MyBatis-Spring 的其中一个主要原因是它允许 MyBatis 参与到 Spring 的事务管理中.而不是给 MyBatis 创建一个新的 ...

  4. Spring系列之AOP

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

  5. Spring AOP 深入剖析

    AOP是Spring提供的关键特性之一.AOP即面向切面编程,是OOP编程的有效补充.使用AOP技术,可以将一些系统性相关的编程工作,独立提取出来,独立实现,然后通过切面切入进系统.从而避免了在业务逻 ...

  6. Spring AOP 实现功能权限校验功能

    版权声明:本文为博主原创文章,未经博主允许不得转载.   目录(?)[-] 使用拦截器实现未登录时跳转到登录界面的功能 1 拦截器SecurityInterceptor 2spring-mvcxml拦 ...

  7. OOP的完美点缀—AOP之SpringAOP实现原理

    OOP的完美点缀-AOP之SpringAOP实现原理 前言 OOP与AOP OOP(Object Oriented Programming,面向对象编程),通过封装.继承将程序抽象为各个层次的对象,进 ...

  8. spring aop + xmemcached 配置service层缓存策略

    Memcached 作用与使用 基本介绍 1,对于缓存的存取方式,简言之,就是以键值对的形式将数据保存在内存中.在日常业务中涉及的操作无非就是增删改查.加入缓存机制后,查询的时候,对数据进行缓存,增删 ...

  9. 面试题思考:解释一下什么叫AOP(面向切面编程)

    这种在运行时,动态地将代码切入到类的指定方法.指定位置上的编程思想就是面向切面的编程. AOP是Spring提供的关键特性之一.AOP即面向切面编程,是OOP编程的有效补充. 使用AOP技术,可以将一 ...

随机推荐

  1. data:image字符转byte[]

    var data = "data:image/bmp;base64,Qk3aHwAAAAAAADYAAAAoAAAAZAAAABsAAAABABgAAAAAAKQfAAAAAQAAAAEAA ...

  2. 796. Rotate String - LeetCode

    Question 796. Rotate String Solution 题目大意:两个字符串匹配 思路:Brute Force Java实现: public boolean rotateString ...

  3. c++:-9

    上节(c++:-8)主要学习了C++的流类库和输入输出,本节学习C++的异常处理. 异常处理 介绍 (1)异常处理的基本思想: (2)异常处理的语法: (3)举例:处理除0异常 #include &l ...

  4. 每天一个 HTTP 状态码 205

    205 Reset Content 205 Reset Content 表示服务器成功地处理了客户端的请求,要求客户端重置它发送请求时的文档视图.这个响应码跟 204 No Content 类似,也不 ...

  5. 详解TCP四次挥手(断开TCP连接过程)

    在讲述TCP四次挥手,即断开TCP连接的过程之前,需要先介绍一下TCP协议的包结构. TCP协议包结构: 这里只对涉及到四次挥手过程的字段做解释 (1) 序号(Sequence number) 我们通 ...

  6. Random方法中的nextInt(int arg0)方法讲解

    nextInt方法会生成一个随机的在5以内的数,负载均衡随机策略底层用的就是这个方法: Random rand = new Random(); int index = rand.nextInt(5); ...

  7. vue组件data函数

    vue组件data通常定义为一个函数并return一个对象,对象中定义的就是组件数据,当然定义数据还有props.computed等方式. data如果直接定义为对象data: {message: ' ...

  8. mybatis-plus分页插件

    package com.tanhua.server.config; import com.baomidou.mybatisplus.extension.plugins.PaginationInterc ...

  9. 《SQL Server基础——SQL语句》

    SQL Server基础--SQL语句       一.创建和删除数据库: 1.创建数据库(默认化初始值) 格式: CREATE DATABASE 数据库名称 例如: CREATE DATABASE ...

  10. 使用 DartPad 制作代码实践教程

    DartPad 是一个开源的.在浏览器中体验和运行 Dart 编程语言的线上编辑器,目标是为了帮助开发者更好地了解 Dart 编程语言以及 Flutter 应用开发. DartPad 项目起始于 20 ...