# ArgumentResolver ํ™œ์šฉ
Study Repository

ArgumentResolver ํ™œ์šฉ

by rlaehddnd0422

https://rlaehddnd0422.tistory.com/61#article-3-1-1----argumentresolver

 

Spring MVC ๊ธฐ๋ณธ ๊ธฐ๋Šฅ - ์‘๋‹ต ๋งคํ•‘ @ResponseBody, MessageConverter, ์š”์ฒญ ๋งคํ•‘ ํ•ธ๋“ค๋Ÿฌ ์–ด๋Œ‘ํ„ฐ ๊ตฌ์กฐ

HTTP Response - ์ •์  ๋ฆฌ์†Œ์Šค, ๋ทฐ ํ…œํ”Œ๋ฆฟ ์Šคํ”„๋ง์—์„œ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๋งŒ๋“œ๋Š” ๋ฐฉ๋ฒ•์€ 3๊ฐ€์ง€์ž…๋‹ˆ๋‹ค. 1. ์ •์  ๋ฆฌ์†Œ์Šค ์›น ๋ธŒ๋ผ์šฐ์ €์— ์ •์ ์ธ HTML, css, js๋ฅผ ์ œ๊ณตํ•  ๋•Œ๋Š” ์ •์  ๋ฆฌ์†Œ์Šค๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์Šคํ”„๋ง ๋ถ€

rlaehddnd0422.tistory.com

 

ArgumentResolver๋ฅผ ํ™œ์šฉํ•ด ์„ธ์…˜์„ ๊ฐ€์ง„ ํšŒ์›๋งŒ login ํ™ˆ ํ™”๋ฉด์œผ๋กœ ์ด๋™,

                                               ์„ธ์…˜์ด ์—†๋Š” ํšŒ์›์€ ํ™ˆ ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•˜๋„๋ก ์„ค์ •ํ•ด๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค.

 

@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface Login {
}

@Login ์–ด๋…ธํ…Œ์ด์…˜์„ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ์–ด๋…ธํ…Œ์ด์…˜ ์ƒ์„ฑ  

 

@GetMapping("/")
public String homeLoginV3ArgumentResolver(@Login Member loginMember, Model model)
{
    // ์„ธ์…˜์— ํšŒ์› ๋ฐ์ดํ„ฐ๊ฐ€ ์—†์œผ๋ฉด home์œผ๋กœ ์ด๋™
    if(loginMember==null)
    {
        return "home";
    }

    // ์„ธ์…˜์ด ์œ ์ง€๋˜๋ฉด ๋กœ๊ทธ์ธ์œผ๋กœ ์ด๋™
    model.addAttribute("member",loginMember);
    return "loginHome";
}

์ปจํŠธ๋กค๋Ÿฌ์˜ @Login Member loginMember ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด ArgumentResolver๋ฅผ ์ถ”๊ฐ€ํ–ˆ์Šต๋‹ˆ๋‹ค.

@Slf4j
public class LoginMemberArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter parameter) {
        log.info("supportsParameter ์‹คํ–‰");

        boolean hasLoginAnnotation = parameter.hasParameterAnnotation(Login.class);
        boolean hasMemberType = Member.class.isAssignableFrom(parameter.getParameterType());

        // @Login ์–ด๋…ธํ…Œ์ด์…˜์ด๊ณ  && ํƒ€์ž…์ด Member์—ฌ์•ผ resolveArguemnt๋ฅผ ์‚ฌ์šฉ ๊ฐ€๋Šฅ
        return hasLoginAnnotation && hasMemberType;
    }

    @Override
    public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer,
                                  NativeWebRequest webRequest, WebDataBinderFactory binderFactory)
            throws Exception {
        log.info("resolverArgument ์‹คํ–‰");

        HttpServletRequest request = (HttpServletRequest) webRequest.getNativeRequest();
        HttpSession session = request.getSession();

        if(session==null || session.getAttribute(SessionConst.LOGIN_MEMBER)==null)
        {
            return null;
        }

        Object member = session.getAttribute(SessionConst.LOGIN_MEMBER);

        return member;
    }
}
  • ArgumentResolver๋ฅผ ์ถ”๊ฐ€ํ•˜๊ธฐ ์œ„ํ•ด์„œ HandlerMethodArgumentResolver ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค!
  • boolean supportsParameter(MethodParameter parameter) : parameter์— ์–ด๋…ธํ…Œ์ด์…˜ @Login์ด ๋ถ™์–ด์žˆ๊ณ , Member ํƒ€์ž…์ธ ๊ฒฝ์šฐ ์ด ArgumentResolver๋ฅผ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค. ์„ค์ •ํ•œ ์กฐ๊ฑด์ด true์ธ ๊ฒฝ์šฐ์—๋งŒ resolverArgument ๋ฉ”์†Œ๋“œ ์‚ฌ์šฉ ๊ฐ€๋Šฅ.
  • resolveArgument(...) : ์–ด๋–ค ์กฐ๊ฑด์˜ Object๋ฅผ ๋ฆฌํ„ดํ•  ์ง€ ์ž‘์„ฑ. ์ด ์ฝ”๋“œ์—์„œ๋Š” ๋กœ๊ทธ์ธํ•œ ํšŒ์›์— ๋Œ€ํ•œ ์„ธ์…˜์ด ์žˆ์œผ๋ฉด ํ•ด๋‹น ํšŒ์›์„ ๋ฆฌํ„ดํ•ฉ๋‹ˆ๋‹ค.
@Configuration
public class WebConfig implements WebMvcConfigurer {
    
    @Override
    public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
        resolvers.add(new LoginMemberArgumentResolver());
    }
}

๋งˆ์ง€๋ง‰์œผ๋กœ ๋งŒ๋“ค์—ˆ๋˜ ArgumentResolver๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” WebMvcConfigurer๋ฅผ ์ƒ์†๋ฐ›์•„ ArgumentResolver๋ฅผ addArgumentResolvers() ๋ฉ”์†Œ๋“œ๋ฅผ ํ†ตํ•ด ๋ฐ˜๋“œ์‹œ ๋“ฑ๋กํ•ด ์ฃผ์–ด์•ผํ•ฉ๋‹ˆ๋‹ค.

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

Study Repository

rlaehddnd0422

ํ™œ๋™ํ•˜๊ธฐ