RamPrakash
RamPrakash

Reputation: 3234

Get Class level annotation value in Spring AOP

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.

Upvotes: 0

Views: 1732

Answers (1)

R.G
R.G

Reputation: 7121

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

Upvotes: 4

Related Questions