了解AOP以及实现方式
AOP是什么?
面向切面编程,把那些与业务无关,却为业务模块所共同调用的逻辑封装成一个可重的模块,即切面
使用"横切"技术,AOP把软件系统分为两个部分:核心关注点和横切关注点。业务处理的主要流程是核心关注点,与之关系不大的部分是横切关注点
为什么使用AOP?
如果说面向对象编程是关注将需求功能划分为不同的并且相对独立,封装良好的类,并让它们有着属于自己的行为,依靠继承和多态等来定义彼此的关系的话;那么面向切面编程则是希望能够将通用需求功能从不相关的类当中分离出来,能够使得很多类共享一个行为,一旦发生变化,不必修改很多类,而只需要修改这个行为即可。
如下,有两个方法分别为mul和div,这两个方法的内容代码大多数是重复的,则我们可以抽离一个新的方法
package com.zzj.calculatar.service;
import org.springframework.stereotype.Service; @Service
public class CalculatorService implements ICalculatorService{ @Override
public int mul(int a, int b) {
System.out.println("The mul method begins.");
System.out.println("The mul method args:["+a+","+b+"]");
return a*b;
} @Override
public int div(int a, int b) {
System.out.println("The div method begins.");
System.out.println("The div method args:["+a+","+b+"]");
return a/b;
} }
新的方法如下:
@Service
public class CalculatorService implements ICalculatorService{ @Override
public int mul(int a, int b) {
t("mul", a, b);
return a*b;
} @Override
public int div(int a, int b) {
t("div", a, b);
return a/b;
} private void t(String methodName,int a,int b){
System.out.println("The"+methodName+"method begins.");
System.out.println("The"+methodName+"method args:["+a+","+b+"]");
} }
AOP的切面化就体现在此,具体的AOP实现方式有:注解和XML配置
package com.zzj.calculatar.service;
import org.springframework.stereotype.Service; @Service
public class CalculatorService implements ICalculatorService{ @Override
public int mul(int a, int b) {
return a*b;
} @Override
public int div(int a, int b) {
return a/b;
} }
注解方式实现AOP:
package com.zzj.aspect; import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component; @Order(1)//切片执行顺序,默认为字典顺序
@Aspect
@Component
public class CalculatorAspect { //提取表达式
@Pointcut("execution(public int com.zzj.calculatar.service.CalculatorService.*(..))")
public void pointCut(){ } @Around(value="pointCut()")
public Object around(ProceedingJoinPoint joinPoint){
Object result = null;
Object target = joinPoint.getTarget();//目标对象
String methodName = joinPoint.getSignature().getName();
Object[] params = joinPoint.getArgs(); try{
try{
//前置增强
System.out.println(target.getClass().getName()+": The "+methodName+" method begins.");
System.out.println(target.getClass().getName()+": Parameters of the "+methodName+"method: ["+params[0]+","+params[1]+"]");
//执行目标对象内的方法
result = joinPoint.proceed();
}finally{
//后置增强
System.out.println(target.getClass().getName()+":The "+methodName+" method ends.");
}
//返回增强
System.out.println(target.getClass().getName()+":Result of the "+methodName+" method:"+result);
}catch (Throwable e) {
System.out.println(target.getClass().getName()+":Exception of the method "+methodName+": "+e);
}
return result;
} }
<!--XML代码-->
<context:component-scan base-package="com.zzj"></context:component-scan>
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>
XML配置方式实现AOP(不需要注解,将注解放在XML中):
package com.zzj.aspect;
import org.aspectj.lang.ProceedingJoinPoint;
public class CalculatorAspect {
public Object around(ProceedingJoinPoint joinPoint){
Object result = null;
Object target = joinPoint.getTarget();//目标对象
String methodName = joinPoint.getSignature().getName();
Object[] params = joinPoint.getArgs(); try{
try{
//前置增强
System.out.println(target.getClass().getName()+": The "+methodName+" method begins.");
System.out.println(target.getClass().getName()+": Parameters of the "+methodName+"method: ["+params[0]+","+params[1]+"]");
//执行目标对象内的方法
result = joinPoint.proceed();
}finally{
//后置增强
System.out.println(target.getClass().getName()+":The "+methodName+" method ends.");
}
//返回增强
System.out.println(target.getClass().getName()+":Result of the "+methodName+" method:"+result);
}catch (Throwable e) {
System.out.println(target.getClass().getName()+":Exception of the method "+methodName+": "+e);
}
return result;
}
}
<!--扫描-->
<context:component-scan base-package="com.zzj"></context:component-scan>
<!--获取aspect类-->
<bean id="calculatorAspect" class="com.zzj.aspect.CalculatorAspect"></bean> <aop:config>
<!--提取表达式-->
<aop:pointcut expression="execution(public int com.zzj.calculatar.service.CalculatorService.*(..))" id="pointCut"/>
<!--环绕增强-->
<aop:aspect ref="calculatorAspect" order="1">
<aop:around method="around" pointcut-ref="pointCut"/>
</aop:aspect>
</aop:config>
测试类:
package com.zzj.test;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.zzj.calculatar.service.ICalculatorService; public class Test { public static void main(String[] args){ ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("application.xml");
ICalculatorService calculatorService = applicationContext.getBean(ICalculatorService.class);
System.out.println(calculatorService.getClass());//当有代理类时,获取的代理对象
System.out.println(calculatorService.div(1,1));
applicationContext.close(); } }
测试结果:

