Hi,
I have a requirement to implement logging using AOP for my portlets developed in spring. I have done a sample application and the logging is working fine. But i have used an interface, I would like to know if i can use just a class (BusinessLogic) instead of the interface(IBusinessLogic) i have used in my code and achieve the same logging. Also the logging for "entering into my controller" is not being achieved in my case. Please let me know what changes i have to do, to not make use of an interface and also to do logging for my controller. Following is my code ,
1) Controller :
2) Interface, throwsbeforeadvice class and throws after advice classCode:package com.controller; import java.util.HashMap; import org.apache.log4j.Logger; import java.util.Map; import javax.portlet.PortletException; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import org.springframework.web.portlet.mvc.AbstractController; import org.springframework.web.portlet.ModelAndView; import com.controller.BusinessLogicException; import com.controller.IBusinessLogic; import java.util.*; public class LoggingController extends AbstractController{ private Logger log = Logger.getLogger(this.getClass()); public ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception { ApplicationContext ctx =new FileSystemXmlApplicationContext("D:/SDE/workspace/AOP/loggingAOP/WEB-INF/logging-portlet.xml"); IBusinessLogic testObject = (IBusinessLogic) ctx.getBean("businesslogicbean"); testObject.foo(); try { testObject.bar(); } catch(BusinessLogicException ble) { System.out.println("Caught BusinessLogicException"); } Map model = new HashMap(); return new ModelAndView("WireFrame1", "model", model); } }
3) logging-portlet.xmlCode:package com.controller; public interface IBusinessLogic { public void foo(); public void bar() throws BusinessLogicException; } package com.controller; import org.apache.log4j.Logger; public class BusinessLogic implements IBusinessLogic { private Logger log = Logger.getLogger(this.getClass()); public void foo() { log.info("************Inside BusinessLogic.foo() method**************"); } public void bar() throws BusinessLogicException { log.info("***********Inside BusinessLogic.bar() method***********"); throw new BusinessLogicException(); } } package com.controller; public class BusinessLogicException extends Exception { } package com.controller; import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; import org.springframework.aop. import org.springframework. import org.apache.log4j.Logger; public class TracingBeforeAdvice implements MethodBeforeAdvice { private Logger log = Logger.getLogger(this.getClass()); public void before(Method m, Object[] args, Object target) throws Throwable { log.info("********************************************************"); log.info("Entering " +this.getClass().getName()); log.info("The Method triggered "+m.getName()); log.info("The class triggered "+target.getClass()); log.info("The declaring interface "+m.getDeclaringClass()); log.info("********************************************************"); } } } package com.controller; import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; import org.apache.log4j.Logger; public class TracingAfterAdvice implements AfterReturningAdvice { private Logger log = Logger.getLogger(this.getClass()); public void afterReturning(Object object, Method m, Object[] args, Object target) throws Throwable { log.info("********************************************************"); log.info("Entering Tracing After Advice" +this.getClass().getName()); log.info("The Method triggered "+m.getName()); log.info("The class triggered "+target.getClass()); log.info("The declaring interface "+m.getDeclaringClass()); log.info("********************************************************"); } }
Code:<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "../../dtd/spring-beans.dtd"> <beans > <!-- Controllers --> <bean id="logController" class="com.controller.LoggingController"/> <bean id="businesslogicbean" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="proxyInterfaces"> <value>com.controller.IBusinessLogic</value> </property> <property name="target"> <ref local="beanTarget"/> </property> <property name="interceptorNames"> <list> <value>tracingBeforeAdvisor</value> <value>tracingAfterAdvisor</value> <value>loggingThrowsAdvisor</value> </list> </property> </bean> <bean id="beanTarget" class="com.controller.BusinessLogic"/> <!-- Advisor pointcut definition for before advice --> <bean id="tracingBeforeAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="theTracingBeforeAdvice"/> </property> <property name="pattern"> <value>.*</value> </property> </bean> <!-- Advisor pointcut definition for after advice --> <bean id="tracingAfterAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="theTracingAfterAdvice"/> </property> <property name="pattern"> <value>.*</value> </property> </bean> <!-- Advisor pointcut definition for throws advice --> <bean id="loggingThrowsAdvisor" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice"> <ref local="theLoggingThrowsAdvice"/> </property> <property name="pattern"> <value>.*</value> </property> </bean> <!--ADVICE--> <bean id="theTracingBeforeAdvice" class="com.controller.TracingBeforeAdvice"/> <bean id="theTracingAfterAdvice" class="com.controller.TracingAfterAdvice"/> <bean id="theLoggingThrowsAdvice" class="com.controller.LoggingThrowsAdvice"/> <!-- Handler Mapping --> <bean id="portletModeHandlerMapping" class="org.springframework.web.portlet.handler.PortletModeHandlerMapping"> <property name="portletModeMap"> <map> <entry key="view"><ref bean="logController"/></entry> </map> </property> </bean> </beans> 4) Logs:Code:2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - <********************************************************> 2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - <Entering com.controller.TracingBeforeAdvice> 2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - <The Method triggered foo> 2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - <The class triggered class com.controller.BusinessLogic> 2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - <The declaring interface interface com.controller.IBusinessLogic> 2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - <********************************************************> 2006-12-07 10:34:20,668 INFO [com.controller.BusinessLogic] - <************Inside BusinessLogic.foo() method**************> 2006-12-07 10:34:20,668 INFO [com.controller.TracingAfterAdvice] - <********************************************************> 2006-12-07 10:34:20,668 INFO [com.controller.TracingAfterAdvice] - <Entering Tracing After Advicecom.controller.TracingAfterAdvice> 2006-12-07 10:34:20,668 INFO [com.controller.TracingAfterAdvice] - <The Method triggered foo> 2006-12-07 10:34:20,668 INFO [com.controller.TracingAfterAdvice] - <The class triggered class com.controller.BusinessLogic> 2006-12-07 10:34:20,668 INFO [com.controller.TracingAfterAdvice] - <The declaring interface interface com.controller.IBusinessLogic> 2006-12-07 10:34:20,668 INFO [com.controller.TracingAfterAdvice] - <********************************************************> 2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - <********************************************************> 2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - <Entering com.controller.TracingBeforeAdvice> 2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - <The Method triggered bar> 2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - <The class triggered class com.controller.BusinessLogic> 2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - <The declaring interface interface com.controller.IBusinessLogic> 2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - <********************************************************> 2006-12-07 10:34:20,668 INFO [com.controller.BusinessLogic] - <***********Inside BusinessLogic.bar() method***********>


Reply With Quote