0

I have an annotation which is a class level annotation

 @Dummy(value = 123)

How I do create an aspect which gets invoked before any method execution of this annotated class. I would like to just print the value of annotation in the aspect's advice.

RamPrakash
  • 2,218
  • 3
  • 25
  • 54

1 Answers1

3

Following aspect would achieve the same

@Component
@Aspect
public class DummyAspect {
    @Before(value = "@target(dummy) && within(com.your.package..*)")
    public void before(JoinPoint jp, Dummy dummy) {
        System.out.println(dummy.value());
    }
}

within() - is a scoping designator to narrow the scope of classes to be advised. Without this designator the run could have undesired outcome as it could target framework classes as well.

Do go through this answer from @kriegaex to understand the designator in detail.

References : https://docs.spring.io/spring/docs/current/spring-framework-reference/core.html#aop-pointcuts-designators

R.G
  • 6,436
  • 3
  • 19
  • 28
  • 1
    within designator is for *Any join point (method execution only in Spring AOP) within the specified package or one of its sub-packages* and `(com..*)` means any *Any type inside the com package or all of its direct subpackages*. `..*` requires a root package ( com in our example ) to make it a valid package reference. – R.G May 16 '20 at 03:42
  • More info [here](https://www.eclipse.org/aspectj/doc/next/progguide/quick-typePatterns.html) . – R.G May 16 '20 at 03:56