AspectJ dostęp do zawartości adnotacji

AspectJ dostęp do zawartości adnotacji
M9
  • Rejestracja:prawie 10 lat
  • Ostatnio:około 6 lat
0

Cześć,
Mam taką własną adnotację:

Kopiuj
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AdvancedAnnotation {

    boolean disabled() default false;

}

Mam taki aspekt:

Kopiuj
package pl.margor.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

@Aspect
@Component
@Slf4j
public class AdvancedAnnotationAspect {

    @Before("@annotation(AdvancedAnnotation)")
    public void advAnnotationProcess(JoinPoint jp) throws Throwable {
        System.out.println("Advanced annotation aspect executed");
        // TO DO:
        // metoda wykonuje sie
        // w jaki sposob wykonac logike w zaleznosci od wartosci pola isDisabled w adnotacji?
    }


}

Użycie wygląda w ten sposób:

Kopiuj
package pl.margor.rest;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import pl.margor.aop.AdvancedAnnotation;
import pl.margor.aop.TestAnnotation;
import pl.margor.services.CustomTextProvider;

@RestController
@RequestMapping("/api/hello")
@Slf4j
@RequiredArgsConstructor
public class HelloController {

    @RequestMapping(method = RequestMethod.GET, path = "another")
    @AdvancedAnnotation(disabled = true)
    public void another() {
        logger.info("another");
    }

}

Pytanie: w jaki sposób elegancko uzyskać dostęp do pól w adnotacji @AdvancedAnnotation?

Mniej więcej chcę uzyskać taki efekt (nie kompiluje się):

Kopiuj
@Aspect
@Component
@Slf4j
public class AdvancedAnnotationAspect {

    @Before("@annotation(AdvancedAnnotation)")
    public void advAnnotationProcess(JoinPoint jp, AdvacedAnnotation a) throws Throwable {
        System.out.println("Advanced annotation aspect executed");
        if(a.disabled()) {
            System.out.println("Disabled field has value true");
        }
    }

}

To powinno być dość proste. Idealnie byłoby uzyskać adnotacje jako parametr aspektu (widziałem takie przykłady w sieci, ale mi to nie działa).

edytowany 2x, ostatnio: margor90
krzysiek050
  • Rejestracja:ponad 12 lat
  • Ostatnio:ponad 4 lata
  • Postów:1272
1
Kopiuj
@Aspect
@Component
@Slf4j
public class AdvancedAnnotationAspect {

    @Before("@annotation(a)")
    public void advAnnotationProcess(JoinPoint jp, AdvacedAnnotation a) throws Throwable {
        System.out.println("Advanced annotation aspect executed");
        if(a.disabled()) {
            System.out.println("Disabled field has value true");
        }
    }

}

W pointcut wrzucasz nazwę parametru a nie klasę.

M9
  • Rejestracja:prawie 10 lat
  • Ostatnio:około 6 lat
0

Jest postęp, bo kod się kompiluje. To jednak jeszcze nie działa tak jak chciałem: wartość w adnotacji ma zawsze wartość false.

Kopiuj
@Aspect
@Component
@Slf4j
public class TestAnnotationAspect {

    @Before("@annotation(ta)")
    public void testAnnotationProcess(JoinPoint jp, TestAnnotation ta) throws Throwable {
        logger.info("annotation aspect");
        if (ta.disabled() == true) {
            // ten kod nigdy sie nie wykona, bo ta ma zawsze false
            logger.info("disabled = true");
        }
    }

}

Kod adnotacji:

Kopiuj
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AdvancedAnnotation {

    boolean disabled();

}

Czy coś z tą adnotacją jest nie tak, że disabled ma zawsze wartość false?

Użycie:

Kopiuj
@RestController
@RequestMapping("/api/hello")
@Slf4j
public class HelloController {

    @RequestMapping(method = RequestMethod.GET, path = "another")
    @AdvancedAnnotation(disabled = true)
    public void another() {
        logger.info("another");
    }

}
edytowany 1x, ostatnio: margor90
jarekczek
Może pomieszałeś klasy. TestAnnotation ta?
M9
Racja, pomieszałem klasy thx.

Zarejestruj się i dołącz do największej społeczności programistów w Polsce.

Otrzymaj wsparcie, dziel się wiedzą i rozwijaj swoje umiejętności z najlepszymi.