提交 ec92d71d 编写于 作者: 项羽过江东's avatar 项羽过江东

aop 代码编译成功,添加了一个 demo 程序

上级 f06b35e2
...@@ -19,6 +19,7 @@ public class AopApplication { ...@@ -19,6 +19,7 @@ public class AopApplication {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(HelloConfigOfAOP.class); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(HelloConfigOfAOP.class);
MathCalculator mathCalculator = ctx.getBean(MathCalculator.class); MathCalculator mathCalculator = ctx.getBean(MathCalculator.class);
int divide = mathCalculator.divide(4, 1); int divide = mathCalculator.divide(4, 1);
log.error("ee", new RuntimeException("e"));
log.info("end: " + divide); log.info("end: " + divide);
} }
} }
package com.matrix.nickel.aop; package com.matrix.nickel.aop;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.*; import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.SourceLocation;
import org.springframework.util.StringUtils;
/** /**
* 切面类 * 切面类
...@@ -22,23 +26,34 @@ public class LogAspect { ...@@ -22,23 +26,34 @@ public class LogAspect {
// 前置通知,方法执行之前切入 // 前置通知,方法执行之前切入
@Before("execution(public int com.matrix.nickel.aop.MathCalculator.divide(int, int))") @Before("execution(public int com.matrix.nickel.aop.MathCalculator.divide(int, int))")
public void beforeInvoke(){ public void beforeInvoke(JoinPoint joinPoint){
log.info("before invoke...参数列表:"); Object[] args = joinPoint.getArgs();
Signature signature = joinPoint.getSignature();
// 代理的目标类
Object target = joinPoint.getTarget();
// 封装后的代理类
Object aThis = joinPoint.getThis();
JoinPoint.StaticPart staticPart = joinPoint.getStaticPart();
SourceLocation sourceLocation = joinPoint.getSourceLocation();
String longString = joinPoint.toLongString();
String shortString = joinPoint.toShortString();
log.info("before invoke...参数列表:" + StringUtils.arrayToCommaDelimitedString(args));
} }
// 后置通知,方法执行之后切入,不管正常还是异常返回,都会执行 // 后置通知,方法执行之后切入,不管正常还是异常返回,都会执行
@After("com.matrix.nickel.aop.LogAspect.pointCut()") @After("com.matrix.nickel.aop.LogAspect.pointCut()")
public void afterInvoke(){ public void afterInvoke(JoinPoint joinPoint){
log.info("after invoke..."); log.info("after invoke...");
} }
// 正常返回通知 // 正常返回通知
@AfterReturning("pointCut()") @AfterReturning(pointcut = "pointCut()", returning = "result")
public void returnInvoke(){ public void returnInvoke(JoinPoint joinPoint, Object result){
log.info("method return...返回值:"); log.info("method return...返回值:" + result);
} }
@AfterThrowing("pointCut()") @AfterThrowing(pointcut = "pointCut()", throwing = "ex")
public void throwInvoke(){ public void throwInvoke(JoinPoint jp, Exception ex){
log.info("method return...发生异常"); log.info("method return...发生异常");
} }
} }
...@@ -2,7 +2,11 @@ ...@@ -2,7 +2,11 @@
<Configuration status="WARN"> <Configuration status="WARN">
<Appenders> <Appenders>
<Console name="Console" target="SYSTEM_OUT"> <Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
<!--<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>-->
<PatternLayout
pattern="%style{%d{HH:mm:ss}}{bright,#ffffff} %highlight{%-5level} [%style{%-20.-20t}{bright,blue}] %style{%-80.80l}{bright,yellow}: %msg%n%style{%throwable}{red}"
disableAnsi="false" noConsoleNoAnsi="false"/>
</Console> </Console>
</Appenders> </Appenders>
<Loggers> <Loggers>
......
导入到 idea 之后,spring 源码项目有两种构建运行方式![](../images/idea中的两种编译.png),intellij idea 和 gradle,建议使用 idea 的方式,这样更快 导入到 idea 之后,spring 源码项目有两种构建运行方式![](../images/idea中的两种编译.png),intellij idea 和 gradle,建议使用 idea 的方式,这样更快
File --> Settings --> Build, Execution, Deployment --> Build Tools --> Gradle 可以选择编译方式
选择 gradle 时,Build Project (Ctrl + F9) 会使用 gradle 编译 选择 intellij 时,Build Project (Ctrl + F9) 会使用自带的编译器编译
~~简单说就是:如果想使用 intellij idea 编译,就把 build.gradle 中的版本写成 1.5 如果想使用 gradle 编译,就把 build.gradle 中的版本写成 1.3~~ 该版本没有这个问题。
如果是 gradle 构建运行,则 spring aop, spring aspectj 这两个模块是可以编译成功的,gradle 自带了 AspectJ 的编译器;
如果是 idea 构建运行,则需要配置 java compiler 编译器为 Ajc,安装 aspectj1.9.20
如果想要使用 aop 功能,需要配置 ![](../images/配置facets开启AspectJ的编译.png),最好安装插件 AspectJ
此差异由.gitattributes 抑制。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册