Announcement Announcement Module
Collapse
No announcement yet.
Spring AOP configuration in a Spring web application Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring AOP configuration in a Spring web application

    Hi,
    I want to configure logging using Spring AOP in a Spring web application. I have several controllers and I want to implement it in one of the controller classes. The definition for the controller class in the servelet.xml is as follows:

    This is the definition for the controller:

    <bean id="aboutYouFormController"
    class="com.hx.hd.controller.AboutYouFormController ">
    <property name="sessionForm">
    <value>true</value>
    </property>
    <property name="commandName">
    <value>screen</value>
    </property>
    <property name="commandClass">
    <value>com.hx.hd.beans.HouseholdBean</value>
    </property>
    <!--<property name="validator"><ref bean="beanValidator"/></property>-->
    <property name="validator"><ref bean="aboutYouValidator" /></property>
    <property name="aboutYouServiceBroker"
    ref="aboutYouServiceBroker" />
    <property name="formView">
    <value>AboutYou</value>
    </property>
    <property name="successView">
    <value>YourHome</value>
    </property>
    </bean>

    This is the configuration for the AOP:
    <bean id="loggingAdvice" class="com.hx.hd.logger.AboutYouLogger" />
    <aop:aspectj-autoproxy />
    <aop:config>
    <aopointcut id="loggingPointcut"
    expression="execution(* com.hx.hd.controller.AboutYouFormController.*(..)) " />
    <aop:advisor advice-ref="loggingAdvice" pointcut-ref="loggingPointcut"
    id="loggingInterceptorAdvisor" />
    </aop:config>

    The problem i am facing is that the logging is not happening . The aop configuration is never recognised. I have tested the same in a standalone app and it works.
    Please throw some insight on this..
    thanks in advance..

  • #2
    Perhaps you have 2 application contexts one for controllers and one for services, daos, etc...and maybe you defined aop only in the second one...if so, remember that Spring aop is only applied to beans in the context where it is configured.

    Comment


    • #3
      Thank you for the reply Enrico.

      yes i do have two app contexts one for the controllers and other for the services. I have put in the aop configurations in a separate xml called hs-aop.xml and defined it this way for the services, it works:
      bean id="hdLoggingAdvice"
      class="com.hs.hd.logger.AboutYouLogger" />

      <!-- Configurations for auditing the methods in AboutYouServiceBroker -->
      <!-- invokeAboutYouNext -->
      <aop:config proxy-target-class="true">
      <aopointcut id="aboutYouNextServiceBrokerInvokeMethodPointcut "
      expression="execution(* com.hs..*.invokeAboutY*(..))" />
      <aop:advisor advice-ref="hdLoggingAdvice"
      pointcut-ref="aboutYouNextServiceBrokerInvokeMethodPointcut "
      id="aboutYouNextServiceBrokerInvokeMethodIntercept orAdvisor" />
      </aop:config>

      <!-- Configurations for auditing the methods in HdSendConverter -->
      <!-- convertHdBeanToWSCB -->
      <aop:config proxy-target-class="true">
      <aopointcut id="hdSendConverterWSCBMethodPointcut"
      expression="execution(* com.hs..*.convertHd*(..))" />
      <aop:advisor advice-ref="hdLoggingAdvice"
      pointcut-ref="hdSendConverterWSCBMethodPointcut"
      id="hdSendConverterWSCBMethodInterceptorAdvisor" />
      </aop:config>

      But it does not log for any methods in the controller. I understand from other threads that the internal methods of a controller cannot be logged. But then, now i have a class generated by axis and it does not log for this as well. So is it correct to say that logging can be done for user defined methods only?

      Thank you.

      Priya

      Comment


      • #4
        I have put in the aop configurations in a separate xml called hs-aop.xml
        and how are you importing this file? my guess is that you are importing using a import-resource tag that points to the file, and this is included in your 'services' config file (applicationContext.xml). If so, then aop doesn't work for controller since it's only defined for the service context and not for the servlet context. You should include aop configuration that is specific for your controllers, and do that in the servlet context file (appname-servlet.xml).

        Next to that, double-check that your pointcut expression creates the desired joinpoints.

        I understand from other threads that the internal methods of a controller cannot be logged. But then, now i have a class generated by axis and it does not log for this as well. So is it correct to say that logging can be done for user defined methods only?
        That is a whole different issue. I suggest a read of the aop chapter in the reference guide; in short, Spring AOP is based on proxies and as such suffers from some important limitations. First of all, AOP is applied only to defined Spring beans that are in the context where the aop config is specified (so, an Axis generated class cannot have pointcuts applied, and this applies also for third-party libraries such as Hibernate classes etc.); next to that, aop obviously doesn't get applied for self-invocations (internal method calls).

        To overcome those limitations, you can still configure and activate full-blown AspectJ, which integrates very well with Spring. Read the appropriate chapter in the reference guide.

        Comment


        • #5
          Originally posted by Enrico Pizzi View Post
          and how are you importing this file? my guess is that you are importing using a import-resource tag that points to the file, and this is included in your 'services' config file (applicationContext.xml). If so, then aop doesn't work for controller since it's only defined for the service context and not for the servlet context. You should include aop configuration that is specific for your controllers, and do that in the servlet context file (appname-servlet.xml).
          I have included the hs-aop.xml this as "contextConfigLocation" in the <init-param> tag in web.xml so i guess all the beans would be at disposal. Also, my controllers extend the simple form controller and i have no other public user defined methods to be logged so i guess its a no-go case with respect to controllers. let me know if you have some thoughts on this..

          Comment


          • #6
            I have included the hs-aop.xml this as "contextConfigLocation" in the <init-param> tag in web.xml so i guess all the beans would be at disposal
            Wrong. The files you specify in the contextConfigLocation parameter are all picked up by the ContextConfigListener, and they all contribute to create a single 'main' application context for your web-app. Spring's DispatcherServlet loads its own 'web' application context which is then made a child context of the main context. Your AOP is defined in the main context, so no AOP for beans defined in the 'web' context (where I suppose, if you've configured things by the book, your controller beans are).

            On a side note extending SimpleFormController is not the way to go with Spring Web, it's deprecated and discouraged, so unless you're still on Java 1.4 switch to annotated @Controller style.

            Comment

            Working...
            X