Announcement Announcement Module
Collapse
No announcement yet.
AspectJ in Spring Portlet--urgent ! ! ! Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • AspectJ in Spring Portlet--urgent ! ! !

    Hi,

    I am developing a simple portlet using Spring 2.0. I am trying to use AspectJ for logging. I am supposed to use the java j2sdk1.4.2_12. Can somebody let me know what more configuration to print my customised log messages. In this case the System.out.println that i had given in my HelloFromAspectJ isnt getting printed.

    I am attaching here with the following,

    i) Controller

    package com.amp.test.aspect;
    import java.util.HashMap;
    import org.apache.log4j.Logger;
    import java.util.Map;
    import javax.portlet.RenderRequest;
    import javax.portlet.RenderResponse;
    import org.springframework.web.portlet.mvc.AbstractContro ller;
    import org.springframework.web.portlet.ModelAndView;
    import java.util.*;
    //import java.util.logging.Level;
    //import java.util.logging.Logger;

    public class GraphController extends AbstractController {
    private Logger log = Logger.getLogger(this.getClass());


    public ModelAndView handleRenderRequestInternal(RenderRequest request, RenderResponse response) throws Exception {

    log.info("######################################## #######################");
    log.info("Inside GraphController.handleRenderRequestInternal() ! ! ! ");
    log.info("######################################## #####################");


    Map model = new HashMap();
    return new ModelAndView("WireFrame1", "model", model);
    }
    }


    ii) Aspect

    package com.amp.test.aspect;

    import java.util.logging.*;
    import org.apache.log4j.Logger;

    import org.aspectj.lang.*;
    import org.aspectj.lang.annotation.Aspect;

    public aspect HelloFromAspectJ {
    private Logger log = Logger.getLogger(this.getClass());

    public void callMeBefore()
    {


    log.info("**************************************** ************");
    log.info("Inside callMeBefore");
    log.info("**************************************** ************");
    System.out.println("---------------------------------------------------------------------------");
    System.out.println("callMeBefore called");
    System.out.println("---------------------------------------------------------------------------");
    throw new RuntimeException();
    }


    }

    iii) Application-context.xml

    <?xml version="1.0" encoding="UTF-8"?>

    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...ring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


    <!-- Controllers -->

    <bean id="graphController" class="com.amp.test.aspect.GraphController"/>

    <bean id="beanAspect" class="com.amp.test.aspect.HelloFromAspectJ">
    </bean>

    <aop:config>
    <aop:aspect id="helloFromAspectJ" ref="beanAspect">
    <aopointcut id="method1" expression="execution(* org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(..))"/>
    <aop:before pointcut="execution(* org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(..))" method="callMeBefore"/>

    </aop:aspect>
    </aop:config>


    <!-- Default View Resolver -->
    <bean
    class="org.springframework.web.servlet.view.Intern alResourceViewResolver" id="viewResolver">
    <property name="cache" value="false"/>
    <property name="viewClass" value="org.springframework.web.servlet.view.JstlVi ew"/>
    <property name="prefix" value="/jsp/"/>
    <property name="suffix" value=".jsp"/>
    </bean>
    </beans>

    iv) aspectController-portlet.xml (name of my portlet)

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schem...ring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd">


    <!-- Controllers -->

    <bean id="graphController" class="com.amp.test.aspect.GraphController"/>

    <bean id="beanAspect" class="com.amp.test.aspect.HelloFromAspectJ">
    </bean>
    <aop:config>
    <aop:aspect id="helloFromAspectJ" ref="beanAspect">
    <!-- <aopointcut id="callMeBefore" expression="execution(* com.amp.test.aspect.*.*(..))"/> -->
    <aopointcut id="callMeBefore" expression="execution(* org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(..))"/>
    <aop:before pointcut="execution(* org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(..))" method="callMeBefore"/>
    <aop:after pointcut="execution(* org.springframework.web.servlet.mvc.AbstractContro ller.handleRequest(..))" method="callMeAfter"/>

    </aop:aspect>
    </aop:config>

    <!-- Handler Mapping -->

    <bean id="portletModeHandlerMapping" class="org.springframework.web.portlet.handler.Por tletModeHandlerMapping">

    <property name="portletModeMap">
    <map>
    <entry key="view"><ref bean="graphController"/></entry>
    </map>
    </property>
    </bean>

    </beans>


    v) logs

    2006-11-29 12:40:41,600 INFO [org.springframework.web.portlet.context.XmlPortlet ApplicationContext] - <Bean factory for application context [PortletApplicationContext for namespace 'aspectController-portlet']: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [graphController,beanAspect,org.springframework.aop .config.internalAutoProxyCreator,callMeBefore,org. springframework.aop.aspectj.AspectJExpressionPoint cut,org.springframework.aop.aspectj.AspectJPointcu tAdvisor,org.springframework.aop.aspectj.AspectJEx pressionPointcut#1,org.springframework.aop.aspectj .AspectJPointcutAdvisor#1,portletModeHandlerMappin g]; parent: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [graphController,beanAspect,org.springframework.aop .config.internalAutoProxyCreator,callMeBefore,org. springframework.aop.aspectj.AspectJExpressionPoint cut,org.springframework.aop.aspectj.AspectJPointcu tAdvisor,org.springframework.aop.aspectj.AspectJEx pressionPointcut#1,org.springframework.aop.aspectj .AspectJPointcutAdvisor#1,viewResolver]; root of BeanFactory hierarchy>
    2006-11-29 12:40:41,600 INFO [org.springframework.web.portlet.context.XmlPortlet ApplicationContext] - <9 beans defined in application context [PortletApplicationContext for namespace 'aspectController-portlet']>
    2006-11-29 12:40:41,616 INFO [org.springframework.web.portlet.context.XmlPortlet ApplicationContext] - <Bean 'org.springframework.aop.config.internalAutoProxyC reator' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)>
    2006-11-29 12:40:41,616 INFO [org.springframework.web.portlet.context.XmlPortlet ApplicationContext] - <Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMess ageSource@10c986]>
    2006-11-29 12:40:41,616 INFO [org.springframework.web.portlet.context.XmlPortlet ApplicationContext] - <Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicatio nEventMulticaster@1e9c3f]>
    2006-11-29 12:40:41,616 INFO [org.springframework.beans.factory.support.DefaultL istableBeanFactory] - <Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [graphController,beanAspect,org.springframework.aop .config.internalAutoProxyCreator,callMeBefore,org. springframework.aop.aspectj.AspectJExpressionPoint cut,org.springframework.aop.aspectj.AspectJPointcu tAdvisor,org.springframework.aop.aspectj.AspectJEx pressionPointcut#1,org.springframework.aop.aspectj .AspectJPointcutAdvisor#1,portletModeHandlerMappin g]; parent: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [graphController,beanAspect,org.springframework.aop .config.internalAutoProxyCreator,callMeBefore,org. springframework.aop.aspectj.AspectJExpressionPoint cut,org.springframework.aop.aspectj.AspectJPointcu tAdvisor,org.springframework.aop.aspectj.AspectJEx pressionPointcut#1,org.springframework.aop.aspectj .AspectJPointcutAdvisor#1,viewResolver]; root of BeanFactory hierarchy]>
    2006-11-29 12:40:41,663 INFO [org.springframework.web.portlet.DispatcherPortlet] - <Using context class 'org.springframework.web.portlet.context.XmlPortle tApplicationContext' for portlet 'aspectController'>
    2006-11-29 12:40:41,678 INFO [org.springframework.web.portlet.DispatcherPortlet] - <Unable to locate PortletMultipartResolver with name 'portletMultipartResolver': no multipart request handling provided>
    2006-11-29 12:40:41,678 INFO [org.springframework.web.portlet.DispatcherPortlet] - <No HandlerAdapters found in portlet 'aspectController': using default>
    2006-11-29 12:40:41,678 INFO [org.springframework.web.portlet.DispatcherPortlet] - <FrameworkPortlet 'aspectController': initialization completed in 156 ms>
    2006-11-29 12:40:41,678 INFO [org.springframework.web.portlet.DispatcherPortlet] - <Portlet 'aspectController' configured successfully>
    2006-11-29 12:40:41,725 INFO [com.amp.test.aspect.GraphController] - <################################################# ##############>
    2006-11-29 12:40:41,725 INFO [com.amp.test.aspect.GraphController] - <Inside GraphController.handleRenderRequestInternal() ! ! ! >
    2006-11-29 12:40:41,725 INFO [com.amp.test.aspect.GraphController] - <################################################# ############>
    2006-11-29 12:40:41,725 WARN [org.springframework.web.servlet.view.InternalResou rceViewResolver] - <View caching is SWITCHED OFF -- DEVELOPMENT SETTING ONLY: This can severely impair performance>

  • #2
    It looks like your pointcut is incorrect: You should be using org.springframework.web.portlet.mvc.AbstractContro ller in place of org.springframework.web.servlet.mvc.AbstractContro ller.

    -Ramnivas

    Comment


    • #3
      thanks Ramnivas,

      I had made the changes as you said, in my application-context.xml and the aspectController-portlet.xml and deployed my portlet. The System.out.println in my HelloAspectJ.aj isn't gettting printed.The following is the log message that is displayed.

      2006-11-29 14:14:31,944 INFO [org.springframework.web.context.ContextLoader] - <Root WebApplicationContext: initialization started>
      2006-11-29 14:14:32,132 INFO [org.springframework.core.CollectionFactory] - <JDK 1.4+ collections available>
      2006-11-29 14:14:32,147 INFO [org.springframework.core.CollectionFactory] - <Commons Collections 3.x available>
      2006-11-29 14:14:32,241 INFO [org.springframework.beans.factory.xml.XmlBeanDefin itionReader] - <Loading XML bean definitions from ServletContext resource [/WEB-INF/context/applicationContext.xml]>
      2006-11-29 14:14:32,788 INFO [org.springframework.web.context.support.XmlWebAppl icationContext] - <Bean factory for application context [Root WebApplicationContext]: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [graphController,beanAspect,org.springframework.aop .config.internalAutoProxyCreator,method1,org.sprin gframework.aop.aspectj.AspectJExpressionPointcut,o rg.springframework.aop.aspectj.AspectJPointcutAdvi sor,viewResolver]; root of BeanFactory hierarchy>
      2006-11-29 14:14:32,803 INFO [org.springframework.web.context.support.XmlWebAppl icationContext] - <7 beans defined in application context [Root WebApplicationContext]>
      2006-11-29 14:14:32,850 INFO [org.springframework.aop.framework.DefaultAopProxyF actory] - <CGLIB2 not available: proxyTargetClass feature disabled>
      2006-11-29 14:14:33,007 INFO [org.springframework.web.context.support.XmlWebAppl icationContext] - <Bean 'org.springframework.aop.config.internalAutoProxyC reator' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)>
      2006-11-29 14:14:33,007 INFO [org.springframework.web.context.support.XmlWebAppl icationContext] - <Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMess ageSource@1ebf4ff]>
      2006-11-29 14:14:33,022 INFO [org.springframework.web.context.support.XmlWebAppl icationContext] - <Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicatio nEventMulticaster@d1918a]>
      2006-11-29 14:14:33,022 INFO [org.springframework.ui.context.support.UiApplicati onContextUtils] - <Unable to locate ThemeSource with name 'themeSource': using default [org.springframework.ui.context.support.ResourceBun dleThemeSource@455aa8]>
      2006-11-29 14:14:33,022 INFO [org.springframework.beans.factory.support.DefaultL istableBeanFactory] - <Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [graphController,beanAspect,org.springframework.aop .config.internalAutoProxyCreator,method1,org.sprin gframework.aop.aspectj.AspectJExpressionPointcut,o rg.springframework.aop.aspectj.AspectJPointcutAdvi sor,viewResolver]; root of BeanFactory hierarchy]>
      2006-11-29 14:14:33,616 INFO [org.springframework.web.context.ContextLoader] - <Using context class [org.springframework.web.context.support.XmlWebAppl icationContext] for root WebApplicationContext>
      2006-11-29 14:14:33,616 INFO [org.springframework.web.context.ContextLoader] - <Root WebApplicationContext: initialization completed in 1672 ms>
      2006-11-29 14:16:08,882 INFO [org.springframework.web.portlet.DispatcherPortlet] - <Initializing portlet 'aspectController'>
      2006-11-29 14:16:08,913 INFO [org.springframework.web.portlet.DispatcherPortlet] - <FrameworkPortlet 'aspectController': initialization started>
      2006-11-29 14:16:08,913 INFO [org.springframework.beans.factory.xml.XmlBeanDefin itionReader] - <Loading XML bean definitions from PortletContext resource [/WEB-INF/aspectController-portlet.xml]>
      2006-11-29 14:16:08,975 INFO [org.springframework.web.portlet.context.XmlPortlet ApplicationContext] - <Bean factory for application context [PortletApplicationContext for namespace 'aspectController-portlet']: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [graphController,beanAspect,org.springframework.aop .config.internalAutoProxyCreator,callMeBefore,org. springframework.aop.aspectj.AspectJExpressionPoint cut,org.springframework.aop.aspectj.AspectJPointcu tAdvisor,portletModeHandlerMapping]; parent: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [graphController,beanAspect,org.springframework.aop .config.internalAutoProxyCreator,method1,org.sprin gframework.aop.aspectj.AspectJExpressionPointcut,o rg.springframework.aop.aspectj.AspectJPointcutAdvi sor,viewResolver]; root of BeanFactory hierarchy>
      2006-11-29 14:16:08,991 INFO [org.springframework.web.portlet.context.XmlPortlet ApplicationContext] - <7 beans defined in application context [PortletApplicationContext for namespace 'aspectController-portlet']>
      2006-11-29 14:16:08,991 INFO [org.springframework.web.portlet.context.XmlPortlet ApplicationContext] - <Bean 'org.springframework.aop.config.internalAutoProxyC reator' is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)>
      2006-11-29 14:16:08,991 INFO [org.springframework.web.portlet.context.XmlPortlet ApplicationContext] - <Unable to locate MessageSource with name 'messageSource': using default [org.springframework.context.support.DelegatingMess ageSource@dbb335]>
      2006-11-29 14:16:08,991 INFO [org.springframework.web.portlet.context.XmlPortlet ApplicationContext] - <Unable to locate ApplicationEventMulticaster with name 'applicationEventMulticaster': using default [org.springframework.context.event.SimpleApplicatio nEventMulticaster@1d281f1]>
      2006-11-29 14:16:08,991 INFO [org.springframework.beans.factory.support.DefaultL istableBeanFactory] - <Pre-instantiating singletons in factory [org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [graphController,beanAspect,org.springframework.aop .config.internalAutoProxyCreator,callMeBefore,org. springframework.aop.aspectj.AspectJExpressionPoint cut,org.springframework.aop.aspectj.AspectJPointcu tAdvisor,portletModeHandlerMapping]; parent: org.springframework.beans.factory.support.DefaultL istableBeanFactory defining beans [graphController,beanAspect,org.springframework.aop .config.internalAutoProxyCreator,method1,org.sprin gframework.aop.aspectj.AspectJExpressionPointcut,o rg.springframework.aop.aspectj.AspectJPointcutAdvi sor,viewResolver]; root of BeanFactory hierarchy]>
      2006-11-29 14:16:09,335 INFO [org.springframework.web.portlet.DispatcherPortlet] - <Using context class 'org.springframework.web.portlet.context.XmlPortle tApplicationContext' for portlet 'aspectController'>
      2006-11-29 14:16:09,335 INFO [org.springframework.web.portlet.DispatcherPortlet] - <Unable to locate PortletMultipartResolver with name 'portletMultipartResolver': no multipart request handling provided>
      2006-11-29 14:16:09,335 INFO [org.springframework.web.portlet.DispatcherPortlet] - <No HandlerAdapters found in portlet 'aspectController': using default>
      2006-11-29 14:16:09,350 INFO [org.springframework.web.portlet.DispatcherPortlet] - <FrameworkPortlet 'aspectController': initialization completed in 437 ms>
      2006-11-29 14:16:09,350 INFO [org.springframework.web.portlet.DispatcherPortlet] - <Portlet 'aspectController' configured successfully>
      2006-11-29 14:16:09,382 INFO [com.amp.test.aspect.GraphController] - <################################################# ##############>
      2006-11-29 14:16:09,382 INFO [com.amp.test.aspect.GraphController] - <Inside GraphController.handleRenderRequestInternal() ! ! ! >
      2006-11-29 14:16:09,382 INFO [com.amp.test.aspect.GraphController] - <################################################# ############>
      2006-11-29 14:16:09,382 WARN [org.springframework.web.servlet.view.InternalResou rceViewResolver] - <View caching is SWITCHED OFF -- DEVELOPMENT SETTING ONLY: This can severely impair performance>

      Comment


      • #4
        HelloAspectJ.aj? I mean not HelloAspect.java? How are you compiling HelloAspectJ.aj? Also, you seem to have two instances of beanAspect.

        It may be best if you can create a standalone project and post it.

        -Ramnivas

        Comment


        • #5
          Hi Ramnivas,

          Yes its HelloFromAspectJ.aj.

          Well my requirement is to develop a single portlet using Spring2.0 and infuse aspectJ in it do the logging and exception logging. For the moment im just trying to do logging and i am facing this problem.

          What i am doing here is , im creating a portlet application using eclipse and then added my HelloFromAspectJ.aj and converted my portlet application to AspectJ project using an option to convert to AspectJ provided by eclipse and tried deploying the application. I am able to deploy the application and the portlet is up, but the logging is not happening as desired. That is "the system.out.println in my HelloFromAspectJ.aj" is not getting printed. Please help me out.

          If you have a sample portlet application using aspectJ, can you please post it here or send me, it would be very helpful to me :-)

          Thanks in advance Ramnivas

          Comment


          • #6
            Pradeep,

            Unfortunately, I don't have a portlet application with AspectJ.

            I have a feeling that the issue here is not the portlet environemtn. I suggest that you create a JUnit test case, instantiate GraphController in it, and call methods on it. This should test correctness of your pointcut and project configuration.

            One more point: As you are using an AspectJ project (thus the AspectJ compiler), you shouldn't need <aop:aspect id="helloFromAspectJ" ref="beanAspect"> etc. in your configuration at all.

            -Ramnivas

            Comment


            • #7
              Hi Ramnivas,

              Thanks for your suggestion.

              Do you have a sample Junit test case ? Can you please explain how to go about doing with the Junit test case ?

              Comment


              • #8
                This is really not much about Spring, but rather about JUnit and unit testing. Here is one such source:
                http://www.springframework.org/docs/...ep-Part-3.html

                -Ramnivas

                Comment


                • #9
                  Hi Ramnivas,

                  Thanks a lot for the source. But my requirement is just to develop a simple hello world portlet application using spring2.0 and to implement logging using aspectJ . I am not able to find one good example for implementing logging using aspectJ in Spring portlet.

                  A sample spring portlet example with aspectJ logging would be of real good help to me.

                  Comment

                  Working...
                  X