Failed to detect user level annotation in Spring AOP

I am trying to intercept classes in Spring with the following settings

interceptor

@Aspect
@Component
public class MyInterceptor {

    @Around("execution(* com.example.services..*(..))")
    public Object intercept(ProceedingJoinPoint pjp) throws Throwable {
        if (pjp.getTarget() != null && pjp.getTarget().getClass().getAnnotation(MyAnnotation.class) != null) {
            System.out.println("Yes annotation present");
        } else {
            System.out.println("Annotation not present");
        }

        return = pjp.proceed();
    }   
}

and the class will be intercepted as follows

@Component
@MyAnnotation
public class MyAlert implements IAlert {

}

Everything works fine until I make the following changes

@Configuration
@PropertySource(value = { "file:${catalina.home}/conf/my.properties" })
@Component
@MyAnnotation
public class MyAlert implements IAlert {
    @Autowired
    private Environment environment;
} 

I wanted to read the properties located in the conf tomcat7 conf folder, after making these changes, my interceptor was not able to read my user annotation @MyAnnotation. he says (custom message written in interceptor)

No abstract

and here's the user annotation

@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {

}

I really want the class to be intercepted and the annotation should be detected in the class if it was annotated. I also want to read properties from the conf folder in this intercepted class.

+4
1

, . :

pjp.getSignature().getDeclaringType().getAnnotation(RestController.class) != null
0

Source: https://habr.com/ru/post/1626318/


All Articles