Results 1 to 8 of 8

Thread: Aop against AbstractWizardFormController

  1. #1
    Join Date
    May 2009
    Posts
    9

    Default Aop against AbstractWizardFormController

    Hi,

    I am new to AOP.

    I have a controller class, ChangePasswordController , that extends an abstract Controller class which in turn extends AbstractWizardFormController. My requirement is to write an advice against the methods in ChangePasswordController, so that whenever any exception is thrown from the controller, it should come to my Advice class.

    I have written the following code in the configuration file,
    Code:
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    <bean id = "handleBeforeExceptionResolver"  class = "com.a.c.exception.HandleBeforeExceptionResolverAdvice"/>
    
    <aop:config>		
         <aop:aspect ref="handleBeforeExceptionResolver">
               <aop:pointcut id="pointcutExceptionLog" expression="execution(* com.a.b.ChangePasswordController.*(..))" />
               <aop:after-throwing method="afterThrowingException" pointcut-ref="pointcutExceptionLog" throwing="exception"  />		   	 
         </aop:aspect>
    </aop:config>
    I tried with before and after advices also, but control is not coming to my advice.

    I guess the proxy is created using CGLIB. Please let me know if I am doing anything wrong.

    Any pointers are welcome.

    Thanks in Advance,
    SD

  2. #2
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    Spring uses proxy based aop ( i suggest the aop chapter especially the section which explains proxies). For a Controller there is just a single method which can be intercepted, handleRequest and it is not part of your ChangePasswordController.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  3. #3
    Join Date
    May 2009
    Posts
    9

    Default

    Hi,

    Thanks for the reply.

    I tried writing after-throwing advice against handleRequest and it worked.

    But I have a doubt. I think proxies will be created using CGLIB since we have put proxy-target-class="true". In this case, can we advice all methods in my custom controller ?

    Thanks,
    SDB
    Last edited by SDB; Dec 30th, 2010 at 05:06 PM.

  4. #4
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,629

    Default

    It doesn't matter if it is a jdk proxy or a class proxy there is still only 1 method being intercepted and that is the handleRequest method of the Controller.

    As I stated read the aop chapter especially the part which explains proxies.
    Marten Deinum
    Java Consultant / Pragmatist / Open Source Enthousiast / Author


    Pro Spring MVC: With Web Flow
    Conspect

    Have you read the reference guide.
    Use the [ code ] tags, young padawan

  5. #5
    Join Date
    May 2009
    Posts
    9

    Default I think I understand it now

    Hi Marten,

    After reading the documentation again, I think I understand it better now. In my earlier reading I did not take in this part completely:

    If you want to force the use of CGLIB proxying (for example, to proxy every method defined for the target object, not just those implemented by its interfaces) you can do so. However, there are some issues to consider:

    ...

    The constructor of your proxied object will be called twice. This is a natural consequence of the CGLIB proxy model whereby a subclass is generated for each proxied object. For each proxied instance, two objects are created: the actual proxied object and an instance of the subclass that implements the advice. This behavior is not exhibited when using JDK proxies. Usually, calling the constructor of the proxied type twice, is not an issue, as there are usually only assignments taking place and no real logic is implemented in the constructor.
    So, even if CGLIB proxies are used, calls made by the bean against the this reference (from handleRequest method) will not pass through via the proxy, and hence the advice will not be applied.

    I initially assumed that if CGLIB proxies are used, a subclass of the bean class will be created - in which case this would have pointed to the proxy. Mea cupla.

    Thanks,
    SDB

  6. #6
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    287

    Default

    Quote Originally Posted by SDB View Post
    Hi,

    I am new to AOP.

    I have a controller class, ChangePasswordController , that extends an abstract Controller class which in turn extends AbstractWizardFormController. My requirement is to write an advice against the methods in ChangePasswordController, so that whenever any exception is thrown from the controller, it should come to my Advice class.

    I have written the following code in the configuration file,
    Code:
    <aop:aspectj-autoproxy proxy-target-class="true"/>
    
    <bean id = "handleBeforeExceptionResolver"  class = "com.a.c.exception.HandleBeforeExceptionResolverAdvice"/>
    
    <aop:config>		
         <aop:aspect ref="handleBeforeExceptionResolver">
               <aop:pointcut id="pointcutExceptionLog" expression="execution(* com.a.b.ChangePasswordController.*(..))" />
               <aop:after-throwing method="afterThrowingException" pointcut-ref="pointcutExceptionLog" throwing="exception"  />		   	 
         </aop:aspect>
    </aop:config>
    I tried with before and after advices also, but control is not coming to my advice.

    I guess the proxy is created using CGLIB. Please let me know if I am doing anything wrong.

    Any pointers are welcome.

    Thanks in Advance,
    SD

    Isn't what you are looking for is @AfterThrowing?

  7. #7
    Join Date
    Dec 2010
    Location
    Singapore
    Posts
    287

    Default

    Following is something we did on a past project,

    XML configuration

    Code:
    	
    <aop:aspectj-autoproxy />
    <bean id="DaoExceptionAspect" class="com.aspects.DaoExceptionAspect">
    </bean>
    Aspect class

    Code:
    @Aspect
    public class DaoExceptionAspect{
    
    @Pointcut("execution(* com.jdbc.*.*(..))")
    protected void aspectClass() {
    }
    
    @AfterThrowing(pointcut = "aspectClass()", throwing = "ex")
    public void handleException(Throwable ex) {
    	final ExceptionInterceptor exceptionInterceptor = ExceptionInterceptor.getInstance();
    	exceptionInterceptor.handleException(ex);
    }

  8. #8
    Join Date
    May 2009
    Posts
    9

    Default

    Thanks amiladomingo.

    I was trying to write advice against methods in Custom Controller Class, but Now I understood that we can write intercept only handleRequest method.I tried that it's working fine now.

    -SDB

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •