티스토리 뷰

Programming/Spring

Interceptor

Albothyl 2019. 3. 27. 20:51

1. create some interceptor class

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.WebContentInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Slf4j
public class SomeInterceptor extends WebContentInterceptor {

    @Autowired
    private SomeRequestProxy someRequestProxy;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        log.info("SomeInterceptor preHandle");
        someRequestProxy.setPre("SomeInterceptor setPre");
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        log.info("SomeInterceptor postHandle");
        someRequestProxy.setPost("SomeInterceptor setPost");
        log.info(someRequestProxy.toString());
    }
}

2. create some interceptor bean

@Bean
public SomeInterceptor someInterceptor() {
    return new SomeInterceptor();
}

 

3. regiest some interceptors

public class SomeConfiguration extend WebMvcConfigurerAdapter {
    ...

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(someInterceptor());
    }
}

Interceptor는 controller 이전 / 이후 단계에서 특정 데이터나 비지니스를 처리할 경우 사용한다.

'Programming > Spring' 카테고리의 다른 글

AOP  (0) 2019.03.27
Request Proxy  (0) 2019.03.27
Argument Resolver  (0) 2019.03.27
DI (Dependency Injection)  (2) 2018.11.01
Spring Application Start Flow  (0) 2018.10.06
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
«   2024/11   »
1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
글 보관함