aspectj pointcut using custom annotations on mapper interface

219 views
Skip to first unread message

Huntygowk

unread,
Jul 15, 2013, 12:40:24 PM7/15/13
to mybati...@googlegroups.com
Is there a way of applying custom spring annotations to mapper files?

I'm transferring from classic spring-hibernate to spring-mybatis and
in the DAO's for my hibernate-spring have some custom annotations
applied to some methods. These are then used in aspectj for
intercepting the particular methods.

I can't seem to get the the aspecjt pointcut to trigger when the
method in the  mmybatis mapper file is called. It works fine in the
spring-hierbante combo. I think it might be because the mapper class
is an interface and the annotations are applied to the method in the
interface as opposed to the method in the implementation class.

Some code to help explain...

Spring - hibernate classes

BookDAO.class
public interface BookDAO (){
List<Book> getBookList(String bookType);
}
 
BookDAO.Impl.class
public class BookDAOImpl  implements BookDAO {
@MyAnnotation
List<Book> getBookList(String bookType) {
//impl code
}
}
 
MyAnnotation.class
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation{

}

MyAnnotationAspect.class

@Around("execution(@path.to.annotation.MyAnnotation * *(..))")
public Object checkAroundGetList(final ProceedingJoinPoint pjp) throws
Throwable {
Object[] args = pjp.getArgs();
// implemented code

return obj;
}

This works absolutely fine when running the spring-hibernate classes.

When I try this with mybatis mapper equivalents the annotation is not
picked up. (Everything else is the same except insterad of
BookDAO.class and BookDAO.Impl.class I just have a single
BookDAO.class)

BookDAO.class

public interface BookDAO (){
@MyAnnotation
List<Book> getBookList(String bookType);
}


If I change pointcut to something like:

@Pointcut("execution(* path.to.my.mappers.*.getBookList(..)) && args(String)")

It works, but I'm keen to avoid writing paths to method names.
Annotations seems a much less code smelly way to do it.

Has anyone got any thoughts/advice on this?

Cheers

zachv

unread,
Jul 22, 2013, 3:10:00 AM7/22/13
to mybati...@googlegroups.com

Annotations are not inherited in java. So if only the interface is annotated then the concrete class will not have the annotation.

I have not tried it myself but it seems there are ways that you can get aspectj to still intercept it based on the parent annotations, here's a stackoverflow question along those lines:

http://stackoverflow.com/questions/7122106/aspectj-pointcut-for-subclasses-of-a-class-with-an-annotation
Reply all
Reply to author
Forward
0 new messages