Results 1 to 6 of 6

Thread: Why my Logging Event does not happen?

  1. #1

    Exclamation 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 at 10:00 AM.

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

    Default

    Checkout chapter 6.6.1 to understand the way Spring AOP works.
    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

    Default

    Quote 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 at 10:09 AM.

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

    Default

    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.
    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

    Default

    Quote 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

  6. #6
    Join Date
    Jun 2006
    Location
    The Netherlands
    Posts
    13,632

    Default

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



    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.
    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

Posting Permissions

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