AOP的原理和实例

简介: AOP的原理 对哪些对象在什么位置拦截做什么 哪些对象             dataSourceAspect 是切面要拦截什么。aop:before就是在拦截对象的前面位置。
AOP的原理
对哪些对象在什么位置拦截做什么 <=> <aop:before method="before" pointcut-ref="txPointcut" />
哪些对象
<!-- 定义切面,所有的service的所有方法 --> 
        <aop:pointcut id="txPointcut" expression="execution(* com.masterslave.service.*.*(..))" /> 
dataSourceAspect 是切面要拦截什么。aop:before就是在拦截对象的前面位置。method="before"就是使用切面中的方法处理拦截 
85 <aop:aspect ref="dataSourceAspect" order="-9999"> 
86 <aop:before method="before" pointcut-ref="txPointcut" /> 
87 </aop:aspect> 
如上是用配置文件配置的
还有方法是直接用注解方式
注解中也说明了,拦截范围@Pointcut,在什么位置@Before,做什么@Aspect,具体拦截对象是谁JoinPoint
@Aspect
public class LoggingAspect {


// @Pointcut("execution(* com.samsung.sdsc.legal..*Impl.*(..)) || execution(* com.samsung.sdsc.legal..*Action.*(..))")
@Pointcut("execution(* com.samsung.sdsc.legal..*Impl.*(..))")
public void serviceMethod() {
}


@Before("serviceMethod()")
public void beforeLogging(JoinPoint thisJoinPoint) {
Class<? extends Object> clazz = thisJoinPoint.getTarget().getClass();
Logger logger = Logger.getLogger(clazz);
String methodName = thisJoinPoint.getSignature().getName();
Object[] arguments = thisJoinPoint.getArgs();


StringBuffer argBuf = new StringBuffer();
StringBuffer argValueBuf = new StringBuffer();
int i = 0;
for (Object argument : arguments) {
String argClassName = "";
if (null == argument) {
argClassName = "Null";
argument = "";
} else {
argClassName = argument.getClass().getSimpleName();
}


if (i > 0) {
argBuf.append(", ");
}
argBuf.append(argClassName + " arg" + ++i);
argValueBuf.append(".arg" + i + " : " + argument.toString() + "\n");


}


if (i == 0) {
argValueBuf.append("No arguments\n");
}


StringBuffer messageBuf = new StringBuffer();
messageBuf.append("before executing " + methodName + "("
+ argBuf.toString() + ") method");
messageBuf
.append("\n-------------------------------------------------------------------------------\n");
messageBuf.append(argValueBuf.toString());
messageBuf
.append("-------------------------------------------------------------------------------");
logger.info(messageBuf);


}
}


目录
相关文章
|
3月前
|
监控 Java Spring
Spring AOP的作用和底层原理、AOP相关术语
Spring AOP的作用和底层原理、AOP相关术语
35 0
|
3月前
|
数据库
AOP(面向切面编程)的基本概念和原理
AOP(面向切面编程)的基本概念和原理
76 0
|
6月前
|
Java Spring 容器
【Spring AOP底层实现原理】
【Spring AOP底层实现原理】
|
3月前
|
Java Spring
Spring 源码阅读 72:基于 CGLIB 的 AOP 代理的原理(2)- 拦截器的查找与执行
【1月更文挑战第7天】本文分析了基于 CGLIB 的 AOP 代理如何查找和执行拦截器链,其主要的逻辑在 DynamicAdvisedInterceptor 的intercept方法执行。
35 1
|
2月前
|
XML Java 数据格式
5个点轻松搞定Spring AOP底层实现原理
AOP 也是 Spring 中一个较为重要的内容,相对于传统的 OOP 模式,AOP 有很多让人难以理解的地方,本篇文章将向大家介绍 AOP 的实现方法及其底层实现,内容包括:
44 1
|
3月前
|
Java Spring
Spring 源码阅读 71:基于 CGLIB 的 AOP 代理的原理(1)- DynamicAdvisedInterceptor 分析
【1月更文挑战第6天】本文分析了基于 CGLIB 的 AOP 代理对象,是通过一个 DynamicAdvisedInterceptor 类型的 Callback 来完成 AOP 增强逻辑处理的,DynamicAdvisedInterceptor 通过实现 MethodInterceptor 接口的intercept方法来处理 AOP 增强逻辑。下一篇,将重点分析这个方法的原理。
53 7
|
2月前
|
XML Java 数据格式
浅谈基于动态代理的Spring AOP原理
浅谈基于动态代理的Spring AOP原理
25 0
|
2月前
|
缓存 Java uml
SpringBoot2 | Spring AOP 原理深度源码分析(八)
SpringBoot2 | Spring AOP 原理深度源码分析(八)
45 0
|
3月前
|
Java Spring
Spring AOP之MethodInterceptor原理
Spring AOP之MethodInterceptor原理
72 0
|
3月前
|
设计模式 监控 Java
【Spring】Spring AOP原理
【Spring】Spring AOP原理