目录

1 配置文件

2 自定义切面日志类

3 bean

4 dao

5 daoImpl

6 service

7 serviceImpl

8 Main


系列文章:

1. Spring实例参考01-一个简单实例

2. spring实例参考02-一个有基本框架雏形的实例

3. Spring实例参考03-通过构造方法创建对象

4. Spring实例参考04-通过工厂创建对象

5. Spring实例参考05-导入其他文件:import

6. Spring实例参考06-setter注入的10种方式

7. Spring实例参考07-bean的作用域

8. Spring实例参考08-bean的自动装配

9. Spring实例参考09-静态代理

10. Spring实例参考10-动态代理

11. Spring实例参考11-API实现AOP前置/后置通知

12. Spring实例参考12-自定义类实现AOP

13. Spring实例参考13-注解实现AOP


本文供以下文章参考使用:

Spring基础回顾__evenif的博客-CSDN博客

1 配置文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- 头文件中引入了aop命名空间 -->
<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.xsdhttp://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd"><!-- 自动创建单例 --><bean id="indexDao" class="com.evenif.dao.impl.IndexMysqlDaoImpl"/><bean id="aspectLog" class="com.evenif.log.AspectLog"/><!-- 自动创建单例服务依赖注入已存在dao --><bean id="indexService" class="com.evenif.service.impl.IndexServiceImpl"><property name="indexDao" ref="indexDao"></property></bean><!-- 配置aop --><aop:config><!-- 定义“切点”包含    所有返回类型     该包下的所有类               所有参数类型(包含无参)的方法↓           ↓                 ↓                       ↓execution(  *       com.evenif.service.impl.*.*      (..)                     )--><aop:pointcut id="pointcut" expression="execution(* com.evenif.service.impl.*.*(..))"/><!-- 指定通知类型pointcut和通知者类aspectLog --><aop:advisor advice-ref="aspectLog" pointcut-ref="pointcut"/></aop:config>
</beans>

2 自定义切面日志类

package com.evenif.log;import org.springframework.aop.AfterAdvice;
import org.springframework.aop.AfterReturningAdvice;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.aop.ThrowsAdvice;
import org.springframework.aop.aspectj.AspectJAroundAdvice;import java.lang.reflect.Method;
import java.util.Arrays;/** MethodBeforeAdvice: 前置通知接口* AfterReturningAdvice:后置通知接口* 这两个接口互相独立,如果只需要其中一个,只需要实现一个就行,也可以配置多个类分别实现这些接口,* 笔者为了方便,直接使用一个日志类同时实现了这两个接口而已。*/
public class AspectLog implements MethodBeforeAdvice, AfterReturningAdvice, ThrowsAdvice {/*** @param method 目标方法* @param args 方法参数* @param target 目标对象* @throws Throwable*/@Overridepublic void before(Method method, Object[] args, Object target) throws Throwable {System.out.println(target.getClass().getName() + "." + method.getName() + ": " + Arrays.toString(args) + "[BEFORE]");}/*** @param returnValue 返回值* @param method* @param args* @param target* @throws Throwable*/@Overridepublic void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {System.out.println(returnValue +" "+target.getClass().getName() + "." + method.getName() + ": " + Arrays.toString(args) + "[AFTER]");}public void afterThrowing(Method method,Exception ex) throws Throwable{System.err.println(method.getName()+":"+ex);}
}

3 bean

