Announcement Announcement Module
Collapse
No announcement yet.
Pointcut for methods on DomainObject Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Pointcut for methods on DomainObject

    I have some domain objects that are annotated with custom annotations. I would like a pointcut that would enable around advice on their equals method. I've tried almost everything using @Aspect support, but the only methods I have been able to advice so far are those within spring-managed beans (e.g. Service or Repository beans). Is there a pointcut for non-managed beans such as Domain objects? Spring reference guide seems to imply yes, but I can't figure this out.

    Code:
    @CustomAnnotation
    public class DomainObject{
      ...
    }
    Code:
    @Aspect
    public class DomainObjectEqualsAspect
    {
      @Around("equalsMethod()")
      public Object evaluateEquals(ProceedingJoinPoint jp) throws Throwable
      {
         Object retVal = null;
         ....
         return retVal;
      }
    
      @Pointcut("execution(* org.mycompany.DomainObject.equals(..))")
      public void equalsMethod()
      {
      }
    }

  • #2
    I suggest a forum search as this question has been answered numerous times before and a read of the aop chapter.

    In short spring uses proxy based aop and as such can only advice beans it knowns, i.e. spring managed beans. If you want something more elaborate you have to use loadtime or compile time weaving instead of proxy based aop.

    Comment


    • #3
      Ok, got the LTW working, but I am using java-based config and was wondering what the equivalent would be for <context:load-time-weaver/>? DefaultContextLoadTimeWeaver bean doesn't seem to be doing the trick.

      Comment


      • #4
        Aside from the Java-based config question, my AOP setup using LTW is working. The reason I'm setting this up is so I can define a pointcut that will intercept all calls to the equals() method for all my domain objects that have a certain annotation.

        I've tried something below, but it only works if my domain classes actually have already overridden Object's equals method. I want the advice to actually perform the override logic. Is there a way of doing this without declaring the override in each domain class?

        Code:
        @Aspect
        public class DomainEntityEqualsAspect
        {
        
          @Around("equalsMethod() && allDomainEntities()")
          public Object evaluateEquals(ProceedingJoinPoint jp) throws Throwable
          {
            //...override equals() logic here
          }
        
         @Pointcut("@target(org.mycompany.annotation.CustomAnnotation)")
          public void allDomainEntities()
          {
          }
        
          @Pointcut("execution(boolean equals(..))")
          public void equalsMethod()
          {
          }
        }
        Anyone out there good with pointcuts that may be able to help?
        Last edited by jzcfk9; May 26th, 2011, 07:56 PM.

        Comment

        Working...
        X