๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’Š Java & Kotlin & Spring/- spring framework +

[Spring-Core] ์Šคํ”„๋ง์˜ ์ปค๋งจ๋“œ ๊ฐ์ฒด(Command Object)๋ฅผ ๋น ๋ฅด๊ณ  ๊ฐ„๋‹จํ•˜๊ฒŒ ์•Œ์•„๋ณด์ž.

by Wonit 2020. 7. 30.

์˜ค๋Š˜ ์•Œ์•„๋ณผ ๊ฒƒ์€ Spring-Command Object์ด๋‹ค.

 

์ปค๋งจ๋“œ ๊ฐ์ฒด๋ฅผ ์„ค๋ช…ํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” Spring์„ ์‚ฌ์šฉํ•˜๊ธฐ ์ „์ด๋ผ๋Š” ๊ฐ€์ •์„ ํ•˜๊ณ  ๋ถˆํŽธํ•จ์„ ๋จผ์ € ์ƒ๊ฐํ•ด๋ณด๊ณ  Spring์ด ์ด ๋ถˆํŽธํ•จ์„ ์–ด๋–ป๊ฒŒ ํ•ด๊ฒฐํ•ด๋‚˜๊ฐ€๋ ค๊ณ  ํ•˜๋Š”์ง€์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด๋„๋กํ•˜์ž.

๋ถˆํŽธํ•จ : ๊ธฐ์กด์˜ Http ์š”์ฒญ

์šฐ๋ฆฌ๊ฐ€ ๋งŒ์•ฝ request๋กœ 100๊ฐ€์ง€์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๋Š”๋‹ค๊ณ  ๊ฐ€์ •ํ•ด๋ณด์ž.

public String joinMember(HttpServletRequest request) {

        String username = request.getParameter("username");

        String password = request.getParameter("password");

        String address = request.getParameter("address");

        String phone = request.getParameter("phone");

        String part = request.getParameter("part");

        String name = request.getParameter("name");

        String party =request.getParameter("party");

        //... 100๊ฐ€์ง€๋ผ๊ณ  ๊ฐ€์ •

        return "";
    }

๊ทธ๋Ÿผ ์šฐ๋ฆฌ๋Š” request์— ๋Œ€ํ•œ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๋ฅผ ์กฐ์‚ฌํ•ด์„œ ๋„ฃ์–ด์•ผ ํ•œ๋‹ค.

 

๊ทธ๋Ÿผ 1000๊ฐ€์ง€์˜ ์š”์ฒญ์ด๋ฉด 1000๊ฐ€์ง€์˜ ๋ชจ๋“  ํ”„๋กœํผํ‹ฐ๋ฅผ ์žก์•„์„œ ๋„ฃ์–ด์•ผ๋œ๋‹ค.

 

์—ฌ๊ธฐ์„œ ์ปค๋งจ๋“œ ๊ฐ์ฒด๋ผ๋Š” ๊ฐœ๋…์ด ๋“ฑ์žฅํ•œ๋‹ค.

ํ•ด๊ฒฐ

์ปค๋งจ๋“œ ๊ฐ์ฒด๋Š” ์šฐ๋ฆฌ๊ฐ€ ์ผ๋ฐ˜์ ์œผ๋กœ ์‚ฌ์šฉํ•˜๋Š” getter/setter ๋ฉ”์„œ๋“œ๊ฐ€ ์žˆ๋Š” VO๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋œ๋‹ค.

 

์šฐ๋ฆฌ๋Š” ๋งค๊ฐœ๋ณ€์ˆ˜๋กœ ๊ฐ์ฒด๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๊ณ  ๊ฐ์ฒด์˜ ์ƒ์„ฑ์ž, ํ˜น์€ getter/setter๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ObjectMapper๋ฅผ ํ†ตํ•ด ๋ฐ”์ธ๋”ฉ์„ ์‹œํ‚ค๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.


๊ทธ๋Ÿผ ์šฐ๋ฆฐ ์œ„์—์„œ ๋ดค๋˜ ๋ถˆํŽธํ•œ ์ฝ”๋“œ๋ฅผ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊ฐ„๊ฒฐํ•˜๊ฒŒ ๊ตฌ์„ฑํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

public String joinMember (User user) {
        joinService.register(user.getName(), user.getPassword, user.getAddress ...);
}

์ฃผ์˜ํ•˜์ž

์šฐ๋ฆฌ๋Š” ๊ฐ์ฒด๋ผ๋Š” ๊ฒƒ์„ ์ฝ”๋“œ๋ฅผ ๋ณด๊ณ  ๋”ฑ ์•Œ ์ˆ˜ ์žˆ์ง€๋งŒ, Spring์€ ์šฐ๋ฆฌ์ฒ˜๋Ÿผ ์ง๊ด€์ ์ธ ์ƒ๊ฐ์ด ๋ถˆ๊ฐ€๋Šฅํ•˜๋‹ค.


๋”ฐ๋ผ์„œ CommandObject๋Š” Bindingํ•˜๋ ค๋Š” ๊ฐ์ฒด์˜ property name๊ณผ ๋™์ผํ•ด์•ผ ์„œ๋กœ ๋ฐ”์ธ๋”ฉ์„ ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.

๋Œ“๊ธ€