Announcement Announcement Module
Collapse
No announcement yet.
Aspect not called Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Aspect not called

    Hello,

    I'm new to Spring and just start playing with some examples. I built a very simple Web application and created one aspect to be invoked for every method of the controller interface.

    But it never gets called. Would someone please have a look at the following code and give some advice please!

    Thanks very much!
    Code:
    In web.xml:
    
    <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/applicationContext-jdbc.xml</param-value>
        </context-param>
    
    <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    The controller class:
    
    package com.acme.springapp.controller;
    
    public class ProductsController implements Controller {
    
        private ProductManager productManager;
    
        private String successView;
    
        public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
            return new ModelAndView(getSuccessView(), "model", this.getProductManager());
        }
    
    
    The Aspect class:
    
    (at)Aspect
    public class ControllerLoggingAspect {
        private Log logger = LogFactory.getLog(this.getClass());
    
        (at)Before("execution(* org.springframework.web.servlet.mvc.Controller.*(..))")
        public void logBefore() {
            logger.info("Handle request begins");
        }
    }
    
    In applicationContext-jdbc.xml:
    
    <aop:aspectj-autoproxy/>
    <bean class="com.acme.springapp.aspect.ControllerLoggingAspect"/>

  • #2
    Aspects are only applied in the ApplicationContext they are defined in. Yours is defined in the root application context (loaded by the ContextLoaderListener) and your controller is probably loaded by the one loaded from the DispatcherServlet. The parent applicationcontext doesn't know about it's childs contexts.

    Comment


    • #3
      Originally posted by Marten Deinum View Post
      Aspects are only applied in the ApplicationContext they are defined in. Yours is defined in the root application context (loaded by the ContextLoaderListener) and your controller is probably loaded by the one loaded from the DispatcherServlet. The parent applicationcontext doesn't know about it's childs contexts.
      Thanks a lot! I was expecting to spend a whole night working on this.

      Comment


      • #4
        Originally posted by Marten Deinum View Post
        Aspects are only applied in the ApplicationContext they are defined in. Yours is defined in the root application context (loaded by the ContextLoaderListener) and your controller is probably loaded by the one loaded from the DispatcherServlet. The parent applicationcontext doesn't know about it's childs contexts.
        Hi Marten,

        I have the same problem as bqiao, but I put the aspect in the same context of my controllers.

        My aspect is as follow:

        Code:
        package it.esel.ge.aop;
        
        import org.aspectj.lang.ProceedingJoinPoint;
        import org.aspectj.lang.annotation.Around;
        import org.aspectj.lang.annotation.Aspect;
        import org.aspectj.lang.annotation.Pointcut;
        
        @Aspect
        public class ValidUserSessionAdvice {
            
            @Pointcut("execution(* *..handleRequest(..))")
            public void inControllers() {}
            
            @Around("inControllers()")
            public Object doValidation(ProceedingJoinPoint pjp) throws Throwable {
                Object[] arguments = pjp.getArgs();
                System.out.println(arguments[0].getClass().getName());
                return pjp.proceed(arguments);
            }
        
        }
        My controllers and the aspect are defined in <webapp name>-servlet.xml:

        Code:
        <!--
          - DispatcherServlet application context for the Spring web MVC
          - implementation of AnagrafeWeb's web tier.
          -->
        <beans>
        
            <!-- ========================= VIEW DEFINITIONS ========================= -->
        
            <bean id="viewResolver1" class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
                <property name="basename" value="views"/>
            </bean>
        
            <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
                <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
                <property name="prefix" value="/WEB-INF/jsp/"/>
                <property name="suffix" value=".jsp"/>
            </bean>
        
        ...
        
            <bean name="/ricerca.do" class="it.esel.ge.controllers.RicercaController">
                <property name="tableJoinsService" ref="tableJoins" />
            </bean>
        
        ...
        
            <bean id="validUserSessionAdvice" class="it.esel.ge.aop.ValidUserSessionAdvice" />
        
        <beans>
        The aspectj-autoproxy it's enabled into the ApplicationContext (<aop:aspectj-autoproxy />).

        The above controller is as follow:

        Code:
        package it.esel.ge.controllers;
        
        ...
        import it.esel.ge.controllers.base.AWController;
        ...
        
        public class RicercaController extends AWController implements IController {
        
            private final static String QUERY_INFO_MESSAGE = "Query ricerca dati: ";
            
        ...
            private IJoinsService tableJoinsService;
                
            /**
             * @author Riccardo Foraf˛
             * @since Mar 7, 2008
             *
             * @param tableJoinsService
             */
            public void setTableJoinsService(IJoinsService tableJoinsService) {
                this.tableJoinsService = tableJoinsService;
            }
            
            /* 
             * Riccardo Foraf˛
             * Mar 7, 2008
             *
             * (non-Javadoc)
        * @see it.esel.ge.controllers.base.AWController#handleRequest(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
             */
            public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
        
                Map<Object, Object> model = new HashMap<Object, Object>();
                ModelAndView modelAndView = null;
        ...
        and it's extend a base controller that implemets the Controller interface:

        Code:
        package it.esel.ge.controllers.base;
        
        import org.springframework.web.servlet.mvc.Controller;
        ...
        public class AWController extends ApplicationObjectSupport implements Controller {
        
            protected static ReloadableResourceBundleMessageSource propertiesRepository;
            protected static MessageSource staticPropertiesRepository;
        ...    
            public AWController() {
                super();
            }
            
        ...    
            public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {
                return null;
            }
        
        ...
        I'm not able to understand what I'm doing wrong.

        Could you help me please?

        Thank you.

        Riccardo.
        Last edited by ricforaf; Sep 12th, 2008, 07:53 AM. Reason: Corrections

        Comment


        • #5
          Try adding @Component to your aspect;
          Code:
          @Component
          @Aspect
          public class ValidUserSessionAdvice {

          Comment


          • #6
            Originally posted by Marten Deinum View Post
            Aspects are only applied in the ApplicationContext they are defined in. Yours is defined in the root application context (loaded by the ContextLoaderListener) and your controller is probably loaded by the one loaded from the DispatcherServlet. The parent applicationcontext doesn't know about it's childs contexts.
            Marten, what are the implications of what you're saying if I don't have a configuration file for the DispatcherServlet?
            Code:
            <?xml version="1.0" encoding="UTF-8"?>
            
            <web-app
                id="WebApp_ID" version="2.5"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xmlns="http://java.sun.com/xml/ns/javaee"
                xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
                xsi:schemaLocation="
                    http://java.sun.com/xml/ns/javaee
                    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
                <display-name>people_locator</display-name>
            
                <context-param>
                    <param-name>contextConfigLocation</param-name>
                    <param-value>
                        /WEB-INF/config/web-app-context.xml
                    </param-value>
                </context-param>
            
                <listener>
                    <listener-class>
                        org.springframework.web.context.ContextLoaderListener
                    </listener-class>
                </listener>
            
                <listener>
                    <listener-class>
                        org.springframework.web.context.request.RequestContextListener
                    </listener-class>
                </listener>
            
                <servlet>
                    <servlet-name>SpringMvcDispatcherServlet</servlet-name>
            
                    <servlet-class>
                        org.springframework.web.servlet.DispatcherServlet
                    </servlet-class>
            
                    <init-param>
                        <param-name>contextConfigLocation</param-name>
                        <param-value />
                    </init-param>
            
                    <load-on-startup>1</load-on-startup>
                </servlet>
            
                <servlet-mapping>
                    <servlet-name>SpringMvcDispatcherServlet</servlet-name>
            
                    <url-pattern>*.zug</url-pattern>
                </servlet-mapping>
            </web-app>
            I started doing this after I noticed that's how they do it in the Spring Web Flow examples. It makes things simpler.

            Comment

            Working...
            X