为什么Spring拦截器会调用两次?

时间:2015-08-25 04:44:37

标签: spring logging interceptor log4j2

为什么Spring拦截器在这种情况下调用了两次?

控制器

public class UserController
{
    @Resource(name="userDAO")
    private UserDAO userDAO;

    @RequestMapping("/user/registUser")
    public String registUser(HttpServletRequest request, Model model)
    {
        String message = "Regist User Page!";

        model.addAttribute("message", message);

        return "/user/registUser";
    }
}

拦截

public class LoggerInterceptor extends HandlerInterceptorAdapter
{
    final protected Logger logger = LogManager.getLogger(LoggerInterceptor.class);

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception
    {
        logger.debug("===============================================START================================================");
        logger.debug("Request URI : " + request.getRequestURI());

        return super.preHandle(request, response, handler);
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception
    {
        logger.debug("================================================END=================================================");

        super.postHandle(request, response, handler, modelAndView);
    }
}

上下文servlet.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:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
       http://www.springframework.org/schema/context
       http://www.springframework.org/schema/context/spring-context-4.0.xsd
       http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc.xsd">

    <context:component-scan base-package="com.mamoru.firstweb.*"/>

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/user/**"/>
            <bean id="loggerInterceptor" class="com.mamoru.firstweb.common.logger.LoggerInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>

    <bean id="viewResolverTiles" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.tiles3.TilesView"/>
        <property name="contentType" value="text/html; charset=UTF-8"/>
        <property name="order" value="1"/>
    </bean>

    <bean id="viewResolverJstl" class="org.springframework.web.servlet.view.UrlBasedViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
        <property name="contentType" value="text/html; charset=UTF-8"/>
        <property name="prefix" value="/jsp/"/>
        <property name="suffix" value=".jsp"/>
        <property name="order" value="2"/>
    </bean>

    <bean id="tilesConfigurer" class="org.springframework.web.servlet.view.tiles3.TilesConfigurer">
        <property name="definitions">
            <list>
                <value>/config/tiles/tiles-*.xml</value>
            </list>
        </property>
    </bean>

    <mvc:resources mapping="/res/**" location="/resources/" />

    <mvc:annotation-driven />

</beans>

我将Log4j2用于此项目。日志在文本下。 我想,也许拦截器叫了两次。

日志

    [DEBUG] 13:43:17.693 [http-apr-8080-exec-9]  - ===============================================START================================================
[DEBUG] 13:43:17.693 [http-apr-8080-exec-9]  - ===============================================START================================================
[DEBUG] 13:43:17.695 [http-apr-8080-exec-9]  - Request URI : /user/registUser.do
[DEBUG] 13:43:17.695 [http-apr-8080-exec-9]  - Request URI : /user/registUser.do
[DEBUG] 13:43:17.709 [http-apr-8080-exec-9]  - ================================================END=================================================
[DEBUG] 13:43:17.709 [http-apr-8080-exec-9]  - ================================================END=================================================

为什么拦截器会调用两次? 我想只召唤一次inteceptor。

我添加了我的log4j2.xml。

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

<Configuration>
    <Appenders>
        <File name="FileLog" fileName="/logs/firstweb.log">
            <PatternLayout pattern="[%-5level] %d{HH:mm:ss.SSS} [%t] - %msg%n"/>
        </File>
        <Console name="ConsoleLog" target="SYSTEM_OUT">
            <!--<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>-->
            <PatternLayout pattern="[%-5level] %d{HH:mm:ss.SSS} [%t]  - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="com.mamoru.firstweb.common.logger.LoggerInterceptor" level="TRACE">
            <AppenderRef ref="FileLog"/>
            <AppenderRef ref="ConsoleLog"/>
        </Logger>
        <Root level="all">
            <AppenderRef ref="FileLog"/>
            <AppenderRef ref="ConsoleLog"/>
        </Root>
    </Loggers>
</Configuration>

1 个答案:

答案 0 :(得分:0)

我们面临两次被拦截器调用的相同问题,这是因为我们同时使用了applicationContext.xml和@Configuration

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

    let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "PhotosCollectionViewCell", for: indexPath) as! PhotosCollectionViewCell

    cell.OnDeleteTapped = { cell in
          // Do Anything Here is Your Action Event
    }

    return cell
}