實戰Spring之注解處理器

實戰Spring之注解處理器

需求場景

有時候我們希望定義一個特定的注解,被注解打標過的方法能夠被代理,完成一些特定的操作。

當然我們可以通過Spring的切面去完成比如:

@Around(value = "execution(* com.xxx.xxx.xxx.dao..*(..))")

但這種方式感覺還是太麻煩了,我們希望像事務注解@Transaction 一樣,打上標記就會被代理,不需要定義各種表達式切面。

另外簡單點、通用點、好理解一點。。。

另外簡單點、通用點、好理解一點。。。

另外簡單點、通用點、好理解一點。。。

實現思路

其實本質的做法也是通過切面去完成,不過@Around 是基于表達式去處理,而我們希望能通過注解方式,來決定是否需要代理。(PS: 表達式也能實現注解攔截)

這里涉及到切面的兩個點:

  • Advice : 你可以理解為攔截器
  • Pointcut: 你可以理解規則匹配器

當我們梳理思路的時候只需要思考:

  1. Spring在bean處理的時候,會遍歷每個類和方法。
  2. 這個時候在遍歷時,去判斷每個方法是否符合pointcut規則,滿足的話則進行代理
  3. 那么一旦代理的話,肯定是需要有具體的代理業務攔截邏輯的,advice就是邏輯處理攔截器。
  4. 我們只需要將兩者結合形成一個工廠類每次去找被代理后的邏輯就行了。

好,那么我們看如何去實現?

實現代碼

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內部提供的類,用于組合PointcutAdvisor的。

我們只需要在配置文件進行組合

@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);
    }

}

為了觀看體驗,有的無關緊要的代碼我就不貼了,希望大家更關注核心邏輯流轉。

如果你有疑問,歡迎留言交流,我看到了會第一時間答復你。

如果你覺得有收獲,可以點個關注點贊支持下,謝謝啦。

?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,546評論 6 533
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,570評論 3 418
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,505評論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,017評論 1 313
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,786評論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 55,219評論 1 324
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,287評論 3 441
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,438評論 0 288
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,971評論 1 335
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,796評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,995評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,540評論 5 359
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,230評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,662評論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,918評論 1 286
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,697評論 3 392
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,991評論 2 374

推薦閱讀更多精彩內容