Spring学习之AOP的实现方式
Spring学习之AOP的三种实现方式
一、介绍AOP
在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期间动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
需要了解一下名词:
- 横切关注点:跨越应用程序多个模块的方法或功能。即是,与我们业务逻辑无关的,但是我们需要关注的部分,就是横切关注点。如日志 , 安全 , 缓存 , 事务等等 ....
- 切面(ASPECT):横切关注点 被模块化 的特殊对象。即,它是一个类。
- 通知(Advice):切面必须要完成的工作。即,它是类中的一个方法。
- 目标(Target):被通知对象。
- 代理(Proxy):向目标对象应用通知之后创建的对象。
- 切入点(PointCut):切面通知 执行的 “地点”的定义。
- 连接点(JointPoint):与切入点匹配的执行点。
简单来说就是不改变原来代码的情况下增加新功能的方式。
二、三种实现方式
设定通过在具体的业务实现方法前后增加输出日志的功能来实现AOP的功能
首先需要导入依赖包
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
</dependencies>
第一种通过 Spring API 实现
首先编写业务接口和实现类
public interface UserService {
void add();
void delete();
void update();
void select();
}
public class UserServiceImpl implements UserService {
@Override
public void add() {
System.out.println("增加了一个用户!");
}
@Override
public void delete() {
System.out.println("删除了一个用户!");
}
@Override
public void update() {
System.out.println("更新了一个用户!");
}
@Override
public void select() {
System.out.println("查询了一个用户!");
}
}
编写两个打印日志的实现类
一个是实现API的 MethodBeforeAdvice 方法,实现执行方法之前的日志输出
import org.springframework.aop.MethodBeforeAdvice;
import java.lang.reflect.Method;
public class BeforeLog implements MethodBeforeAdvice {
// method:要执行目标对象的方法
// args:参数
// target:目标对象
@Override
public void before(Method method, Object[] args, Object target) throws Throwable {
System.out.println(target.getClass().getName()+"的"+method.getName()+"被执行了");
}
}
另一个是API的 AfterReturningAdvice 方法,实现执行方法之后的日志输出
import org.springframework.aop.AfterReturningAdvice;
import java.lang.reflect.Method;
public class AfterLog implements AfterReturningAdvice {
// returnValue:返回值
@Override
public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
System.out.println("执行了"+method.getName()+"的方法,返回结果为:"+returnValue);
}
}
然后去applicationContext.xml配置文件中注册bean以及配置切入方式
<!--方式一:使用spring原生API接口-->
<!--注册bean-->
<bean id="userService" class="com.tioxy.service.UserServiceImpl"/>
<bean id="afterLog" class="com.tioxy.log.AfterLog"/>
<bean id="beforeLog" class="com.tioxy.log.BeforeLog"/>
<!--配置AOP-->
<aop:config>
<!--切入点:就是在哪里执行方法
expression:表达式
execution(要执行的位置)
-->
<aop:pointcut id="pointcut" expression="execution(* com.tioxy.service.UserServiceImpl.*(..))"/>
<!--执行环绕增强-->
<aop:advisor advice-ref="afterLog" pointcut-ref="pointcut"/>
<aop:advisor advice-ref="beforeLog" pointcut-ref="pointcut"/>
</aop:config>
最后进行测试
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}
第二种自定义类来实现Aop
业务接口和实现类还是 UserService 和 UserServiceImpl
编写自定义实现类
public class DiyPointCut {
public void before(){
System.out.println("=============方法执行前============");
}
public void after(){
System.out.println("=============方法执行后============");
}
}
然后去applicationContext.xml配置文件中注册bean以及配置切入方式
<!--第二种方式自定义实现-->
<!--注册bean-->
<bean id="diy" class="com.kuang.config.DiyPointcut"/>
<!--aop的配置-->
<aop:config>
<!--第二种方式:使用AOP的标签实现-->
<aop:aspect ref="diy">
<aop:pointcut id="diyPonitcut" expression="execution(* com.kuang.service.UserServiceImpl.*(..))"/>
<aop:before pointcut-ref="diyPonitcut" method="before"/>
<aop:after pointcut-ref="diyPonitcut" method="after"/>
</aop:aspect>
</aop:config>
然后进行测试
public class Mytest {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
UserService userService = context.getBean("userService", UserService.class);
userService.add();
}
}
第三种使用注解方式实现Aop
编写一个注解方式的实现类
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AnnotationPointcut {
@Before("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void before(){
System.out.println("---------方法执行前---------");
}
@After("execution(* com.kuang.service.UserServiceImpl.*(..))")
public void after(){
System.out.println("---------方法执行后---------");
}
}
然后去applicationContext.xml配置文件中注册bean以及配置切入方式
<!--方式三:使用注解-->
<bean id="annotationPointCut" class="com.tioxy.diy.AnnotationPointCut"/>
<!--开启注解支持-->
<aop:aspectj-autoproxy/>
最后进行测试
以上就是AOP的三种实现方式
文章引用
狂神说Java:https://www.bilibili.com/video/BV1WE411d7Dv?p=20
Spring学习之AOP的实现方式的更多相关文章
- spring 学习之 bean 的注入方式 property和constructor-arg的使用方式
spring 学习之 bean 的注入方式 property和constructor-arg的使用方式. bean的注入方式: property 注入是: 通过setxx方法注入. construct ...
- Spring学习之AOP总结帖
AOP(面向方面编程),也可称为面向切面编程,是一种编程范式,提供从另一个角度来考虑程序结构从而完善面向对象编程(OOP). 在进行 OOP 开发时,都是基于对组件(比如类)进行开发,然后对组件进行组 ...
- spring学习(二) ———— AOP之AspectJ框架的使用
前面讲解了spring的特性之一,IOC(控制反转),因为有了IOC,所以我们都不需要自己new对象了,想要什么,spring就给什么.而今天要学习spring的第二个重点,AOP.一篇讲解不完,所以 ...
- spring学习(六)注解方式实现AOP
一.导包(导入maven的依赖) <?xml version="1.0" encoding="UTF-8"?> <project xmlns= ...
- Spring学习之AOP
Spring-AOP(Aspect-orented programming) 在业务流程中插入与业务无关的逻辑,这样的逻辑称为Cross-cutting concerns,将Crossing-cutt ...
- Spring学习之Aop的各种增强方法
AspectJ允许使用注解用于定义切面.切入点和增强处理,而Spring框架则可以识别并根据这些注解来生成AOP代理.Spring只是使用了和AspectJ 5一样的注解,但并没有使用AspectJ的 ...
- Spring学习之Aop的基本概念
转自:http://my.oschina.net/itblog/blog/209067 AOP的基本概念 AOP从运行的角度考虑程序的流程,提取业务处理过程的切面.AOP面向的是程序运行中的各个步骤, ...
- Spring学习之AOP与事务
一.概述 在软件业,AOP为Aspect Oriented Programming的缩写,意为:面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续, ...
- spring学习笔记-AOP
1.aop:aspect oriented programming 面向切面编程 2.aop在spring中的作用: 提供声明式服务(声明式事务) 允许用户实现自定义切面 3.aop:在不改变原有 ...
随机推荐
- Typography convention
1 h1 Chapter title centered,number three in bold,used ##. 1.1 h2 The chapter is a section, and the s ...
- Flutter vs React Native vs Native:深度性能比较
老孟导读:这是老孟翻译的付费文章,文章所有权归原作者所有. 欢迎加入老孟Flutter交流群,每周翻译2-3篇付费文章,精彩不容错过. 原文地址:https://medium.com/swlh/flu ...
- npm tip: go to the package's home page
exec the following order: --- npm home <package name>
- vscode 配置 c++ 环境
vscode 配置 c++ 环境 参考的这篇bloghttps://blog.csdn.net/bat67/article/details/81268581 1.安装编译器.这里安装 codebloc ...
- 转载--gulp入门
关于gulp的入门文章,先转载了 http://markpop.github.io/2014/09/17/Gulp%E5%85%A5%E9%97%A8%E6%95%99%E7%A8%8B/
- window的常用操作
一.window.location location对象属性 1.location.href 属性返回当前页面的 URL. 2.location.pathname 返回路径和方法名称 3.locati ...
- 师兄大厂面试遇到这条 SQL 数据分析题,差点含泪而归!
写在前面:我是「云祁」,一枚热爱技术.会写诗的大数据开发猿.昵称来源于王安石诗中一句 [ 云之祁祁,或雨于渊 ] ,甚是喜欢. 写博客一方面是对自己学习的一点点总结及记录,另一方面则是希望能够帮助更多 ...
- 开发者必备——API设计问题
本文主要探讨RPC和RESTFul两种API风格的特点以及在开发中应该如何进行技术选型,同时截取了网上社区,文章一部分关于API设计的想法和观点供读者参考,取舍. 1,背景简述 API学名:应用程序接 ...
- day73 bbs项目☞基本功能实现
目录 一.登录功能 二.首页搭建 三.admin后台管理 四.图片防盗链 五.个人站点展示 一.登录功能 views.py 0难度,都是基本操作,要熟悉auth模块的使用 # 登录功能 def log ...
- 【Hack.lu-2017】FlatScience
信息: 题目来源:Hack.lu-2017 标签:SQL注入.源码泄露 解题过程 题目页面有多层,存在许多pdf文件,首先进行目录扫描: [TIME] => 2020-07-07 16:08:5 ...