The project structure is
spring-contract
spring-aop
as dependency of (1)spring-service
as dependency of (2).
I have a param annotation @MyAnnotation
in spring-contract
project, an aspect class in spring-aop
,
package com.learning.aspect;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AnnotatedFieldAspect {
@Before("execution(* *(.., @MyAnnotation (*), ..)) && args(newValue)")
public void advice(JoinPoint jp, Object newValue) {
System.out.println(">>> inspecting "+newValue+" on "+jp.getTarget()+", "+jp.getSignature());
}
}
The class which will be advised by the annotation is in spring-service
project,
package com.learning.fieldtest.service;
public class TestField {
private String name;
public String getName() {
return name;
}
public void setName(@MyAnnotation String name) {
this.name = name;
}
public static void main(String[] args) {
TestField testField = new TestField();
testField.setName("Alex");
testField.m1(testField.getName());
System.out.println(testField.getName());
}
public void m1(@MyAnnotation String string) {
System.out.println("Inside m1() @MyAnnotation" + string);
}
}
The class TestField methods are not marked as advised, if i move all the classes in the same package i get result. There are other aspects which is written at class and method level that is getting applied.
>>> inspecting Alex on com.learning.fieldtest.service.TestField@3fa77460, void com.learning.fieldtest.service.TestField.setName(String)
>>> inspecting Alex on com.learning.fieldtest.service.TestField@3fa77460, void com.learning.fieldtest.service.TestField.m1(String)
Inside m1() @MyAnnotationAlex
Alex
Custom Annotation
package com.learning.spring.contract;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.PARAMETER)
public @interface MyAnnotation {
}