I pasted my code following.
I use AOP to log the time when each Spring Controller is invoked. Since each controller has method, 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.)Code:Map referenceData(HttpServletRequest request)
AfterReturningAdvice:
XML configuration file aopConfiguration.xml: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); } }
The controller referenced service layer classes are defined in another xml file services.xmlCode:<?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>
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??


Reply With Quote

