I have created an annotation with name Validation and inject on method parameter and I have been using aspect before invocation to validate my object. It is not working
Annotation code
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Validation {}
Aop Code
@Aspect
@Component
public class ValidatorAOP {
@Before("valditionAnnotation()")
public void validate(final JoinPoint jp) throws Throwable {
Validator object = (Validator) jp.getTarget();
object.validator();
}
@Pointcut("@annotation(Validation)")
public void valditionAnnotation() {
}
}
Using annotation as public TrackingId createNewOrder(@Validation Order newOrder)
This is called before any method that has argument annotated with @Validation
, I think that's what you wanted:
@Before("execution(* *(.., @Validation (*), ..)) && args(.., toVerify)")
public void validate(final JoinPoint joinPoint, final Object toVerify) {
}
If you don't want the parameter value then just remove the && ...
part and method argument.
@Before("execution(* *(.., @Validation (*), ..)) && args(.., toVerify)")
| | | | | | | | | | | |
1 2 3 4 5 6 7 8 9 10 11 12
@Validation
) Object toVerify
) Be careful if you have methods with multiple parameters, some annotated, some not, and other combinations - not sure it's gonna always work.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.