了解AOP以及实现方式的更多相关文章
- spring aop 使用注解方式总结
spring aop的注解方式:和xml的配置方式略有区别,详细如下: 1.首先还是建立需要的切面类:切面类里面定义好切点配置,以及所有的需要实现的通知方法. /** * */ package com ...
- springboot aop 自定义注解方式实现完善日志记录(完整源码)
版权声明:本文为博主原创文章,欢迎转载,转载请注明作者.原文超链接 一:功能简介 本文主要记录如何使用aop切面的方式来实现日志记录功能. 主要记录的信息有: 操作人,方法名,参数,运行时间,操作类型 ...
- springboot aop 自定义注解方式实现一套完善的日志记录(完整源码)
https://www.cnblogs.com/wenjunwei/p/9639909.html https://blog.csdn.net/tyrant_800/article/details/78 ...
- Spring AOP 不同配置方式产生的冲突问题
Spring AOP的原理是 JDK 动态代理和CGLIB字节码增强技术,前者需要被代理类实现相应接口,也只有接口中的方法可以被JDK动态代理技术所处理:后者实际上是生成一个子类,来覆盖被代理类,那么 ...
- 5.3 Spring5源码--Spring AOP使用接口方式实现
Spring 提供了很多的实现AOP的方式:Spring 接口方式,schema配置方式和注解. 本文重点介绍Spring使用接口方式实现AOP. 使用接口方式实现AOP以了解为目的. 更好地理解动态 ...
- Spring学习总结(三)——Spring实现AOP的多种方式
AOP(Aspect Oriented Programming)面向切面编程,通过预编译方式和运行期动态代理实现程序功能的横向多模块统一控制的一种技术.AOP是OOP的补充,是Spring框架中的一个 ...
- spring aop 使用xml方式的简单总结
spring aop的 xml的配置方式的简单实现: 1.编写自己的切面类:配置各个通知类型 /** * */ package com.lilin.maven.service.aop; import ...
- ssm+redis整合(通过aop自定义注解方式)
此方案借助aop自定义注解来创建redis缓存机制. 1.创建自定义注解类 package com.tp.soft.common.util; import java.lang.annotation.E ...
- 浅谈spring中AOP以及spring中AOP的注解方式
AOP(Aspect Oriented Programming):AOP的专业术语是"面向切面编程" 什么是面向切面编程,我的理解就是:在不修改源代码的情况下增强功能.好了,下面在 ...
- spring AOP自定义注解方式实现日志管理
今天继续实现AOP,到这里我个人认为是最灵活,可扩展的方式了,就拿日志管理来说,用Spring AOP 自定义注解形式实现日志管理.废话不多说,直接开始!!! 关于配置我还是的再说一遍. 在appli ...
随机推荐
- ANSYS 瞬态热分析--样件加热
目录 1. 要求 2. ANSYS有限元分析 2.1 APDL建模 1. 要求 一块0.8m*0.4m*0.2m厚的钢板,在300℃的炉子中进行加热,其材料参数如下表所示: 材料参数 序号 名称 参数 ...
- ConcurrentHashMap 结构 1.7 与1.8
1.结构 1.7 segment+HashEntity+Unsafe 1.8 移除Segment,使锁的粒度更小,Synchronized+CAS+Node+Unsafe 2. put() 1.7 先 ...
- 使用 OClint 进行静态代码分析
OCLint 就是一个建立在 Clang 上的工具,能够发现代码中潜在的问题. 最近需要一个静态分析代码工具,帮助我们发布运行应用前找到代码潜在的问题. 其实对于iOS开发,我们的日常开发上已经用到了 ...
- Fleck WebSocket使用
Fleck WebSocket使用 作为笔记存储. 最近公司有这方面的使用需求.在网上查了一些资料后.得到了想要的结果.以下记录摘抄至网上资料. 1.首先,服务端.项目NuGet直接引用Fleck类库 ...
- for in 与for 与hasOwnProperty
在遍历一个对象的时候我们会使用到for in属性. 现有对象和数组如下: var filght = { number: 1, status: 'watit', arrival: [1,2,3], ad ...
- 【协作式原创】查漏补缺之Golang中mutex源码实现(预备知识)
预备知识 CAS机制 1. 是什么 参考附录3 CAS 是项乐观锁技术,当多个线程尝试使用 CAS 同时更新同一个变量时,只有其中一个线程能更新变量的值,而其它线程都失败,失败的线程并不会被挂起,而是 ...
- spriteKit简单学习
https://shimo.im/docs/YejitfPrGkcNO3Ls/ <SpriteKit学习相关> 关于SpriteKit https://www.jianshu.com/ ...
- MVC 拦截器
https://www.cnblogs.com/blosaa/archive/2011/06/02/2067632.html
- 变量的注释(python3.6以后的功能)
有时候导入模块,然后使用这个变量的时候,却没点出后面的智能提示.用以下方法可以解决:https://www.cnblogs.com/xieqiankun/p/type_hints_in_python3 ...
- color转成image对象
.h //颜色转换成图片 + (UIImage *)imageFromColor:(UIColor *)color; .m //颜色转换成图片 + (UIImage *)imageFromColor: ...