-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from weihubeats/v_1.1.0_aop优化
V 1.1.0 代码优化
- Loading branch information
Showing
18 changed files
with
282 additions
and
110 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
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
115 changes: 115 additions & 0 deletions
115
wh-core/src/main/java/com/mq/idempotent/core/aop/MqIdempotentAnnotationAdvisor.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.mq.idempotent.core.aop; | ||
|
||
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 : 2021/12/30 17:58 | ||
* @description: | ||
*/ | ||
public class MqIdempotentAnnotationAdvisor extends AbstractPointcutAdvisor implements BeanFactoryAware { | ||
|
||
private final Advice advice; | ||
|
||
private final Pointcut pointcut; | ||
|
||
private final Class<? extends Annotation> annotation; | ||
|
||
public MqIdempotentAnnotationAdvisor(@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
Oops, something went wrong.