Results 1 to 3 of 3

Thread: using AOP for adding logging to web layer

  1. #1
    Join Date
    Jan 2012
    Location
    United States
    Posts
    4

    Default using AOP for adding logging to web layer

    I am trying to add logging to trace the path a user took starting from structs action classes to the persistance layer via the service layer. The logging works for service and persistance layer, but does not work on the web layer.

    I have several structs action classes in the abc.cde.action package and all have names ending in Action and they extend ActionSupport.

    This is what I added to the application-context.xml
    ..
    ..
    <import resource="spring/web-profiling.xml" />
    ..
    ..

    This is in web-profiling.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <!--
    ======================= Profiling Interceptor ====================================
    -->


    <!-- Before Advice Interceptor -->
    <bean id="actionMethodBeforeInterceptor" class="abc.cde.myapp.aop.LoggingMethodBeforeInterc eptor" />


    <bean name="webProfileAutoProxy"
    class="org.springframework.aop.framework.autoproxy .BeanNameAutoProxyCreator">
    <property name="beanNames">
    <list>
    <value>abc.cde.action.*Action</value>

    </list>
    </property>
    <property name="interceptorNames">
    <list>
    <value>actionMethodBeforeInterceptor</value>
    </list>
    </property>
    <property name="proxyTargetClass" value="true"></property>
    </bean>
    </beans>


    This is LoggingMethodBeforeInterceptor

    import java.lang.reflect.Method;

    import org.apache.log4j.Logger;
    import org.springframework.aop.MethodBeforeAdvice;


    public class LoggingMethodBeforeInterceptor implements MethodBeforeAdvice
    {

    public void before(Method method, Object[] args, Object target) throws Throwable
    {
    Logger log = Logger.getLogger("myApp-file-web");
    log.info("method="+method.getName() + " from " + target.getClass()+ " called");
    }

    }

    I am not getting any errors, but not seeing anything in my logs.

  2. #2
    Join Date
    Dec 2009
    Location
    India
    Posts
    108

    Default

    The only problem I see, is the beanNames property of webProfileAutoProxy. Is the regular expression correct. I think you must be naming your action classes without their package names. i.e. the abc.cde.action.MyCustomAction class would be named as myCustomAction. If this is the case your regular expression should look like *Action and not like abc.cde.action.*Action.

    Can you give it a go and check once again?

  3. #3
    Join Date
    Jan 2012
    Location
    United States
    Posts
    4

    Default

    I noticed that the action classes were not defined as beans in the struts-config.xml(below). Does that cause a problem for Spring AOP to find the beans?
    In the service/persistance layers they were declared as interfaces and the logging works there properly.

    Assuming that might be the issue I creating an xml file (web-actions.xml below) and added all my action clases with a unique bean id and referenced it in application-context.xml.

    I am then referencing it in web-profiling.xml by the bean id like you suggested. I got a couple of startup errors as some of the action classes were abstract.
    I fixed those by declaring only the non-absrtact classes in both web-actions.xml and web-profiling.xml

    I am still not getting the log statements in LoggingMethodBeforeInterceptor to appear.

    Also I am wondering if I should use DefaultAdvisorAutoProxyCreator instead, as I am having to explicitly declare all action class bean ids now. Please advise.
    This is how the configuration looks now:


    struts-config.xml

    <action path="/Abc" type="abc.cde.MyAction" />

    <action path="/Xyz" type="abc.cde.YourAction" />

    ..
    ..
    ..


    application-config.xml

    <!-- Profiling Interceptors -->
    <import resource="spring/web-actions.xml" />
    <import resource="spring/web-profiling.xml" />
    ..
    ..
    ..

    web-actions.xml :

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>

    <bean id="myAction" class="abc.cde.MyAction" />
    <bean id="yourAction" class="abc.cde.YourAction" />
    ..
    ..

    </beans>


    web-profiling.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    <!--
    ======================= Profiling Interceptor ====================================
    -->


    <!-- Before Advice Interceptor -->
    <bean id="actionMethodBeforeInterceptor" class="gov.usda.fsa.wa.businessfile.aop.LoggingMet hodBeforeInterceptor" />


    <bean name="webProfileAutoProxy"
    class="org.springframework.aop.framework.autoproxy .BeanNameAutoProxyCreator">
    <property name="beanNames">
    <list>
    <value>myAction</value>
    <value>yourAction</value>
    ..
    ..
    </list>
    </property>

    <property name="interceptorNames">
    <list>
    <value>actionMethodBeforeInterceptor</value>
    </list>
    </property>
    <property name="proxyTargetClass" value="true"></property>
    </bean>
    </beans>

Tags for this Thread

Posting Permissions

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