Announcement Announcement Module
No announcement yet.
Spring/AOP advice failing randomly Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring/AOP advice failing randomly

    I have some methods advised by two aspects, one is using the spring AOP support and the other is a `BeanPostProcessor` (`MethodValidationPostProcessor` specifically) which advises all method with `@Validation` annotation. With my unit tests I am trying to force throwing an error by breaking the method contract, but sometimes the validation is in place (the advise imposed by the above mentioned post processor) and sometimes does not work. Does anybody have experienced something similar.

    Here is a small snippet of what I am trying to do (Aspect code):
    public final class LoggingAspect {
        @Before(value = "execution(public * * (..)) && @annotation(loggable)",
            argNames = "joinPoint, loggable")
         public void before(JoinPoint joinPoint, Loggable loggable) {
             //logging here... 
    Annotation (Loggable Code):

    public @interface Loggable {...}
    Interface being annotated with @Validated annotation (Here's a link with related info)

    public interface Dao<T, K> {
        T findById(@NotNull K id);
        T persist(@NotNull T object);

    A base class implementing this interface:
    public abstract class BaseDao<T, K> implements DAO<T,K> {
        public T persist(T object){...}
    And a subclass with a particular behavior:

    public final class UserDao extends BaseDao<User,Long> {
        public T findById(User object){...} 

    And the spring context at last something like this

    <context:component-scan base-package=""/>
    <bean id="validator"
    <bean class="org.springframework.validation.beanvalidation.MethodValidationPostProcessor"/>
    I am testing by calling both methods with null as the argument, but in some occasions I received an IllegalArgumentException: attempt to create saveOrUpdate event with null entity instead of the MethodConstraintViolationException supposedly raised by the MethodValidationInterceptor which advises/intercepts all public method for @Validated annotated interfaces. I am using spring 3.1, Hibernate Validator 4.2 (as required by spring) and aspectj 1.7. Thanks for your help.

  • #2
    EDIT 1: I did some more digging around the test code, and I found out there is something strange related to the MethodValidationPostProcessor or the MethodValidationInterceptor. I disabled the aop-autoproxy and remove the LoggerAspect during testing, and still there is some problem that in some occasion the Validation resulted in what I expected and in some other situations where even though it is supposed to fails the MethodValidationInterceptor or the MethodValidator fails to catch the errors in the calls.
    EDIT 2: I traced the error back to the following lines inside the MethodValidationInterceptor
    Set<MethodConstraintViolation<Object>> result = this.validator.validateAllParameters(
    				invocation.getThis(), invocation.getMethod(), invocation.getArguments(), groups);
    It is possible there is a bug inside the ValidatorImpl that does not pick up the annotation in this situation, and thus failing to validate the parameters?
    EDIT 3: Found solution and my problem was where I put the annotations. Sorry for the inconvenience
    Last edited by avillalain; Aug 19th, 2012, 10:59 PM. Reason: Found solution