Как реализовать расширение с помощью 2 классов для динамического добавления аннотаций

Я использую Jakarta EE. У меня есть пара расширений CDI.

Эти 2 аннотации являются перехватчиками с приоритетом 1002 и 1003.

Я добавил эти 2 класса в папку службы в META-INF с именем файла jakarta.enterprise.inject.api.Extension, как показано ниже

abc.xyz.RestFlowInterceptorExtension abc.xyz.LogInterceptorExtension

Однако на самом деле работает только один из перехватчиков (с приоритетом 1003), хотя оба настроены. По отдельности оба перехватчика работают нормально, когда в файл добавлен только один из них.

Может ли кто-нибудь подсказать, как на самом деле с этим поступить?

Мне нужно динамически добавить пару перехватчиков из 2 расширений.

RestFlowInterceptorExtension implements Extension {

    <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> pat) {
        System.out.println("Inside RestFlowInterceptorExtension");
        AnnotatedType<T> annotatedType = pat.getAnnotatedType();
        AnnotatedTypeConfigurator<T> configurator = pat.configureAnnotatedType();
        for (AnnotatedMethod<? super T> method : annotatedType.getMethods()) {
            if (method.isAnnotationPresent(GET.class) || method.isAnnotationPresent(POST.class)
                    || method.isAnnotationPresent(PUT.class) || method.isAnnotationPresent(OPTIONS.class)
                    || method.isAnnotationPresent(DELETE.class) || method.isAnnotationPresent(HEAD.class)) {
                if (!method.isAnnotationPresent(DoNotLogPayload.class)) {
                    System.out.println("Adding RestFlowInterceptorBinding annotation to "+method.getClass().getName());
                    configurator.filterMethods(m -> m.equals(method)).findFirst().ifPresent(
                            methodConfigurator -> methodConfigurator.add(RestFlowInterceptorBinding.Literal.getInstance()));
                }
            }
        }
    }
}
public class LogInterceptorExtension implements Extension {

    <T> void processAnnotatedType(@Observes ProcessAnnotatedType<T> pat) {
        System.out.println("Inside LogInterceptorExtension");
        AnnotatedType<T> annotatedType = pat.getAnnotatedType();
        AnnotatedTypeConfigurator<T> configurator = pat.configureAnnotatedType();
        for (AnnotatedMethod<? super T> method : annotatedType.getMethods()) {
            if (method.isAnnotationPresent(GET.class) || method.isAnnotationPresent(POST.class)
                    || method.isAnnotationPresent(PUT.class) || method.isAnnotationPresent(OPTIONS.class)
                    || method.isAnnotationPresent(DELETE.class) || method.isAnnotationPresent(HEAD.class)) {
                if (!method.isAnnotationPresent(DoNotLogPayload.class)) {
                    System.out.println("Adding LogInterceptorBinding annotation to "+method.getClass().getName());
                    configurator.filterMethods(m -> m.equals(method)).findFirst().ifPresent(
                            methodConfigurator -> methodConfigurator.add(LogInterceptorBinding.Literal.getInstance()));
                }
            }
        }
    }
}
Феврония
Вопрос задан22 апреля 2024 г.

1 Ответ

Ваш ответ

Загрузить файл.