package com.evenif.bean;public class Index {private int id;private String name;public Index() {
//        System.out.println("create Index class");}public Index(int id, String name) {this.id = id;this.name = name;
//        System.out.println("create Index class with params");}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}@Overridepublic String toString() {return "Index{" +"id=" + id +", name='" + name + '\'' +'}';}
}

4 dao

package com.evenif.dao;
import com.evenif.bean.Index;import java.util.List;public interface IndexDao {Index getById(int id);void insert(Index index);void update(Index index);void delete(int id);List<Index> loadAll();
}

5 daoImpl

package com.evenif.dao.impl;import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;import java.util.ArrayList;
import java.util.List;public class IndexMysqlDaoImpl implements IndexDao {@Overridepublic Index getById(int id) {// 假装是从数据库获取的Index对象return new Index(id, "mysqlIndex");}@Overridepublic void insert(Index index) {System.out.println("mysql insert : " + index);}@Overridepublic void update(Index index) {System.out.println("mysql update : " + index);}@Overridepublic void delete(int id) {System.out.println("mysql delete index : " + id);}/*** @return*/@Overridepublic List<Index> loadAll() {List<Index> list = new ArrayList<>();for (int i = 0; i < 3; i++) {list.add(new Index(i + 1, "mysql-" + i));}return list;}
}

6 service

package com.evenif.service;import com.evenif.bean.Index;import java.util.List;public interface IndexService {Index getIndex(int id);void addIndex(Index index);void modifyIndex(Index index);void deleteIndex(int id);List<Index> loadIndexList();
}

7 serviceImpl

package com.evenif.service.impl;import com.evenif.bean.Index;
import com.evenif.dao.IndexDao;
import com.evenif.service.IndexService;import java.util.List;public class IndexServiceImpl implements IndexService {private IndexDao indexDao;public void setIndexDao(IndexDao indexDao) {this.indexDao = indexDao;}public IndexServiceImpl() {}public IndexServiceImpl(IndexDao indexDao) {this.indexDao = indexDao;}@Overridepublic Index getIndex(int id) {return indexDao.getById(id);}@Overridepublic void addIndex(Index index) {indexDao.insert(index);}@Overridepublic void modifyIndex(Index index) {indexDao.update(index);}@Overridepublic void deleteIndex(int id) {indexDao.delete(id);}@Overridepublic List<Index> loadIndexList() {return indexDao.loadAll();}
}

8 Main

package com.evenif;import com.evenif.bean.Index;
import com.evenif.bean.Person;
import com.evenif.bean.User;
import com.evenif.service.IndexService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");IndexService service = (IndexService) context.getBean("indexService");System.out.println(service.getIndex(1));Index index = new Index(2,"text02");service.addIndex(index);service.deleteIndex(index.getId());service.modifyIndex(index);service.loadIndexList();}
}
//执行结果:
//        com.evenif.service.impl.IndexServiceImpl.getIndex: [1][BEFORE]
//        Index{id=1, name='mysqlIndex'} com.evenif.service.impl.IndexServiceImpl.getIndex: [1][AFTER]
//        Index{id=1, name='mysqlIndex'}
//        com.evenif.service.impl.IndexServiceImpl.addIndex: [Index{id=2, name='text02'}][BEFORE]
//        mysql insert : Index{id=2, name='text02'}
//        null com.evenif.service.impl.IndexServiceImpl.addIndex: [Index{id=2, name='text02'}][AFTER]
//        com.evenif.service.impl.IndexServiceImpl.deleteIndex: [2][BEFORE]
//        mysql delete index : 2
//        null com.evenif.service.impl.IndexServiceImpl.deleteIndex: [2][AFTER]
//        com.evenif.service.impl.IndexServiceImpl.modifyIndex: [Index{id=2, name='text02'}][BEFORE]
//        mysql update : Index{id=2, name='text02'}
//        null com.evenif.service.impl.IndexServiceImpl.modifyIndex: [Index{id=2, name='text02'}][AFTER]
//        com.evenif.service.impl.IndexServiceImpl.loadIndexList: [][BEFORE]
//        [Index{id=1, name='mysql-0'}, Index{id=2, name='mysql-1'}, Index{id=3, name='mysql-2'}] com.evenif.service.impl.IndexServiceImpl.loadIndexList: [][AFTER]

Spring实例参考11-API实现AOP前置/后置通知相关推荐

  1. AOP日志-后置通知

    创建切面类处理日志 @Component @Aspect public class LogAop {@Autowiredprivate HttpServletRequest request;@Auto ...

  2. Spring AOP中的前置通知和后置通知详解

    不同版本的spring对AOP的支持有所不同,spring2.0之前,它主要针对不同类型的拦截器使用XML配置文件通过代理来实现.而spring2.0之后,它可以使用JDK5的注解来完成AOP的实现, ...

  3. spring之aop(前置通知,后置通知,环绕通知,过滤通知,异常通知)

    1.AOP中关键性概念  连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出 目标(Target):被通知(被代理)的对象 注1:完成具体的业务逻辑 通知(Advice ...

  4. spring之AOP(面向切面编程)和五大通知(前置通知、后置通知、异常通知、环绕通知、过滤通知)

    一.aop的介绍 1.AOP中关键性概念 : 连接点(Joinpoint):程序执行过程中明确的点,如方法的调用,或者异常的抛出. 目标(Target):被通知(被代理)的对象 注1:完成具体的业务逻 ...

  5. Spring Boot AOP面向切面编程使用(定义切入点、前置通知、后置通知、返回通知、异常通知、环绕通知)

    1 AOP AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软件开发 ...

  6. AOP开发——在不修改源代码的前提下,对类里面的方法进行增强 : 前置 后置 环绕 异常||如何得到目标方法的参数和返回值

    AOP开发 @Transactionl 在不修改源代码的前提下,对类里面的方法进行增强 前置 后置 环绕 异常 创建项目前引入aop的包 <dependency>     <grou ...

  7. 手写Spring-第六章-让我访问!实现前置后置处理扩展功能

    前言 上一次我们实现了bean定义的自动化注册,这让我们的demo看起来已经很有Spring的那个味道了.但是扩展性还是差了一些.我们都知道,要写出高内聚,低耦合,富有扩展性的代码.那么如何增加扩展性 ...

  8. 【Android RTMP】NV21 图像旋转处理 ( 快速搭建 RTMP 服务器 Shell 脚本 | 创建 RTMP 服务器镜像 | 浏览器观看直播 | 前置 / 后置摄像头图像旋转效果展示 )

    文章目录 安卓直播推流专栏博客总结 一. 编写快速搭建 RTMP 服务器 Shell 脚本 二. RTMP 快速搭建方法 三.创建阿里云 RTMP 服务器镜像 四.浏览器查看直播内容 五.前置 / 后 ...

  9. 配置切入点表达式|| 前置通知、后置通知、异常通知、最终通知、环绕通知

    环绕通知 pom.xml <?xml version="1.0" encoding="UTF-8"?> <project xmlns=&quo ...

  10. 实验10:创建带有生命周期方法的bean ||实验11:测试bean的后置处理器

    实验10:创建带有生命周期方法的bean 实验11:测试bean的后置处理器 MyBeanPostProcessor.java package com.atguigu.bean;import org. ...

最新文章

  1. linux设置ulimit值永久生效
  2. Silverlight 和WPF的Composite Guidance(Prism V2)发布了
  3. 测验8.2 指针与字符串 6-1 函数实现字符串逆序
  4. 不得不看的cookie和session
  5. Linux学习笔记---烧写bin文件分析
  6. 使用 LwIP TCP/IP 栈,在 STM32Cube 上开发应用
  7. 微信小程序企业号注册
  8. java英语流利_day186-2018-12-23-英语流利阅读-待学习
  9. python爬取去哪网数据_用户观点:企查查数据爬取技术与Python 爬取企查查数据...
  10. ZOJ 3755 - Mines (状压DP)
  11. 林轩田《机器学习基石》作业一-Python实现
  12. GPT-3+DALL-E 2 = 海量带标签数据自动生成 ?
  13. 【海外APP】Twitch 全球首屈一指的游戏直播平台
  14. JAVA设计模式--结构型模式--代理模式
  15. PHP函数记录-trim导致的编码异常
  16. 软件开发可行性分析九个流程的理解
  17. 64位操作系统安装——Linux(Ubuntu 16.04)+Windows7+iNode
  18. 【CSS】线性渐变、径向渐变
  19. Java毕设项目自行车在线租赁管理系统2021(java+VUE+Mybatis+Maven+Mysql)
  20. vb语言中怎样编码窗体中所有字体加粗_VB窗口属性中文对照表

热门文章

  1. 介绍如何用Python语言实现一个最基本的CNN模型: Implementing Convolutional Neural Network on CIFAR10 Dataset
  2. 大众点评前650家咖啡店的数据分析(三)分析篇
  3. 904.水果成篮 - 采用unordered_map进行统计查重!
  4. opencv学习笔记(八)彩色视频转换成黑白视频
  5. 擦亮眼睛!这些期刊,一定要看准了再投!
  6. BJOI2017 喷式水战改
  7. 百度站长平台年中考核,答题获取站长特权,部分试题参考
  8. mysql怎样不区分_如何设置mysql的表不区分你大小写
  9. 山东大学软件工程应用与实践——ECommerceCrawlers代码分析(五)
  10. 深入解读应聘阿里系全流程注意事项,助力求职