文章目录

  • 1. 业务需求
  • 2. AOP实现
    • 2.1 使用AOP注意事项
    • 2.2 实现方法一(Spring接口实现)
    • 2.3 实现方法二(自定义类实现)
    • 2.4 实现方式三(注解实现)

1. 业务需求

现在有一个类,类中有一些方法,现在需要在执行每个方法前执行另一个方法,输出要执行的方法名称

  1. UserService
public interface UserService {void add();void delete();void update();void select();
}
  1. UserServiceImpl
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 select() {System.out.println("查询了一个用户");}
}

2. AOP实现

2.1 使用AOP注意事项

  1. 必须引入Maven依赖
<dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.4</version>
</dependency>
  1. 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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><!--将要使用的类注册使Spring可以管理--><bean id="userService" class="com.hust.service.UserServiceImpl"/><bean id="log" class="com.hust.log.Log"/><!--配置AOP--><aop:config><!--切入点,需要执行方法的位置,execution里面的内容为要执行的位置--><aop:pointcut id="pointcut" expression="execution(* com.hust.service.UserServiceImpl.*(..))"/><!--执行环绕--><aop:advisor advice-ref="log" pointcut-ref="pointcut"/></aop:config>
</beans>

2.2 实现方法一(Spring接口实现)

使用Spring自带的API接口实现

  • 写一个Log类,去实现MethodBeforeAdvice接口,使用里面的方法去实现
public class Log implements MethodBeforeAdvice {/*method:要执行的目标对象的方法args:对象参数target:目标对象* */public void before(Method method, Object[] args, Object target) throws Throwable {System.out.println(target.getClass().getName() + "类的" + method.getName() + "方法被执行了");}
}
  • 测试
public class MyTest {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");UserService userService = context.getBean("userService", UserService.class);userService.add();}
}
/*
com.hust.service.UserServiceImpl类的add方法被执行了
增加了一个用户
*/

2.3 实现方法二(自定义类实现)

自定义切入点类

  • 自定义切入点类
public class DiyPointcut {public void before(){System.out.println("=====方法执行前=====");}public void after(){System.out.println("=====方法执行后=====");}
}
  • 将类注入Spring管理
<?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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="userService" class="com.hust.service.UserServiceImpl"/><bean id="diy" class="com.hust.diy.DiyPointcut"/><aop:config><!--自定义切面,ref为要引用的类--><aop:aspect ref="diy"><!--切入点--><aop:pointcut id="point" expression="execution(* com.hust.service.UserServiceImpl.*(..))"/><!--通知--><aop:before method="before" pointcut-ref="point"/><aop:after method="after" pointcut-ref="point"/></aop:aspect></aop:config>
</beans>

2.4 实现方式三(注解实现)

  • 实现切面的类
//标注这个类是一个切面
@Aspect
public class AnnotationPointcut {@Before("execution(* com.hust.service.UserServiceImpl.*(..))")public void before(){System.out.println("-----方法执行前-----");}@After("execution(* com.hust.service.UserServiceImpl.*(..))")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/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><bean id="userService" class="com.hust.service.UserServiceImpl"/><bean id="anno" class="com.hust.diy.AnnotationPointcut"/><aop:aspectj-autoproxy/>
</beans>

Spring--AOP实现的方法相关推荐

  1. Spring AOP根据JdbcTemplate方法名动态设置数据源

