-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Zhengjiaao
committed
Mar 4, 2022
1 parent
4520df9
commit 05d7835
Showing
21 changed files
with
806 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
# starter-aop | ||
|
||
- [spring-framework AOP 官方](https://docs.spring.io/spring-framework/docs/current/reference/html/core.html#aop) | ||
- [@Before、@Around和@After等advice 执行顺序](https://www.cnblogs.com/lidj/p/7194193.html) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.zja</groupId> | ||
<artifactId>spring-boot-starter-test-root</artifactId> | ||
<version>2.0-SNAPSHOT</version> | ||
</parent> | ||
|
||
<groupId>com.zja</groupId> | ||
<artifactId>starter-aop</artifactId> | ||
<packaging>jar</packaging> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-aop</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-test</artifactId> | ||
<scope>test</scope> | ||
</dependency> | ||
<dependency> | ||
<groupId>io.springfox</groupId> | ||
<artifactId>springfox-boot-starter</artifactId> | ||
<version>3.0.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.alibaba</groupId> | ||
<artifactId>fastjson</artifactId> | ||
<version>1.2.79</version> | ||
<scope>compile</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* @Company: 上海数慧系统技术有限公司 | ||
* @Department: 数据中心 | ||
* @Author: 郑家骜[ào] | ||
* @Email: [email protected] | ||
* @Date: 2022-03-04 11:14 | ||
* @Since: | ||
*/ | ||
package com.zja; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
/** | ||
* http://localhost:8080/swagger-ui/index.html#/ | ||
*/ | ||
@SpringBootApplication | ||
public class AopApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(AopApplication.class, args); | ||
} | ||
|
||
} |
75 changes: 75 additions & 0 deletions
75
starter-aop/src/main/java/com/zja/aop/CommonPointcuts.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/** | ||
* @Company: 上海数慧系统技术有限公司 | ||
* @Department: 数据中心 | ||
* @Author: 郑家骜[ào] | ||
* @Email: [email protected] | ||
* @Date: 2022-03-04 15:10 | ||
* @Since: | ||
*/ | ||
package com.zja.aop; | ||
|
||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Pointcut; | ||
|
||
/** | ||
* 常见切入点 | ||
* | ||
* 示例:针对 com.zja.controller 包路下所有控制器的方法添加aop | ||
* @Pointcut("execution (* com.zja.controller. * . * ( ..))") | ||
* 第一个 * 是返回任意类型 | ||
* 第二个 * 是任意类 | ||
* 第三个 * 是任意方法 | ||
*/ | ||
@Aspect | ||
//@Component | ||
public class CommonPointcuts { | ||
|
||
/** | ||
* A join point is in the web layer if the method is defined | ||
* in a type in the com.zja.controller package or any sub-package | ||
* under that. | ||
*/ | ||
@Pointcut("within(com.zja.controller..*)") | ||
public void inWebLayer() {} | ||
|
||
/** | ||
* A join point is in the service layer if the method is defined | ||
* in a type in the com.zja.service package or any sub-package | ||
* under that. | ||
*/ | ||
@Pointcut("within(com.zja.service..*)") | ||
public void inServiceLayer() {} | ||
|
||
/** | ||
* A join point is in the data access layer if the method is defined | ||
* in a type in the com.zja.dao package or any sub-package | ||
* under that. | ||
*/ | ||
@Pointcut("within(com.zja.dao..*)") | ||
public void inDataAccessLayer() {} | ||
|
||
/** | ||
* A business service is the execution of any method defined on a service | ||
* interface. This definition assumes that interfaces are placed in the | ||
* "service" package, and that implementation types are in sub-packages. | ||
* | ||
* If you group service interfaces by functional area (for example, | ||
* in packages com.zja.abc.service and com.zja.def.service) then | ||
* the pointcut expression "execution(* com.zja..service.*.*(..))" | ||
* could be used instead. | ||
* | ||
* Alternatively, you can write the expression using the 'bean' | ||
* PCD, like so "bean(*Service)". (This assumes that you have | ||
* named your Spring service beans in a consistent fashion.) | ||
*/ | ||
@Pointcut("execution(* com.zja..service.*.*(..))") | ||
public void businessService() {} | ||
|
||
/** | ||
* A data access operation is the execution of any method defined on a | ||
* dao interface. This definition assumes that interfaces are placed in the | ||
* "dao" package, and that implementation types are in sub-packages. | ||
*/ | ||
@Pointcut("execution(* com.zja.dao.*.*(..))") | ||
public void dataAccessOperation() {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* @Company: 上海数慧系统技术有限公司 | ||
* @Department: 数据中心 | ||
* @Author: 郑家骜[ào] | ||
* @Email: [email protected] | ||
* @Date: 2022-03-04 15:29 | ||
* @Since: | ||
*/ | ||
package com.zja.aop; | ||
|
||
import org.aspectj.lang.annotation.After; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.aspectj.lang.annotation.Before; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Aspect | ||
@Component | ||
public class ServiceAspect { | ||
|
||
/** | ||
* 前置通知(@Before):在目标方法调用之前调用通知 | ||
*/ | ||
@Before("CommonPointcuts.inServiceLayer()") | ||
public void beforeAdvice() { | ||
System.out.println("[ServiceAspect] beforeAdvice..."); | ||
} | ||
|
||
/** | ||
* 后置通知(@After):在目标方法完成之后调用通知 | ||
* 注意:final增强,不管是抛出异常或者正常退出都会执行 | ||
*/ | ||
@After("CommonPointcuts.inServiceLayer()") | ||
public void afterAdvice() { | ||
System.out.println("[ServiceAspect] afterAdvice..."); | ||
} | ||
|
||
|
||
} |
101 changes: 101 additions & 0 deletions
101
starter-aop/src/main/java/com/zja/aop/TestAopControllerAspect1.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* @Company: 上海数慧系统技术有限公司 | ||
* @Department: 数据中心 | ||
* @Author: 郑家骜[ào] | ||
* @Email: [email protected] | ||
* @Date: 2022-03-04 11:21 | ||
* @Since: | ||
*/ | ||
package com.zja.aop; | ||
|
||
|
||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.*; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* 通知有五种类型,分别是: | ||
* 前置通知(@Before):在目标方法调用之前调用通知() | ||
* 后置通知(@After):在目标方法完成之后调用通知 | ||
* 环绕通知(@Around):在被通知的方法调用之前和调用之后执行自定义的方法 | ||
* 返回通知(@AfterReturning):在目标方法成功执行之后调用通知 | ||
* 异常通知(@AfterThrowing):在目标方法抛出异常之后调用通知 | ||
* | ||
* 示例:针对 com.zja.controller 包路下所有控制器的方法添加aop | ||
* @Pointcut("execution (* com.zja.controller. * . * ( ..))") | ||
* 第一个 * 是返回任意类型 | ||
* 第二个 * 是任意类 | ||
* 第三个 * 是任意方法 | ||
*/ | ||
@Aspect | ||
@Component | ||
public class TestAopControllerAspect1 { | ||
|
||
/** | ||
* 针对特定控制器,AopController 的特定方法 | ||
*/ | ||
@Pointcut(value = "execution (* com.zja.controller.TestAopController.*(..))") | ||
public void pointCut() { | ||
} | ||
|
||
//前置通知(@Before):在目标方法调用之前调用通知 | ||
|
||
/** | ||
* 前置通知(@Before):在目标方法调用之前调用通知 | ||
*/ | ||
@Before("pointCut()") | ||
public void beforeAdvice() { | ||
System.out.println("[Aspect1] beforeAdvice..."); | ||
} | ||
|
||
/** | ||
* 后置通知(@After):在目标方法完成之后调用通知 | ||
* 注意:final增强,不管是抛出异常或者正常退出都会执行 | ||
*/ | ||
@After("pointCut()") | ||
public void afterAdvice() { | ||
System.out.println("[Aspect1] afterAdvice..."); | ||
} | ||
|
||
/** | ||
* 环绕通知(@Around):在被通知的方法调用之前和调用之后执行自定义的方法 | ||
* 注意:环绕增强,相当于MethodInterceptor | ||
* 环绕通知=前置+目标方法执行+后置通知,proceed方法就是用于启动目标方法执行的 | ||
* | ||
* @param proceedingJoinPoint getArgs():返回方法参数.getThis():返回代理对象.getTarget():返回目标对象.getSignature():返回所建议方法的描述.toString():打印所建议方法的有用描述 | ||
*/ | ||
@Around("pointCut()") | ||
public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) { | ||
System.out.println("[Aspect1] aroundAdvice before"); | ||
try { | ||
proceedingJoinPoint.proceed(); | ||
} catch (Throwable t) { | ||
System.out.println("[Aspect1] aroundAdvice exception"); | ||
t.printStackTrace(); | ||
} | ||
System.out.println("[Aspect1] aroundAdvice after"); | ||
} | ||
|
||
/** | ||
* 返回通知(@AfterReturning):在目标方法成功执行之后调用通知 | ||
* | ||
* @param joinPoint getArgs():返回方法参数.getThis():返回代理对象.getTarget():返回目标对象.getSignature():返回所建议方法的描述.toString():打印所建议方法的有用描述 | ||
*/ | ||
@AfterReturning("pointCut()") | ||
public void afterReturning(JoinPoint joinPoint) { | ||
System.out.println("[Aspect1] afterReturning advise"); | ||
} | ||
|
||
/** | ||
* 异常通知(@AfterThrowing):在目标方法抛出异常之后调用通知 | ||
* | ||
* @param joinPoint | ||
*/ | ||
@AfterThrowing("pointCut()") | ||
public void afterThrowing(JoinPoint joinPoint) { | ||
System.out.println("[Aspect1] afterThrowing advise"); | ||
} | ||
|
||
|
||
} |
101 changes: 101 additions & 0 deletions
101
starter-aop/src/main/java/com/zja/aop/TestAopControllerAspect2.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
/** | ||
* @Company: 上海数慧系统技术有限公司 | ||
* @Department: 数据中心 | ||
* @Author: 郑家骜[ào] | ||
* @Email: [email protected] | ||
* @Date: 2022-03-04 11:21 | ||
* @Since: | ||
*/ | ||
package com.zja.aop; | ||
|
||
|
||
import org.aspectj.lang.JoinPoint; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.*; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* 通知有五种类型,分别是: | ||
* 前置通知(@Before):在目标方法调用之前调用通知() | ||
* 后置通知(@After):在目标方法完成之后调用通知 | ||
* 环绕通知(@Around):在被通知的方法调用之前和调用之后执行自定义的方法 | ||
* 返回通知(@AfterReturning):在目标方法成功执行之后调用通知 | ||
* 异常通知(@AfterThrowing):在目标方法抛出异常之后调用通知 | ||
* | ||
* 示例:针对 com.zja.controller 包路下所有控制器的方法添加aop | ||
* @Pointcut("execution (* com.zja.controller.*.*(..))") | ||
* 第一个 * 是返回任意类型 | ||
* 第二个 * 是任意类 | ||
* 第三个 * 是任意方法 | ||
*/ | ||
@Aspect | ||
//@Component | ||
public class TestAopControllerAspect2 { | ||
|
||
/** | ||
* 针对特定控制器,AopController 的特定方法 | ||
*/ | ||
@Pointcut(value = "execution (* com.zja.controller.TestAopController.*(..))") | ||
public void pointCut() { | ||
} | ||
|
||
//前置通知(@Before):在目标方法调用之前调用通知 | ||
|
||
/** | ||
* 前置通知(@Before):在目标方法调用之前调用通知 | ||
*/ | ||
@Before("pointCut()") | ||
public void beforeAdvice() { | ||
System.out.println("[Aspect2] beforeAdvice..."); | ||
} | ||
|
||
/** | ||
* 后置通知(@After):在目标方法完成之后调用通知 | ||
* 注意:final增强,不管是抛出异常或者正常退出都会执行 | ||
*/ | ||
@After("pointCut()") | ||
public void afterAdvice() { | ||
System.out.println("[Aspect2] afterAdvice..."); | ||
} | ||
|
||
/** | ||
* 环绕通知(@Around):在被通知的方法调用之前和调用之后执行自定义的方法 | ||
* 注意:环绕增强,相当于MethodInterceptor | ||
* 环绕通知=前置+目标方法执行+后置通知,proceed方法就是用于启动目标方法执行的 | ||
* | ||
* @param proceedingJoinPoint getArgs():返回方法参数.getThis():返回代理对象.getTarget():返回目标对象.getSignature():返回所建议方法的描述.toString():打印所建议方法的有用描述 | ||
*/ | ||
@Around("pointCut()") | ||
public void aroundAdvice(ProceedingJoinPoint proceedingJoinPoint) { | ||
System.out.println("[Aspect2] aroundAdvice before"); | ||
try { | ||
proceedingJoinPoint.proceed(); | ||
} catch (Throwable t) { | ||
System.out.println("[Aspect2] aroundAdvice exception"); | ||
t.printStackTrace(); | ||
} | ||
System.out.println("[Aspect2] aroundAdvice after"); | ||
} | ||
|
||
/** | ||
* 返回通知(@AfterReturning):在目标方法成功执行之后调用通知 | ||
* | ||
* @param joinPoint getArgs():返回方法参数.getThis():返回代理对象.getTarget():返回目标对象.getSignature():返回所建议方法的描述.toString():打印所建议方法的有用描述 | ||
*/ | ||
@AfterReturning("pointCut()") | ||
public void afterReturning(JoinPoint joinPoint) { | ||
System.out.println("[Aspect2] afterReturning advise"); | ||
} | ||
|
||
/** | ||
* 异常通知(@AfterThrowing):在目标方法抛出异常之后调用通知 | ||
* | ||
* @param joinPoint | ||
*/ | ||
@AfterThrowing("pointCut()") | ||
public void afterThrowing(JoinPoint joinPoint) { | ||
System.out.println("[Aspect2] afterThrowing advise"); | ||
} | ||
|
||
|
||
} |
Oops, something went wrong.