I have a class annotated with a custom annotation. I want to run an aspect that should trigger before all the method calls and constructor call whenever an object or static method of that class is called. Can we do this in AspectJ?
Annotation Class
import com.stackoverflow @Target({ElementType.TYPE, ElementType.METHOD, ElementType.CONSTRUCTOR}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface RunAdivceBeforeCall { // Advice logic } Aspect Class
import com.stackoverflow @Aspect Class HandleAspect{ @Before("@annotation(RunAdivceBeforeCall) && execution(@RunAdivceBeforeCall * *.*(..))") public void runAnAdviceBeforeCall(JoinPoint joinpoint) { //Advice logic } } Class to run the Aspects
import com.stackoverflow
@RunAdivceBeforeCall public Class NavigationBar { NavigationBar(){ } public void navigateToHomePage(){ } public void navigateToUserAccount(){ } public void navigateToHelp(){ } }
The above logic i added doesn't work. Is there any other way to trigger before all the method and constructor call whenever an NavigationBar object methods, constructor call and static method of that class is called. I need this only in AspectJ as my project is not spring?
@annotation(MyAnnotation), for that you need@within(MyAnnotation)or, somewhat suboptimally,@within(@MyAnnotation), as suggested in your Baeldung tutorial. Same tutorial does not refer to@annotationat all, though. So please do not claim that you are following it, because actually you are not. You are doing something else.