当前位置: 代码迷 >> Java相关 >> spring mvc 中如何使用aop拦截到@controller?
  详细解决方案

spring mvc 中如何使用aop拦截到@controller?

热度:620   发布时间:2013-06-18 10:24:30.0
spring mvc 中如何使用aop拦截到@controller?
spring mvc 中@controller注解在初始化时被代理,请教一下如何使用aop拦截到@controller,有说实现HanlderInterceptorAdapter的,请具体指教一下,新人上路多多关照...
搜索更多相关的解决方案: 如何  spring  

----------------解决方案--------------------------------------------------------
是要使用拦截器吗?
----------------解决方案--------------------------------------------------------
程序代码:
//配置你的拦截器的bean
<!-- 日志拦截器 -->
  <bean id="loggerInterception" class="web.interceptor.LoggerInterception" />//class填写地址

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <list>
                <ref bean="loggerInterception"/>
            </list>
        </property>
    </bean>

//------------------------分割线-----------------------

public class LoggerInterception extends HandlerInterceptorAdapter{
    private static Log log = Log.getLog(LoggerInterception.class);
    @Override
    public void afterCompletion(HttpServletRequest request,
            HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        // TODO Auto-generated method stub
        super.afterCompletion(request, response, handler, ex);
    }

    @Override
    public void postHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler,
            ModelAndView modelAndView) throws Exception {
        log.info("modelAndView:"+modelAndView);
        super.postHandle(request, response, handler, modelAndView);
    }

    @Override
    public boolean preHandle(HttpServletRequest request,
            HttpServletResponse response, Object handler) throws Exception {
        log.info("request:"+request.getRequestURI());
      
        return super.preHandle(request, response, handler);
    }

}

----------------解决方案--------------------------------------------------------
  相关解决方案