mercredi 13 juillet 2016

Spring aop MethodMatcher get method from interface, not from implementation

I have created MethodMatcher:

new StaticMethodMatcherPointcut() {
    @Override
    public boolean matches(Method method, Class<?> targetClass) {
        return  (targetClass != null && targetClass.getAnnotation(Transactional.class) != null)
                || method.getAnnotation(Transactional.class) != null;
    }
};

and I have

   public interface Service {
       void proceed();
   }
   public ServiceImpl {
       @Transactional
       public void proceed() {
        ...
       }
   }

Some calls of method matcher take ServiceImpl as target class and method from Service, so getAnnotation(Transactional.class) returns null, because proceed in interface have not annotation. Is this desired behaviour? Or is this bug? I fixed this trying to get method from target class

Method getMethod(Method method, Class<?> targetClass) {
    if (targetClass != null) {
        try {
            return targetClass.getMethod(method.getName(), method.getParameterTypes());
        } catch (NoSuchMethodException e) {
            return method;
        }
    }
    return method;
}

Do you know any other way to get it working?

Aucun commentaire:

Enregistrer un commentaire