Back Forum Reply New

AOP in Spring Framework

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 quot;entering into my controllerquot; 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 :Code:
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..context.ApplicationContext;
import org..context.support.ClassPathXmlApplicationContext;
import org..context.support.FileSystemXmlApplicationContext;
import org..web.portlet.mvc.AbstractController;
import org..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(quot;D:/SDE/workspace/AOP/loggingAOP/WEB-INF/logging-portlet.xmlquot;);
IBusinessLogic testObject = (IBusinessLogic) ctx.getBean(quot;businesslogicbeanquot;);

testObject.foo();

try
{testObject.bar();
}
catch(BusinessLogicException ble)
{System.out.println(quot;Caught BusinessLogicExceptionquot;);
}

   Map model = new HashMap();
return new ModelAndView(quot;WireFrame1quot;, quot;modelquot;, model);   }   
}
2) Interface, throwsbeforeadvice class and throws after advice classCode:
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(quot;************Inside BusinessLogic.foo() method**************quot;);          }        public void bar() throws BusinessLogicException    {    log.info(quot;***********Inside BusinessLogic.bar() method***********quot;);    throw new BusinessLogicException();    }
}

package com.controller;public class BusinessLogicException extends Exception
{

}

package com.controller;

import java.lang.reflect.Method;
import org..aop.MethodBeforeAdvice;
import org..aop.
import org..
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(quot;********************************************************quot;);
log.info(quot;Entering quot; +this.getClass().getName());
log.info(quot;The Method triggered quot;+m.getName());
log.info(quot;The class triggered quot;+target.getClass());
log.info(quot;The declaring interface quot;+m.getDeclaringClass());
log.info(quot;********************************************************quot;);  }
}
}

package com.controller;

import java.lang.reflect.Method;
import org..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(quot;********************************************************quot;);  log.info(quot;Entering Tracing After Advicequot; +this.getClass().getName());  log.info(quot;The Method triggered quot;+m.getName());  log.info(quot;The class triggered quot;+target.getClass());  log.info(quot;The declaring interface quot;+m.getDeclaringClass());  log.info(quot;********************************************************quot;);}
}
3) logging-portlet.xmlCode:
lt;?xml version=quot;1.0quot; encoding=quot;UTF-8quot;?gt;
lt;!DOCTYPE beans PUBLIC quot;-//SPRING//DTD BEAN//ENquot; quot;../../dtd/spring-beans.dtdquot;gt;

lt;beans gt;

lt;!-- Controllers --gt;

lt;bean id=quot;logControllerquot; class=quot;com.controller.LoggingControllerquot;/gt;

lt;bean id=quot;businesslogicbeanquot;
class=quot;org..aop.framework.ProxyFactoryBeanquot;gt;
lt;property name=quot;proxyInterfacesquot;gt;
lt;valuegt;com.controller.IBusinessLogiclt;/valuegt;
lt;/propertygt;
lt;property name=quot;targetquot;gt;
lt;ref local=quot;beanTargetquot;/gt;
lt;/propertygt;
lt;property name=quot;interceptorNamesquot;gt;
lt;listgt;
lt;valuegt;tracingBeforeAdvisorlt;/valuegt;
lt;valuegt;tracingAfterAdvisorlt;/valuegt;
lt;valuegt;loggingThrowsAdvisorlt;/valuegt;
lt;/listgt;
lt;/propertygt;
lt;/beangt;lt;bean id=quot;beanTargetquot; class=quot;com.controller.BusinessLogicquot;/gt;

lt;!-- Advisor pointcut definition for before advice --gt;
lt;bean id=quot;tracingBeforeAdvisorquot;
class=quot;org..aop.support.RegexpMethodPointcutAdvisorquot;gt;
lt;property name=quot;advicequot;gt;
lt;ref local=quot;theTracingBeforeAdvicequot;/gt;
lt;/propertygt;
lt;property name=quot;patternquot;gt;
lt;valuegt;.*lt;/valuegt;
lt;/propertygt;
lt;/beangt;

lt;!-- Advisor pointcut definition for after advice --gt;
lt;bean id=quot;tracingAfterAdvisorquot;
class=quot;org..aop.support.RegexpMethodPointcutAdvisorquot;gt;
lt;property name=quot;advicequot;gt;
lt;ref local=quot;theTracingAfterAdvicequot;/gt;
lt;/propertygt;
lt;property name=quot;patternquot;gt;
lt;valuegt;.*lt;/valuegt;
lt;/propertygt;
lt;/beangt;

lt;!-- Advisor pointcut definition for throws advice --gt;
lt;bean id=quot;loggingThrowsAdvisorquot;
class=quot;org..aop.support.RegexpMethodPointcutAdvisorquot;gt;
lt;property name=quot;advicequot;gt;
lt;ref local=quot;theLoggingThrowsAdvicequot;/gt;
lt;/propertygt;
lt;property name=quot;patternquot;gt;
lt;valuegt;.*lt;/valuegt;
lt;/propertygt;
lt;/beangt;

