I am trying to implement a Custommade annotation that contains a value as a parameter and when the annotation is used, this value should be processed with a MDC.put.
So when I use a method with the annotation @Usecase("TestUsecase"), MDC.get("usecase") should actually output "TestUsecase", but currently I only get null. When debugging I noticed that the method in my UsecaseAspect is never called.
I have already tried several things:
- in my TestApplication I have already set @EnableAspectJAutoProxy.
- In my POM I have set both the dependency for aspectjweaver and spring-boot-starter-aop
Here some code:
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Usecase {
String value();
@Aspect
@Component
public class UsecaseAspect {
@After("@annotation(usecase)")
public void setLoggingContext(@NotNull Usecase usecase) {
MDC.put("usecase", usecase.value());
}
}
@SpringBootTest(classes = TestApplication.class)
@ActiveProfiles({"test", "tests"})
@EnableAspectJAutoProxy
class UsecaseTest {
@Usecase("TestUsecase")
public void testUsecaseMethod() {
}
@Test
void testUsecaseAnnotation() {
testUsecaseMethod();
assertEquals("TestUsecase", MDC.get("usecase"));
}
}