Announcement Announcement Module
Collapse
No announcement yet.
AOP in Spring Framework Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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 "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 :

    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.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);
        }
        
    }
    2) Interface, throwsbeforeadvice class and throws after advice class

    Code:
    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("********************************************************");
    	 }
    }
    3) logging-portlet.xml

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

  • #2
    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("D:/SDE/workspace/AOP/loggingAOP/WEB-INF/logging-portlet.xml");
    IBusinessLogic testObject = (IBusinessLogic) ctx.getBean("businesslogicbean");
    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;
        }
    }
     
    <bean id="logController" class="com.controller.LoggingController">
        <property name="businessLogic">
                        <ref local="businesslogicbean"/>
                </property>
    </bean>

    Comment


    • #3
      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 "entering into the logging controller". 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 ?

      Comment


      • #4
        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.
        http://www.artima.com/lejava/article...rinciples.html

        Comment


        • #5
          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 "entering logging controller" ?? any idea how to make this work ?

          Comment


          • #6
            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 "Entering into the Logging Controller". What do i have to do to achieve this using AOP. Any suggestion will be very helpful !
            Last edited by pradeep_scorpion; Dec 7th, 2006, 07:46 AM.

            Comment


            • #7
              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.
              http://www.springframework.org/docs/...tory-lifecycle

              Comment

              Working...
              X