    2019独角兽企业重金招聘Python工程师标准>>> 说明:现在的场景是,采用数据库(Mysql)复制(binlog)的方式在两台不同服务器部署并配置主从(Master-Slave ...

  2. spring AOP对父类方法加强分析

    spring AOP可以对方法进行加强,就是在方法前执行一些想要的事情,执行方法后想执行一些信息,原理就是利用动态代理,具体不在阐述 今天要讨论的是一个springBean继承了父类,在父类里进行了方 ...

  3. 基于 Annotation 拦截的 Spring AOP 权限验证方法

    余 清, 软件工程师, IBM 简介: 使用 Annotation 可以非常方便的根据用户的不同角色,分配访问 Java 方法的权限.在 Java Web 开发中,使用这种方法,可以提高系统的松耦合度 ...

  4. spring AOP实现——xml方法

    上一文中 讲了Annotation如何配置AOP,地址如下:http://5148737.blog.51cto.com/5138737/1428048 使用同样的bean,用xml来实现一下: Hel ...

  5. spring aop 拦截业务方法,实现权限控制

    难点:aop类是普通的java类,session是无法注入的,那么在有状态的系统中如何获取用户相关信息呢,session是必经之路啊,获取session就变的很重要.思索很久没有办法,后来在网上看到了 ...

  6. spring aop不执行_使用Spring AOP重试方法执行

    spring aop不执行 我的一位博客关注者发送了一封电子邮件,要求我显示" Spring AOP的RealWorld用法"示例. 他提到,在大多数示例中,都演示了Spring ...

  7. 使用Spring AOP重试方法执行

    我的一位博客关注者发送了一封电子邮件,要求我显示" Spring AOP的RealWorld用法"示例. 他提到,在大多数示例中,都演示了Spring AOP在日志记录方法进入/退 ...

  8. Spring AOP方法分析

    Spring AOP方法分析 此示例显示如何配置Spring AOP方法概要分析.我们可以在任何服务(或其他)类中使用Spring AOP和任何方法,而无需在任何服务类中编写任何一行分析代码.面向方面 ...

  9. 一个Spring AOP的坑!很多人都犯过!

    点击上方蓝色"方志朋",选择"设为星标" 回复"666"获取独家整理的学习资料! 很多读者看完之后表示用起来很爽,但是后台也有人留言说自己配 ...

  10. 比较Spring AOP与AspectJ

    本文翻译自博客Comparing Spring AOP and AspectJ 介绍 如今有多个可用的AOP库,这些组件需要回答一系列的问题: 是否与我现有的应用兼容? 我在哪实现AOP? 集成到我的 ...

最新文章

  1. CV领域论文常用单词汇总
  2. SAP PM概念学习
  3. html代码 通用代码,新浪博客通用html代码及其使用方法
  4. 2018ICPC南京赛区网络选拔B The writing on the wall (单调栈)
  5. 「洛谷P2397」 yyy loves Maths VI (mode) 解题报告
  6. HDU2501 Tiling_easy version【递推+打表】
  7. 注册表实现欢迎界面的修改
  8. linux下main函数的返回值问题
  9. 【二维码识别】基于matlab GUI 灰度+二值化+校正QR二维码识别与生成【含Matlab源码 600期】
  10. 从键盘录入10个整数,统计有多少个奇数,Java基础轻松实现
  11. lnmp mysql 哪个好_[LNMP]Mysql生产环境配置
  12. 动手学数理统计(2)
  13. java jshell_java9系列(一)安装及jshell使用
  14. 刘晓燕,考研核心词汇1500,1-20讲【前十讲】【谐音记忆】【没人比我详细】【超级原创】
  15. mantis apache mysql_apache+php+mysql搭建mantis
  16. c语言中八进制输出的格式说明符,C 的输入输出格式说明符讲解
  17. MVC模式和DDD模式对比,谁才是银弹?
  18. OPC Server
  19. adb怎么连接到android模拟器,4 adb连接到手机/模拟器
  20. 2021-08-031179 最大的最大公约数

热门文章

  1. 【统计学】【2018】【含源码】苹果公司股票价格的时间序列预测
  2. Eigen的学习(十)几何学应用
  3. Java人民币转大写工具类
  4. 博君一笑,熊猫烧香恶搞
  5. 新闻发布|基于JavaWeb实现新闻发布管理系统+论文+PPT
  6. 解决常用浏览器上传与下载文件中问乱码问题
  7. 打造朋友圈,自动驾驶公司如何走向产业化?
  8. Git clone 与 Git Fork 的不同(Difference between Git Clone and Git Fork)
  9. java的性能,网上说 Java 的性能,已经达到甚至超过 C++,是真的吗?
  10. 带货直播平台搭建功能模块详细介绍