實戰Spring之注解處理器
需求場景
有時候我們希望定義一個特定的注解,被注解打標過的方法能夠被代理,完成一些特定的操作。
當然我們可以通過Spring的切面去完成比如:
@Around(value = "execution(* com.xxx.xxx.xxx.dao..*(..))")
但這種方式感覺還是太麻煩了,我們希望像事務注解@Transaction
一樣,打上標記就會被代理,不需要定義各種表達式切面。
另外簡單點、通用點、好理解一點。。。
另外簡單點、通用點、好理解一點。。。
另外簡單點、通用點、好理解一點。。。
實現思路
其實本質的做法也是通過切面去完成,不過@Around
是基于表達式去處理,而我們希望能通過注解方式,來決定是否需要代理。(PS: 表達式也能實現注解攔截)
這里涉及到切面的兩個點:
-
Advice
: 你可以理解為攔截器 -
Pointcut
: 你可以理解規則匹配器
當我們梳理思路的時候只需要思考:
- Spring在bean處理的時候,會遍歷每個類和方法。
- 這個時候在遍歷時,去判斷每個方法是否符合
pointcut
規則,滿足的話則進行代理 - 那么一旦代理的話,肯定是需要有具體的代理業務攔截邏輯的,
advice
就是邏輯處理攔截器。 - 我們只需要將兩者結合形成一個工廠類每次去找被代理后的邏輯就行了。
好,那么我們看如何去實現?
實現代碼
1. Pointcut
我們先來定義規則,我們希望特定的注解打在方法上,這些方法能夠被代理,然后查看Pointcut
的實現類有一個
StaticMethodMatcherPointcut
/**
* 特定注解攔截器
*
* @author : liukx
* @time : 2020/7/9 - 20:02
*/
public class AnnotationAttributeSourcePointcut extends StaticMethodMatcherPointcut implements Serializable {
/**
* 需要被攔截代理的注解列表
*/
private Set<Class<? extends Annotation>> annotationsOperation = new LinkedHashSet<>(8);
public void addAnnotations(Class<? extends Annotation> annotation) {
annotationsOperation.add(annotation);
}
/**
* 符合該注解的通通被代理起來
*
* @param method
* @param targetClass
* @return
*/
@Override
public boolean matches(Method method, Class<?> targetClass) {
if (CacheParseUtil.isContainAnnotations(annotationsOperation, method)) {
return true;
}
return false;
}
/**
* 遍歷該方法是否包含特定的注解
*
* @param annotations
* @param element
* @return
*/
public boolean isContainAnnotations(Set<Class<? extends Annotation>> annotations, AnnotatedElement element) {
boolean isContain = false;
for (Class<? extends Annotation> annotation : annotations) {
if (AnnotatedElementUtils.hasAnnotation(element, annotation)) {
isContain = true;
break;
}
}
return isContain;
}
}
2. Advice
我們在第一步定義了匹配規則,一旦被規則match匹配上,那么對應的邏輯希望交給advice。
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.beans.factory.annotation.Autowired;
import java.io.Serializable;
import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.List;
/**
* 實現緩存的攔截器
*
* @author : liukx
* @time : 2020/7/9 - 20:08
*/
public class AnnotationInterceptor implements MethodInterceptor, Serializable {
@Autowired(required = false)
private List<AnnotationProcessService> cacheProcessServices;
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
Object proceed = null;
if (cacheProcessServices != null && invocation instanceof ReflectiveMethodInvocation) {
ReflectiveMethodInvocation methodInvocation = (ReflectiveMethodInvocation)invocation;
Method method = invocation.getMethod();
Annotation[] annotations = method.getAnnotations();
for (int i = 0; i < annotations.length; i++) {
Annotation annotation = annotations[i];
for (int j = 0; j < cacheProcessServices.size(); j++) {
AnnotationProcessService cache = cacheProcessServices.get(i);
if (annotation.annotationType() == cache.annotation()) {
proceed = cache.invokeWithinTransaction(methodInvocation);
}
}
}
return proceed;
}
return invocation.proceed();
}
}
AnnotationProcessService
定義一個這樣的接口是希望,后續如果還有其他注解需要處理時,只需要實現該接口準備好注解類和邏輯方法,我們可以直接回調它處理,這樣會更為通用。
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.core.Ordered;
import java.lang.annotation.Annotation;
/**
* 注解的執行器
*
* @author liukaixiong
* @Email liukx@elab-plus.com
* @date 2021/9/27 - 10:49
*/
public interface AnnotationProcessService extends Ordered {
@Override
default int getOrder() {
return LOWEST_PRECEDENCE;
}
/**
* 具體的注解
*
* @return
*/
public Class<? extends Annotation> annotation();
/**
* 上面匹配到的注解會被觸發,盡量不要對結果做改變。
*
* @param invocation
* @return
* @throws Throwable
*/
public Object invokeWithinTransaction(ReflectiveMethodInvocation invocation) throws Throwable;
}
被代理的類,先走這個接口類進行注解annotation
匹配,然后在流轉到invokeWithinTransaction
方法。這樣更為通用。
3. DefaultBeanFactoryPointcutAdvisor
org.springframework.aop.support.DefaultBeanFactoryPointcutAdvisor
這個是Spring內部提供的類,用于組合Pointcut
和Advisor
的。
我們只需要在配置文件進行組合
@Configuration
public class AopConfig {
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@ConditionalOnBean(AnnotationProcessService.class)
public DefaultBeanFactoryPointcutAdvisor transactionAdvisor(
AnnotationAttributeSourcePointcut annotationAttributeSourcePointcut) {
DefaultBeanFactoryPointcutAdvisor advisor = new DefaultBeanFactoryPointcutAdvisor();
// 具體的攔截器
advisor.setAdvice(annotationInterceptor());
// 需要被攔截方法的規則判斷器,一旦符合,才會被代理給攔截器處理
advisor.setPointcut(annotationAttributeSourcePointcut);
return advisor;
}
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@ConditionalOnBean(AnnotationProcessService.class)
public AnnotationAttributeSourcePointcut annotationAttributeSourcePointcut(
List<AnnotationProcessService> annotationProcessServiceList) {
// 該類是用作比對方法的注解是否符合代理的條件
AnnotationAttributeSourcePointcut cacheAttributeSourcePointcut = new AnnotationAttributeSourcePointcut();
annotationProcessServiceList.forEach((annotation) -> {
cacheAttributeSourcePointcut.addAnnotations(annotation.annotation());
});
return cacheAttributeSourcePointcut;
}
@Bean
@Role(BeanDefinition.ROLE_INFRASTRUCTURE)
@ConditionalOnBean(AnnotationProcessService.class)
public AnnotationInterceptor annotationInterceptor() {
AnnotationInterceptor interceptor = new AnnotationInterceptor();
return interceptor;
}
}
后續你需要定義各種自定義的注解只需要實現AnnotationProcessService
接口就行了,簡單方便好拓展。
測試用例
我們來定義一個@AuthorDescription
注解處理器,
/**
* 對于功能定義的一些攔截描述
*
* @author liukaixiong
* @Email liukx@elab-plus.com
* @date 2021/9/27 - 11:10
*/
public class AuthorDescriptionAnnotationProcess implements AnnotationProcessService {
private Logger logger = LoggerFactory.getLogger(getClass());
/**
* 表示只處理@AuthorDescription注解內容
*/
@Override
public Class<? extends Annotation> annotation() {
return AuthorDescription.class;
}
@Override
public Object invokeWithinTransaction(ReflectiveMethodInvocation invocation) throws Throwable {
Method method = invocation.getMethod();
AuthorDescription authorDescription = AnnotationUtils.getAnnotation(method, AuthorDescription.class);
String clazzName = method.getDeclaringClass().getSimpleName();
String name = method.getName();
String methodName = clazzName + "." + name;
Object[] arguments = invocation.getArguments();
// 業務邏輯處理
return invocation.proceed();
}
}
隨便定義的測試方法
public class TestAuthorAnnotation {
@AuthorDescription(modulesName = "user", describe = "這是一個測試", nickname = {"liukx",
"jay"}, searchKey = "${request[0].id}-${request[1].username}")
public String test(Map<String, String> request, UserModel userModel) {
return "OK";
}
}
測試類
@RunWith(SpringRunner.class)
//@EnableCaching
// 這個是開啟切面,必要的。
@EnableAspectJAutoProxy
@SpringBootTest(classes = {AopConfig.class,AuthorDescriptionAnnotationProcess.class,
TestAuthorAnnotation.class})
public class AuthorDescriptionAnnotationProcessTest {
@Autowired
private TestAuthorAnnotation testAnnotation;
@Test
public void testInvokeWithinTransaction() {
Map<String, String> request = new HashMap<>();
request.put("id", "1314");
request.put("username", "jayzhou");
request.put("sex", "MAN");
request.put("age", "13");
UserModel userModel = new UserModel();
userModel.setUserId("5555");
userModel.setUsername("liukx");
String test = testAnnotation.test(request, userModel);
System.out.println(test);
}
}
為了觀看體驗,有的無關緊要的代碼我就不貼了,希望大家更關注核心邏輯流轉。
如果你有疑問,歡迎留言交流,我看到了會第一時間答復你。
如果你覺得有收獲,可以點個關注點贊支持下,謝謝啦。