Announcement Announcement Module
Collapse
No announcement yet.
When advice will apply to a bean or method Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • When advice will apply to a bean or method

    I've seen lots of postings here about confusion for when a particular advisor will apply to a given bean or method. (Note: I'm relatively new to AOP and probably will use some terms incorrectly.) I have just come across some behavior that doesn't really make sense to me, although I have figured out a workaround. So I thought I would share the situation, and at the verly least it may help someone else in a similar situation. Ideally, someone can explain what's really going on.

    I'm trying to apply some "before advice" to a particular service-layer method, in order to verify that the caller has permission. I have defined a service interface, called CampaignService, containing the method in question, and I have a class CampaignServiceImpl that implements CampaignService. The method that I'm trying to advise is called getCampaign.

    I define my pointcut using an expression that matches the interface, not the class, like so (using xml in Spring 2.0):

    Code:
    <aop:config>
      <aop:advisor id="campaignServiceSecurity" advice-ref="campaignSecurityAdvice" 
        pointcut="execution(* *..service.CampaignService.getCampaign(..))" order="0"/>
    </aop:config>
    When I run my app, the getCampaign() method is not advised. I turned debug logging on for Spring, and at some point in the log file I see something like "Candidate advisor ... rejected for class ...service.impl.CampaignServiceImpl". (I have used ... to truncate the log entry, that was not literally in the log file.) It is not really clear to me why this is. I have seen threads about using CGLIB to proxy the class rather than the interface, but I don't think that's what's going on.

    I did figure out two ways to make the advice take place. One was to change the pointcut expression so that it matched the class, rather than the interface, like so:

    Code:
    <aop:config>
      <aop:advisor id="campaignServiceSecurity" advice-ref="campaignSecurityAdvice" 
        pointcut="execution(* *..service.impl.CampaignServiceImpl.getCampaign(..))" order="0"/>
    </aop:config>
    I didn't like this fix, because then I'd have to make sure to update the pointcut expression if I ever swapped in a different implementation class. I could come up with an expression that matched both the interface and the class, but I am much happier if things work based soley on interfaces.

    The second "fix" was more interesting. My CampaignServiceImpl extends a base class, and it is this base class that actually contains the getCampaign() method, although the base class does not implement the CampaignService interface. If I take that getCampaign() method out of the base class, and put it in CampaignServiceImpl, then the method does get advised, with the original pointcut expression (matching the interface, not the class). I can't figure out why this would be. I thought the advice and proxying was based on the interface.

  • #2
    This is interesting. Maybe you should report this to Jira so it can be investigated further.

    Regards,
    Andreas

    Comment

    Working...
    X