AOP(Aspect Orient Programming),面向切面编程,是对面向对象编程OOP的一种补充

面向对象编程使用静态角度考虑程序的结构,而面向切面编程是从动态角度考虑程序运行过程

AOP底层,就是采用动态代理模式实现的。采用了两种代理:JDK的动态代理域CGLIB的动态代理

AOP编程属于:

1.切面(Aspect)

切面泛指交叉业务逻辑

2.织入(weaving)

织入是指将切面代码插入到目标对象的过程

3.切入点(Pointcut)

切入点指切面具体织入的位置

4.目标对象(Target)

目标对象指将要被增强的对象

5.通知(Advice)

通知是切面的一种具体实现,可以完成简单织入功能

6.顾问(Advisor)

顾问是切面的另一种实现,能够将通知以更为复杂的方式织入到目标对象中,是将通知包装为更复制切面的装配器

Spring的AOP编程环境搭建

1.创建Maven项目,配置spring的AOP需要的jar包

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  2. <modelVersion>4.0.0</modelVersion>
  3. <groupId>SrpingTest</groupId>
  4. <artifactId>SrpingTest</artifactId>
  5. <version>0.0.1-SNAPSHOT</version>
  6. <build>
  7. <sourceDirectory>src</sourceDirectory>
  8. <plugins>
  9. <plugin>
  10. <artifactId>maven-compiler-plugin</artifactId>
  11. <version>3.6.1</version>
  12. <configuration>
  13. <source>1.6</source>
  14. <target>1.6</target>
  15. </configuration>
  16. </plugin>
  17. </plugins>
  18. </build>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework</groupId>
  22. <artifactId>spring-core</artifactId>
  23. <version>4.3.8.RELEASE</version>
  24. </dependency>
  25. <dependency>
  26. <groupId>org.springframework</groupId>
  27. <artifactId>spring-context</artifactId>
  28. <version>4.3.2.RELEASE</version>
  29. </dependency>
  30. <dependency>
  31. <groupId>org.aspectj</groupId>
  32. <artifactId>aspectjrt</artifactId>
  33. <version>1.8.2</version>
  34. </dependency>
  35. <dependency>
  36. <groupId>org.aspectj</groupId>
  37. <artifactId>aspectjweaver</artifactId>
  38. <version>1.8.2</version>
  39. </dependency>
  40. <dependency>
  41. <groupId>org.springframework</groupId>
  42. <artifactId>spring-tx</artifactId>
  43. <version>4.3.2.RELEASE</version>
  44. </dependency>
  45. </dependencies>
  46. </project>

2.创建接口UserService.java

  1. package com.agent.service;
  2.  
  3. public interface UserService {
  4.  
  5. void addUser(String name, String password);
  6.  
  7. }

3.创建实现类UserServiceImpl.java

  1. package com.agent.service.impl;
  2.  
  3. import org.springframework.stereotype.Service;
  4.  
  5. import com.agent.service.UserService;
  6.  
  7. @Service(value="userService")
  8. public class UserServiceImpl implements UserService {
  9.  
  10. @Override
  11. public void addUser(String name, String password) {
  12. System.out.println("UserServiceImpl.addUser()...... name: " + name + "; password: " + password);
  13. }
  14.  
  15. }

4.创建AOP的实现,用来记录日志,在方法执行时,记录下方法名和参数的日志工具,LogUtil.java

  1. package com.agent.aop;
  2.  
  3. import org.aspectj.lang.JoinPoint;
  4.  
  5. public class LogUtil {
  6.  
  7. public void logWrited(JoinPoint point) {
  8.  
  9. StringBuffer sb = new StringBuffer();
  10. sb.append("记录日志--> 执行方法: " + point.getSignature().getName() + "; 传入参数: [");
  11. // sb.append("记录日志--> 执行方法: " + point.getSignature().getDeclaringType() + "; 传入参数: [");
  12. // sb.append("记录日志--> 执行方法: " + point.getSignature().getDeclaringTypeName() + "; 传入参数: [");
  13.  
  14. Object[] objectArray = point.getArgs();
  15. for(int i=0; i<objectArray.length; i++) {
  16. if(objectArray[i] instanceof String) {
  17. sb.append(String.valueOf(objectArray[i])).append(", ");
  18. }
  19. }
  20.  
  21. sb.append("]");
  22. System.out.println(sb.toString());
  23. }
  24.  
  25. }

5.配置spring的配置文件ApplicationContext.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xmlns:context="http://www.springframework.org/schema/context"
  6. xmlns:aop="http://www.springframework.org/schema/aop"
  7. xmlns:tx="http://www.springframework.org/schema/tx"
  8. xsi:schemaLocation="http://www.springframework.org/schema/beans
  9. http://www.springframework.org/schema/beans/spring-beans.xsd
  10. http://www.springframework.org/schema/context
  11. http://www.springframework.org/schema/context/spring-context.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop.xsd
  14. http://www.springframework.org/schema/tx
  15. http://www.springframework.org/schema/tx/spring-tx.xsd">
  16.  
  17. <context:component-scan base-package="com.agent" />
  18.  
  19. <bean id="aspect" class="com.agent.aop.LogUtil" />
  20. <aop:config>
  21. <aop:aspect ref="aspect">
  22. <aop:pointcut expression="execution(* add*(..))" id="mypointcut"/>
  23. <aop:after method="logWrited" pointcut-ref="mypointcut"/>
  24. </aop:aspect>
  25. </aop:config>
  26. </beans>

