-
Notifications
You must be signed in to change notification settings - Fork 1
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
1 parent
933d90c
commit 2bcea73
Showing
5 changed files
with
182 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
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,32 @@ | ||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.github.weihubeats</groupId> | ||
<artifactId>spring-boot-nebula</artifactId> | ||
<version>${revision}</version> | ||
</parent> | ||
|
||
<artifactId>spring-boot-nebula-aop-base</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.springframework.boot</groupId> | ||
<artifactId>spring-boot-starter-web</artifactId> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.projectlombok</groupId> | ||
<artifactId>lombok</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |
115 changes: 115 additions & 0 deletions
115
...g-boot-nebula-aop-base/src/main/java/com/nebula/aop/base/NebulaBaseAnnotationAdvisor.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,115 @@ | ||
package com.nebula.aop.base; | ||
|
||
import lombok.NonNull; | ||
import org.aopalliance.aop.Advice; | ||
import org.aopalliance.intercept.MethodInterceptor; | ||
import org.springframework.aop.ClassFilter; | ||
import org.springframework.aop.MethodMatcher; | ||
import org.springframework.aop.Pointcut; | ||
import org.springframework.aop.support.AbstractPointcutAdvisor; | ||
import org.springframework.aop.support.AopUtils; | ||
import org.springframework.aop.support.ComposablePointcut; | ||
import org.springframework.aop.support.StaticMethodMatcher; | ||
import org.springframework.aop.support.annotation.AnnotationMatchingPointcut; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.BeanFactory; | ||
import org.springframework.beans.factory.BeanFactoryAware; | ||
import org.springframework.core.annotation.AnnotatedElementUtils; | ||
import org.springframework.util.Assert; | ||
|
||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Method; | ||
import java.lang.reflect.Proxy; | ||
|
||
/** | ||
* @author : wh | ||
* @date : 2024/3/12 17:02 | ||
* @description: | ||
*/ | ||
public class NebulaBaseAnnotationAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware { | ||
|
||
private final Advice advice; | ||
|
||
private final Pointcut pointcut; | ||
|
||
private final Class<? extends Annotation> annotation; | ||
|
||
public NebulaBaseAnnotationAdvisor(@NonNull MethodInterceptor advice, | ||
@NonNull Class<? extends Annotation> annotation) { | ||
this.advice = advice; | ||
this.annotation = annotation; | ||
this.pointcut = buildPointcut(); | ||
} | ||
|
||
@Override | ||
public Pointcut getPointcut() { | ||
return this.pointcut; | ||
} | ||
|
||
@Override | ||
public Advice getAdvice() { | ||
return this.advice; | ||
} | ||
|
||
@Override | ||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException { | ||
if (this.advice instanceof BeanFactoryAware) { | ||
((BeanFactoryAware) this.advice).setBeanFactory(beanFactory); | ||
} | ||
} | ||
|
||
private Pointcut buildPointcut() { | ||
Pointcut cpc = new AnnotationMatchingPointcut(annotation, true); | ||
Pointcut mpc = new AnnotationMethodPoint(annotation); | ||
return new ComposablePointcut(cpc).union(mpc); | ||
} | ||
|
||
/** | ||
* In order to be compatible with the spring lower than 5.0 | ||
*/ | ||
private static class AnnotationMethodPoint implements Pointcut { | ||
|
||
private final Class<? extends Annotation> annotationType; | ||
|
||
public AnnotationMethodPoint(Class<? extends Annotation> annotationType) { | ||
Assert.notNull(annotationType, "Annotation type must not be null"); | ||
this.annotationType = annotationType; | ||
} | ||
|
||
@Override | ||
public ClassFilter getClassFilter() { | ||
return ClassFilter.TRUE; | ||
} | ||
|
||
@Override | ||
public MethodMatcher getMethodMatcher() { | ||
return new AnnotationMethodMatcher(annotationType); | ||
} | ||
|
||
private static class AnnotationMethodMatcher extends StaticMethodMatcher { | ||
private final Class<? extends Annotation> annotationType; | ||
|
||
public AnnotationMethodMatcher(Class<? extends Annotation> annotationType) { | ||
this.annotationType = annotationType; | ||
} | ||
|
||
@Override | ||
public boolean matches(Method method, Class<?> targetClass) { | ||
if (matchesMethod(method)) { | ||
return true; | ||
} | ||
// Proxy classes never have annotations on their redeclared methods. | ||
if (Proxy.isProxyClass(targetClass)) { | ||
return false; | ||
} | ||
// The method may be on an interface, so let's check on the target class as well. | ||
Method specificMethod = AopUtils.getMostSpecificMethod(method, targetClass); | ||
return (specificMethod != method && matchesMethod(specificMethod)); | ||
} | ||
|
||
private boolean matchesMethod(Method method) { | ||
return AnnotatedElementUtils.hasAnnotation(method, this.annotationType); | ||
} | ||
} | ||
} | ||
} |
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
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,27 @@ | ||
<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>io.github.weihubeats</groupId> | ||
<artifactId>spring-boot-nebula</artifactId> | ||
<version>${revision}</version> | ||
</parent> | ||
|
||
<artifactId>spring-boot-nebula-distribute-lock</artifactId> | ||
|
||
<properties> | ||
<maven.compiler.source>11</maven.compiler.source> | ||
<maven.compiler.target>11</maven.compiler.target> | ||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> | ||
</properties> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>io.github.weihubeats</groupId> | ||
<artifactId>spring-boot-nebula-aop-base</artifactId> | ||
</dependency> | ||
</dependencies> | ||
|
||
</project> |