1. 环境搭建

1.1 导入 jar 包

  • Spring 框架的基本开发包(6个);
  • Spring 的传统AOP的开发包
    • spring-aop-4.3.10.RELEASE
    • org.aopalliance-1.10.0 (在 Spring 依赖包中)
  • aspectJ 的开发包
    • org.aspectj.weave-1.6.8.RELEASE.jar (在 Spring 依赖包中)
    • spring-aspects-4.3.10.RELEASE.jar

1.2 编写 applicationContext.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:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd"><!-- 开启自动代理 --><aop:aspectj-autoproxy/><!-- 配置目标对象 customerDao --><bean id="customerDao" class="com.itheima.demo.CustomerDaoImpl"/><!-- 配置切面类 --><bean id="myAspectAnno" class="com.itheima.demo.MyAspectAnno"/></beans>

1.3 创建包结构,编写具体的接口和实现类

  • com.itheima.demo

    • CustomerDao: 接口;
    • CustomerDaoImpl: 实现类;
  • 在 applicationContext.xml 中配置
// CustomerDao.javapublic interface CustomerDao{public void save();}// CustomerDaoImpl.javapublic class CustomerDaoImpl{public void save(){System.out.println("保存客户...");}}// 编写测试方法@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext.xml")public class Demo{@Resource(name="customerDao")private CustomerDao customerDao;@Testpublic void fun(){customerDao.save();}
}// 编写切面类@Aspectpublic class MyAspectAnno{// 通知(增前方法)// @Before(切入点表达式)@Before(value="execution(public void com.itheima.demo.CustomerDaoImpl.save())")public void log(){System.out.println("记录日志...");}}

1.4 在 applicationContext.xml 中开启自动代理

<aop:aspectj-autoproxy/>

1.5 通知类型

  • @Before: 前置通知;
  • @AfterReturning: 后置通知;
  • @Around: 环绕通知;
  • @After: 最终通知;
  • @AfterThrowing: 异常抛出通知;

1.6 配置通用的切入点

  • 使用 @Pointcut 配置通用的切入点
// 示例
@Aspect
public class MyAspectAnno{@Before(value="MyAspectAnno.fn()")public void log(){System.out.println("记录日志...");}@After(value="MyAspectAnno.fn()")public void after(){System.out.println("最终通知...");}// 自定义切入点@Pointcut(value="execution(public void com.itheima.demo.CustomerDaoImpl.save())")public void fn(){}
}

参考资料

  • Spring 入门视频

转载于:https://www.cnblogs.com/linkworld/p/7721857.html

Spring 框架的AOP之注解的方式相关推荐

  1. 详解Spring框架的AOP机制

    AOP是Spring框架面向切面的编程思想,AOP采用一种称为"横切"的技术,将涉及多业务流程的通用功能抽取并单独封装,形成独立的切面,在合适的时机将这些切面横向切入到业务流程指定 ...

  2. Spring 框架之 AOP 原理深度剖析!|CSDN 博文精选

    作者 | GitChat 责编 | 郭芮 出品 | CSDN 博客 AOP(Aspect Oriented Programming)面向切面编程是 Spring 框架最核心的组件之一,它通过对程序结构 ...

  3. spring框架xml的几种配置方式

    spring框架xml的几种配置方式 ioc配置一般由一下两大类 1 手动装配 利用xml手动配置,分为:setter方法装配 ,构造器装配 2 自动装配 利用注解自动装配 准备工作:userdao ...

  4. java 自动装载_java_详解Java的Spring框架下bean的自动装载方式,Spring容器可以自动装配相互协 - phpStudy...

    详解Java的Spring框架下bean的自动装载方式 Spring容器可以自动装配相互协作bean之间的关系,这有助于减少对XML配置,而无需编写一个大的基于Spring应用程序的较多的和元素. 自 ...

  5. 深入学习Spring框架(二)- 注解配置

    1.为什么要学习Spring的注解配置? 基于注解配置的方式也已经逐渐代替xml.所以我们必须要掌握使用注解的方式配置Spring. 关于实际的开发中到底使用xml还是注解,每家公司有着不同的使用习惯 ...

  6. spring框架复习--aop,事务

    AOP概述 1.AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术. 2. 利用AOP可以对业务逻 ...

  7. Spring框架之AOP

    AOP概念: 1 .什么是 AOP (1)面向切面编程(方面),利用 AOP 可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率. (2 ...

  8. spring框架aop_使用Spring框架和AOP进行动态路由

    spring框架aop 本文的总体思路是展示业务交易如何动态触发业务事件以进行子系统处理. 本文显示的示例有效地使用了Spring框架2.0和Spring AOP来将业务服务与子系统处理功能分离. 现 ...

  9. java 路由框架_使用Spring框架和AOP实现动态路由

    本文的大体思路是展示了一次业务交易如何动态地为子系统处理过程触发业务事件.本文所示的例子使用Spring框架和Spring AOP有效地解耦业务服务和子系统处理功能.现在让我们仔细看看业务需求. 业务 ...

最新文章

  1. 线程间的通信 共享数据安全问题
  2. 高档名片设计:12款专业的名片设计欣赏
  3. 去掉字符串左右的空格
  4. 通过OWA修改密码,提示您输入的密码不符合最低安全要求
  5. in 用不用索引_MySQL 索引最佳实践之问题反馈
  6. Android studio3.0打开Device File Explore(文件管理器)的方法(图文教程)
  7. 的函数原型_JS基础函数、对象和原型、原型链的关系
  8. c++ primer 笔记 (三)
  9. 王道考研 计算机网络19 传输层 传输层的寻址与端口 TCP UDP
  10. ps里面怎么插入流程图_流程图很难画?学会这3个方法,5分钟能绘制出好看又高级的流程图...
  11. MySQL安装图解 地址
  12. RNN、LSTM、GRU
  13. 42.翻转单词顺序 VS 左旋转字符串(C++版本)
  14. 慕课网仿去哪儿项目笔记--(四)-城市页面的优化
  15. Qt+MySQL实现数据库图书管理系统
  16. PAT-Basic Level-1001 害死人不偿命的(3n+1)猜想;
  17. Java 日期的各种操作
  18. PostgreSQL插入大量数据:pg_testgen插件
  19. Google是如何提供“无缝”的街景全景图的?
  20. [Paper]Cardiologist-Level Arrhythmia Detection with Convolutional Neural Networks

热门文章

  1. EEGLAB处理脑电视频教程 part1-3
  2. seaborn系列 (2) | 散点图scatterplot()
  3. Hololens Spatial Mapping功能实现
  4. Android Bluetooth模块学习笔记
  5. 英特尔用ViT做密集预测效果超越卷积,性能提高28%,mIoU直达SOTA|在线可玩
  6. 2.05亿美元!马斯克Neuralink获脑机接口领域史上最大融资,公司正急招软件工程师...
  7. 特斯拉的三季度:车卖很多、车主很烦、股价很魔幻
  8. 明晚直播丨上海名师王召强:高考大变脸后,中小学语文怎么学?
  9. 马化腾首谈腾讯开源时,鹅厂已在Github上放出82个项目,标星24万+ | 附AI项目链接...
  10. 谷歌自动重建了完整果蝇大脑神经图:40万亿像素,可在线交互,用了数千块TPU...