6.创建测试方法AOPTest.java

  1. package com.agent.test;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. import com.agent.service.UserService;
  7.  
  8. public class AOPTest {
  9.  
  10. public static void main(String[] args) {
  11.  
  12. ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
  13. UserService us = (UserService)ac.getBean("userService");
  14. us.addUser("张三", "188");
  15. }
  16.  
  17. }

7.执行测试方法查看结果:

spring学习笔记四:AOP的更多相关文章

  1. Spring学习笔记之aop动态代理(3)

    Spring学习笔记之aop动态代理(3) 1.0 静态代理模式的缺点: 1.在该系统中有多少的dao就的写多少的proxy,麻烦 2.如果目标接口有方法的改动,则proxy也需要改动. Person ...

  2. Spring学习笔记4——AOP

    AOP 即 Aspect Oriented Program 面向切面编程 首先,在面向切面编程的思想里面,把功能分为核心业务功能,和周边功能. 所谓的核心业务,比如登陆,增加数据,删除数据都叫核心业务 ...

  3. Spring学习笔记四 整合SSH

    三大框架架构(整合原理) 步骤1:导包 Hibernate包 1.Hibernate包,hibernate/lib/required 2.hibernate/lib/jpa | java persis ...

  4. Spring学习笔记四:SpringAOP的使用

    转载请注明原文地址:http://www.cnblogs.com/ygj0930/p/6776247.html  一:AOP基础概念 (1)通知(增强)Advice 通知,其实就是我们从众多类中提取出 ...

  5. [Spring学习笔记 4 ] AOP 概念原理以及java动态代理

    一.Spring IoC容器补充(1) Spring IoC容器,DI(依赖注入): 注入的方式:设值方法注入setter(属性注入)/构造子注入(构造函数传入依赖的对象)/字段注入Field(注解) ...

  6. Spring学习笔记2—AOP

    1.AOP概念 AOP(Aspect Oriented Programming):面向切面编程,AOP能够将那些与业务无关,却为业务模块所共同调用的应用(例如事务处理.日志管理.权限控制等)封装起来, ...

  7. Spring学习笔记之AOP配置篇(一)

    [TOC] 1. 创建并声明一个切面 首先,创建一个类,添加@Component注解使其添加到IoC容器 然后,添加@Aspect注解,使其成为一个切面 最后,在配置文件里面,使用<aop:as ...

  8. spring学习笔记四:spring常用注解总结

    使用spring的注解,需要在配置文件中配置组件扫描器,用于在指定的包中扫描注解 <context:component-scan base-package="xxx.xxx.xxx.x ...

  9. spring学习笔记(一) Spring概述

    博主Spring学习笔记整理大部分内容来自Spring实战(第四版)这本书.  强烈建议新手购入或者需要电子书的留言. 在学习Spring之前,我们要了解这么几个问题:什么是Spring?Spring ...

随机推荐

  1. Java 知识点(一)

    博主对 Java知识点的整理基于 c语言,整理内容为 Java的重点及与 c语言的差异点或编程通要知识点.水平有限,欢迎指正.(参考书籍<Java 核心技术 卷Ⅰ>) Java 的类名:名 ...

  2. 题解【[FJOI2018]所罗门王的宝藏】

    本题解同步于luogu emmm切了近年省选题来写题解啦qwq 该题较其他省选题较水吧(否则我再怎么做的出来 思路是图论做法,做法上楼上大佬已经讲的很清楚了,我来谈谈代码实现上的一些细节 \[\tex ...

  3. OpenMP笔记(三)

    个人博客地址:http://www.bearoom.xyz/2019/02/21/openmp3/ 这一部分主要记录一些指令的使用. 一.parallel的使用 parallel是用于构造并行块的,也 ...

  4. springboot+mybatis+通用mapper+多数据源(转载)

    1.数据库准备 数据库表我们在springboot-mybatis数据之外,新建数据库springboot-mybatis2: springboot-mybatis数据库中有t_class表: spr ...

  5. 图论模型--dijstra算法和floyd算法

    matlab代码实现:https://blog.csdn.net/weixin_40108753/article/details/81237585 python代码实现:

  6. iOS 直接使用16进制颜色

    在做iOS开发时,一般我们会吸色,就是产品给的图我们一般会吸色,但是最近吸色时候,老大说有较大的颜色偏差,所以要求我们直接使用UI给出的额16进制颜色,你也可以搜索<RGB颜色值转换成十六进制颜 ...

  7. RSAUtils加密解密

    import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils; import javax.cr ...

  8. vue中过滤器filter

    Vue.js 允许我们自定义过滤器,可被用作一些常见的文本格式化.过滤器可以用在两个地方:mustache 插值表达式. v-bind表达式.过滤器应该被添加在 JavaScript 表达式的尾部,由 ...

  9. C/C++预处理指令#include,#define,#undef,#if,#ifdef,#ifndef,#elif,#endif,#error......

    本文主要记录了C/C++预处理指令,常见的预处理指令如下: #空指令,无任何效果 #include包含一个源代码文件 #define定义宏 #undef取消已定义的宏 #if如果给定条件为真,则编译下 ...

  10. elasticsearch ik中文分词器的使用详解

    (基于es5.4)先喵几眼github,按照步骤安装好分词器 link:https://github.com/medcl/elasticsearch-analysis-ik 复习一下常用的操作 .查看 ...