Announcement Announcement Module
Collapse
No announcement yet.
Why my Logging Event does not happen? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Why my Logging Event does not happen?

    I pasted my code following.

    I use AOP to log the time when each Spring Controller is invoked. Since each controller has method
    Code:
    Map referenceData(HttpServletRequest request)
    , so my point cut is there. When this method is invoked, the Logging Event should happend.(Which print out the time when this method is invoked.)

    AfterReturningAdvice:
    Code:
    import org.apache.log4j.Level;
    import org.apache.log4j.Logger;
    import org.springframework.aop.AfterReturningAdvice;
    
    public class AfterHandler implements AfterReturningAdvice{
    	
    	private Logger logger=Logger.getLogger(this.getClass().getName());
    	
    	public void afterReturning(Object object, Method method,Object[] args, Object target) throws Throwable{
    		long procTime=System.currentTimeMillis();
    		logger.log(Level.INFO, args[0]+"at "+procTime);
    		
    	}
    
    }
    XML configuration file aopConfiguration.xml:
    Code:
    <?xml version="1.0" encoding="UTF8"?>
    	
    <beans xmlns="http://www.springframework.org/schema/beans"
    	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns:lang="http://www.springframework.org/schema/lang"
    	xmlns:aop="http://www.springframework.org/schema/aop"
    	xmlns:tx="http://www.springframework.org/schema/tx"
    	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                               http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang-2.0.xsd
                               http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
    						   http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
    
    <!--Controller classes to be logged-->
    <bean name="articleController" class="org.app.web.ArticleController">
    		<property name="readerService" ref="readerService"/>
    		<property name="authorService" ref="authorService"/>
    </bean>
    
    <bean name="bookController"
    		class="org.app.web.BookController">
    		<property name="authorService" ref="authorService"/>
    		<property name="priceService" ref="priceService"/>
    		
    </bean>
    
    <!--configure as automatical proxy-->
    <bean id="autoProxyCreator" class="org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator"/>
    
    
    <!--TimeHandler used to get time data-->
    <bean id="afterHandler" class="org.app.aop.AfterHandler"/>
    <bean id="afterHandlerAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
    	<property name="advice">
    		<ref bean="afterHandler"/>
    	</property>
    	
    	<!--scan the matched method:referenceData() in controllers -->
    	<property name="patterns">
    		<value>.*referenceData.*</value>
    	</property>
    	
    </bean>
    	
    </beans>
    The controller referenced service layer classes are defined in another xml file services.xml

    When I run my web application, for example, click one link button which will call the controller and invoke the referenceData() method inside controller, the advisor(AfterHandler.java) should print out the information of the time when the referenceData() method is invoked. But no logging event happened thought the application is running. Why??
    Last edited by blust1984; Jul 2nd, 2007, 10:00 AM.

  • #2
    Checkout chapter 6.6.1 to understand the way Spring AOP works.

    Comment


    • #3
      Originally posted by mdeinum View Post
      Checkout chapter 6.6.1 to understand the way Spring AOP works.
      I have read this article, this article described the Spring 2.0 support for AOP using @AspectJ and schema-based aspect definitions. but I use the lower-level Spring AOP APIs(Spring 1.2). I confused that referenceData() method is invoked, but why Logging Event does not happen?hope some body give some help to makes me understand more clearly. thanks
      Last edited by blust1984; Jul 2nd, 2007, 10:09 AM.

      Comment


      • #4
        Apparently you don't understand the mechanism Spring uses.

        Spring uses a proxying mechanism. it proxies only PUBLIC methods and only intercepts EXTERNAL calls. Controllers only have 1 public method handleRequest, which is also the only method you can intercept on a Controller using Spring based AOP.

        Comment


        • #5
          Originally posted by mdeinum View Post
          Spring uses a proxying mechanism. it proxies only PUBLIC methods and only intercepts EXTERNAL calls.
          Hi, thank you for your reply, I am new in AOP. and one more question,what do you mean about EXTERNAL call? can you give an example? Let's suppose we ignore the PUBLIC method things, does the way how I call referenceData() in above code is EXTERNAL call or not?? thanks

          Comment


          • #6
            As I mentioned before read and understand chapter 6.6.1 of the reference guide.

            http://static.springframework.org/do...proxy-call.png

            Only calls ONTO (foo() on the proxUyy) the proxy are intercepted as soon as you are IN (foo() on the object) the proxy nothing is intercepted anymore. A proxy is normally only generated from the interface(s) which the class implements. As in the case of a Controller it has only one method (as I also mentioned in my previous post) only that method can be intercepted.

            Comment

            Working...
            X