lt;!--ADVICE--gt;
lt;bean id=quot;theTracingBeforeAdvicequot;
class=quot;com.controller.TracingBeforeAdvicequot;/gt;
lt;bean id=quot;theTracingAfterAdvicequot;
class=quot;com.controller.TracingAfterAdvicequot;/gt;
lt;bean id=quot;theLoggingThrowsAdvicequot;
class=quot;com.controller.LoggingThrowsAdvicequot;/gt;  

lt;!-- Handler Mapping --gt;

lt;bean id=quot;portletModeHandlerMappingquot; class=quot;org..web.portlet.handler.PortletModeHandlerMappingquot;gt;

lt;property name=quot;portletModeMapquot;gt;
lt;mapgt;
lt;entry key=quot;viewquot;gt;lt;ref bean=quot;logControllerquot;/gt;lt;/entrygt;
lt;/mapgt;
lt;/propertygt;
lt;/beangt;lt;/beansgt;

4) Logs:Code:
2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - lt;********************************************************gt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - lt;Entering com.controller.TracingBeforeAdvicegt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - lt;The Method triggered foogt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - lt;The class triggered class com.controller.BusinessLogicgt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - lt;The declaring interface interface com.controller.IBusinessLogicgt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - lt;********************************************************gt;
2006-12-07 10:34:20,668 INFO [com.controller.BusinessLogic] - lt;************Inside BusinessLogic.foo() method**************gt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingAfterAdvice] - lt;********************************************************gt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingAfterAdvice] - lt;Entering Tracing After Advicecom.controller.TracingAfterAdvicegt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingAfterAdvice] - lt;The Method triggered foogt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingAfterAdvice] - lt;The class triggered class com.controller.BusinessLogicgt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingAfterAdvice] - lt;The declaring interface interface com.controller.IBusinessLogicgt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingAfterAdvice] - lt;********************************************************gt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - lt;********************************************************gt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - lt;Entering com.controller.TracingBeforeAdvicegt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - lt;The Method triggered bargt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - lt;The class triggered class com.controller.BusinessLogicgt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - lt;The declaring interface interface com.controller.IBusinessLogicgt;
2006-12-07 10:34:20,668 INFO [com.controller.TracingBeforeAdvice] - lt;********************************************************gt;
2006-12-07 10:34:20,668 INFO [com.controller.BusinessLogic] - lt;***********Inside BusinessLogic.bar() method***********gt;
Just a quick comment in general. The IBusinessLogic bean doesn't need to be looked up in the code. You don't want to initialise a new applicationContext everytime this method is executed.

Code:
ApplicationContext ctx =new FileSystemXmlApplicationContext(quot;D:/SDE/workspace/AOP/loggingAOP/WEB-INF/logging-portlet.xmlquot;);
IBusinessLogic testObject = (IBusinessLogic) ctx.getBean(quot;businesslogicbeanquot;);
Instead you should use Spring to inject this dependecy.

Code:
public class LoggingController extends AbstractController{   private IBusinessLogic businessLogic;   public void setBusinessLogic(IBusinessLogic businessLogic) {       this.businessLogic = businessLogic;   }
}
lt;bean id=quot;logControllerquot; class=quot;com.controller.LoggingControllerquot;gt;   lt;property name=quot;businessLogicquot;gt;        lt;ref local=quot;businesslogicbeanquot;/gt;lt;/propertygt;
lt;/beangt;Hi,

Thanks a lot for your quick suggestion. And thats really solved my first problem. I found it almost impossible to read the logging-portlet.xml from my controller and thats why i was hardcoding the entire path.

The requirement now is to display logging messages, say quot;entering into the logging controllerquot;. Currently that is not happening. All the other loggings are happening. Can you please tell me what small modification i need to do to log for my controller also.

The other doubt i have is, should i use an interface (IBusinessLogic) in all cases ? Can i just use my Class BusinessLogic without implementing the interface and then change my logging-portlet.xml accordingly to just do the same logging that is done now ?

Can you please give some suggestions ?

You don't *have* to program to interfaces, its just practice to do so.  If you think about testing your application, you can plug in different implementations to suit your needs.
This article might be useful.
lejava/article...rinciples.html

Hi,

Thanks for your quick response. If i were to not use the interface IBusinessLogic and instead use only the BusinessLogic which doesn't implement any interface and its just a class, can you please tell me what changes accordingly i have to do ???

And any inputs to how to do the logging for my controller ? i need my logs printing quot;entering logging controllerquot; ?? any idea how to make this work ?

Hi,

Thanks for all your help. I have successfully done logging in my portlets as required.

I just have one small thing to achieve. When my controller(LoggingController) gets loaded by the server, I also need to display a log message quot;Entering into the Logging Controllerquot;. What do i have to do to achieve this using AOP. Any suggestion will be very helpful !

There are two ways of implementing the lifecycle listeners, you can add an attribute to the bean or implement an interface.  If you want to keep the references to Spring down I would suggest the former.

This should help you out.
docs/...tory-lifecycle
¥
Back Forum Reply New