2011-06-22 16 views
0

を発射しませhttp://www.vaannila.com/spring/spring-interceptor.html春インターセプター、ここで説明したように、私はログを実装しようとしています

これは、ハンドラーマッピングファイルである:ここで

<?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:p="http://www.springframework.org/schema/p" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:oxm="http://www.springframework.org/schema/oxm" 
xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context-3.0.xsd 
     http://www.springframework.org/schema/oxm 
     http://www.springframework.org/schema/oxm/spring-oxm-3.0.xsd"> 

    <bean id="beanNameResolver" 
     class="org.springframework.web.servlet.view.BeanNameViewResolver"/> 

    <bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping" 
    p:interceptors-ref="loggerInterceptor" /> 
    <context:component-scan base-package="com.audiClave.controllers" /> 
    <bean id="loggerInterceptor" class="com.audiClave.controllers.LoggerInterceptor" /> 

</beans> 

はインターセプタです:

package com.audiClave.controllers; 

... 

public class LoggerInterceptor extends HandlerInterceptorAdapter { 

static Logger logger = Logger.getLogger(LoggerInterceptor.class); 

static{ 
    BasicConfigurator.configure(); 
      logger.setLevel((Level)Level.INFO); 
} 

@Override 
public boolean preHandle(HttpServletRequest request, 
     HttpServletResponse response, Object handler) throws Exception { 
    logger.info("Before handling the request"); 
    return super.preHandle(request, response, handler); 
} 

@Override 
public void postHandle(HttpServletRequest request, 
     HttpServletResponse response, Object handler, 
     ModelAndView modelAndView) throws Exception { 
    logger.info("After handling the request"); 
    super.postHandle(request, response, handler, modelAndView); 
} 

@Override 
public void afterCompletion(HttpServletRequest request, 
     HttpServletResponse response, Object handler, Exception ex) 
     throws Exception { 
    logger.info("After rendering the view"); 
    super.afterCompletion(request, response, handler, ex); 
} 
} 

次のメッセージコンソールに表示されます:

Mapping [/REST/en/actions] to HandlerExecutionChain with handler [[email protected]] and 3 interceptors 

コントローラーは呼び出されますが、インターセプターは呼び出されません。 なぜインターセプタは呼び出されませんか?私は春3.0.5を使用しています

すべてのイベントにデバッグブレークポイントを設定しようとしましたが、何も起動しませんでした。ロギングをINFOに設定しましたが、出力はありません。

loggerInterceptorがあるため、次のログ文のピックアップされている:

2011-06-22 21:11:39,828 [ContainerBackgroundProcessor[StandardEngine[Catalina]]] INFO org.springframework.beans.factory.support.DefaultListableBeanFactory - Destroying singletons in org.s[email protected]f2ea42: defining beans [beanNameResolver,org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping#0,baseController,restController,org.springframework.context.annotation.internalConfigurationAnnotationProcessor,org.springframework.context.annotation.internalAutowiredAnnotationProcessor,org.springframework.context.annotation.internalRequiredAnnotationProcessor,org.springframework.context.annotation.internalCommonAnnotationProcessor,loggerInterceptor]; root of factory hierarchy 

たぶんクラスがリストに間違って配置されています?

+0

試みはあなたのようになります。http://www.vaannila.com/spring/spring-interceptors.html –

+0

は、あなたが確信していますロガーのINFOレベルが有効になっていますか? (ほとんど同じコードを持っています - それは働いています) –

+0

ありがとうございますが、私はファイルを似たように調整しましたが、インターセプタはまだ呼び出されません。 –

答えて

0

働い以下のとおりです。次のリンクで提供された例を使用して

<?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:p="http://www.springframework.org/schema/p" 
xmlns:mvc="http://www.springframework.org/schema/mvc" 
xmlns:context="http://www.springframework.org/schema/context" 
xsi:schemaLocation=" 
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

<bean id="loggerInterceptor" class="com.audiClave.controllers.LoggerInterceptor" /> 
<bean id="handlerMapping" class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" 
    p:interceptors-ref="loggerInterceptor" /> 
<!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

<!-- Scans within the base package of the application for @Components to configure as beans --> 
<!-- @Controller, @Service, @Configuration, etc. --> 
<context:component-scan base-package="com.audiClave.controllers" /> 

</beans> 
1

これらは助けるが、あなたの春バージョンと一致するのschemaLocationを交換

+0

こんにちは、入力のおかげで、あなたの提案を試みたが運がない。 –

関連する問題