Announcement Announcement Module
Collapse
No announcement yet.
AOP Logging does not work. Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AOP Logging does not work.

    Dear all,

    I would like to implement Spring AOP for logging perpose in my applicaton. But my sevice bean does not created by the applicationContext.xml file. if i remore the aop code form the application than it works fine. Please guide me.

    my applicationContext.xml is as follows..........

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
    
    <!--
     my other beans
    
     if I remove the following beans from my file then the applicaiton works fine.
    -->
    	 
    	<bean id="userServiceBean" class="org.springframework.aop.framework.ProxyFactoryBean">
    		<property name="proxyInterfaces">
    			<value>org.services.IUserDataService</value>
    		</property>
    		<property name="target">
    			<ref bean="userDataServiceTarget"/>
    		</property>
    		<property name="interceptorNames">
    			<list>
    				<value>loggingInterceptor</value>
    			</list>
    		</property>		
    	</bean>
    	
    	<!--
    	<bean id="userServiceBean" class="org.springframework.aop.framework.ProxyFactoryBean">
      		<property name="proxyInterfaces">
      			<value>org.services.IUserDataService</value>
      		</property>
        	<property name="interceptorNames">
          	<list>
            	<value>loggingInterceptor</value>
            	<value>userDataServiceTarget</value>
          	</list>
        	</property>
      	</bean>
    	-->
    	
    	<bean id="loggingInterceptor" class="org.interceptors.LoggingInterceptor"></bean>	
    	
    	<bean id="indexAction" class="org.test.IndexAction" singleton="false">
    		<constructor-arg ref="user"/>
    		<property name="userService" ref="userServiceBean"/>
    	</bean>
    	 
    </beans>
    my interceptor is as follows
    Code:
    package org.interceptors;
    import java.lang.reflect.Method;
    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.springframework.aop.AfterReturningAdvice;
    import org.springframework.aop.MethodBeforeAdvice;
    import org.springframework.aop.ThrowsAdvice;
    
    public class LoggingInterceptor implements AfterReturningAdvice,MethodBeforeAdvice,ThrowsAdvice{ 
                 private static Log log = null;	
    
                 public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
    		log = LogFactory.getLog(target.getClass());
    		log.info("Beginning method : "+method.getName());		
    	}
    
    	public void before(Method method, Object[] args, Object target) throws Throwable {
    		log = LogFactory.getLog(target.getClass());
    		log.info("Ending method : "+method.getName());		
    	}
    	
    	public void afterThrowing(Method method, Object[] args, Object target, Throwable ex){
    		log = LogFactory.getLog(target.getClass());
    		log.info("Exception in method : "+method.getName()+"  Exception is : "+ex);
    	}	
    }
    i addedd the following jars.
    spring.jar for sring 2.0.7
    dom4j-1.6.1.jar
    common-collections.jar
    cglib-nodep-2.1_3.jar

    Thank a lot for giving me time.

    with best regards,
    ishaan
    Last edited by ishaan; Oct 17th, 2007, 05:37 AM. Reason: for making my query more viewar friendly

  • #2
    The reference manual has examples of how to do this, I'd have a look at them first. When you say it's not working, what you do mean? Are you getting an exception? Is it possible to see the code and the rest of the configuration?

    Comment


    • #3
      Service class does not instatiate in AOP logging application

      Dear Karl,

      Thanks a lot for your reply. Actually I am using struts2 and spring. So in my action class when I try to use the obeject of sevice class i.e. UserDataServiceImpl I got the NullPointerException (I pointed that in the code in red color). here is my struts Action Class.

      Code:
      package org.test;
      import org.services.IUserDataService;
      import org.apache.struts2.ServletActionContext;
      import org.beans.User;
      import com.opensymphony.xwork2.ActionSupport;
      import org.springframework.context.ApplicationContext;
      import org.springframework.web.context.support.WebApplicationContextUtils;
      import com.opensymphony.xwork2.ActionContext;
      
      public class IndexAction extends ActionSupport {
      	IUserDataService userService;// come by dependency Injection
      	User user;// = new User();
          public static final String MESSAGE = "Struts is up and running...";
          
          /*
          public IndexAction(IUserDataService userService, User user){
          	this.userService = userService;
          	this.user = user;
          }
      	*/
          
          public IndexAction(User user){    	
          	this.user = user;
          }
          
          @Override
      	public String execute() throws Exception {
          	if(user == null)
          		System.out.println("User is null");
          	else
          		System.out.println("User : "+user.toString());
          	if(userService == null)
          		System.out.println("userService is null");
          	else
          		System.out.println("userService : "+userService.toString());
              setMessage(MESSAGE);
              if(userName != null){        	
              	//user = findUser(userName);
              	user = findUserById(1);
              	ActionContext.getContext().getSession().put("userName", user.getUserName());
              	ActionContext.getContext().getSession().put("email", user.getEmail());
              	System.out.println("User : "+user.getUserName()+"----------------Email : "+user.getEmail());
              	return SUCCESS;
              }
              return ERROR;
          }
          
          public User findUser(String userName){
          	    	return userService.findUserByUserName(userName);
                     /*At this point I get the null pointer exception so
      i think my action class could not get the object of my service class 
      from applicaitonContect.xml but in applicationContext.xml 
      if i remove the configuratino for AOP i mean the "userServiceBean" (i.e ProxyFactoryBean)
      and simply code the configuration for service class without AOP then 
      it works fine. so I think I may wrong some where in implementing AOP.
      But I could not identify that what goes wrong.*/
          }
          
          
          public User findUserById(long id){
          	return userService.findUserById(id);
          }
          
      
          private String message;
          private String userName;
      
          public void setMessage(String message){
              this.message = message;
          }
      
          public String getMessage() {
              return message;
          }
      
      	public String getUserName() {
      		return userName;
      	}
      
      	public void setUserName(String userName) {
      		this.userName = userName;
      	}
      
      	public IUserDataService getUserService() {
      		return userService;
      	}
      
      	public void setUserService(IUserDataService userService) {
      		this.userService = userService;
      	}
      }
      in fact I also use hibernate3 for the persistence and data fetching perpose. But all that worked fine till I addedd the AOP. As I added the AOP for logging I start getting the NullPointerException for the service bean.

      I addedd the following jars for the struts2
      common-logging-1.0.4.jar
      freemarker-2.3.8.jar
      ognl-2.6.11.jar
      struts2-core-2.0.9.jar
      xwork-2.0.4.jar
      struts2-spring-plugin-2.0.9.jar
      hibernate3.jar

      do i need to add some more jars ?
      pleaseeeeeeeeeeeeeeeee help me out to solve my problem from last 2 full days I am trying to implement AOP for logging in my application but could not able to do so. pleaseeee pleaseeeee help meee

      pleaseeeeeeeeee

      thank you very much
      Ishaan

      Comment


      • #4
        Change your logging strategy, maybe?

        I had a similar problem and discovered it is quite tricky -- almost downright impossible -- to debug Spring AOP because of the proxying that it does, so I ended up scrapping my logging interceptor class and its associated Spring configuration and going with a different approach that is documented here:

        http://www.theserverside.com/tt/arti...espacesSpring2

        It took me about 20 minutes to incorporate it into my project (many thanks to Aravindan Ramkumar, the author of the article, for providing the example!). The best part is that my seemingly spurious NullPointerException messages disappeared. It bothers me that I couldn't track them down but OTOH I'm moving forward again.

        FWIW,
        Blake
        Last edited by bbishop; Oct 19th, 2007, 08:01 AM.

        Comment


        • #5
          Thanks a lot for your valuable reply

          Dear bbishop,

          Thankssss a lot for your reply. Now I am reading the article you sent me. But I am really very thankful to you for giving me reply. In fact from last 2 days I was trying to solve my problem but I anable to find the cause of this. Your guidance will definitely help me.

          Thank you very much :-)

          with best regards,
          Ishaan

          Comment

          Working...
          X