!โญ๏ธ๋ชฉ์ฐจ๋ฅผ ํตํด ์์ฐจ์ ์ผ๋ก ๊ฐ๋
์ ํ์
ํ๋ ๊ฒ์ ์ถ์ฒํฉ๋๋ค.
!โญ๏ธ๋ชฉ์ฐจ
Spring Bean์ด ๋ฌด์์ธ์ง ์์๋ณด๊ธฐ
Spring MVC ์ด์ ๋จ๊ณ์ Controller๋ฅผ ์์ฑํ ๋ฐฉ๋ฒ ์์๋ณด๊ธฐ
Spring์์ ์ฃผ๋ก ์ฐ๋ MVC ํจํด๊ณผ ๊ด๋ จ๋ Annotation๋ค ์์๋ณด๊ธฐ
์์ฃผ ์ฌ์ฉํ๋ ๋ฌธ๊ตฌ๋ฅผ ๋ฐ๋ก propertiesํ์ผ๋ก ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ ์์๋ณด๊ธฐ
ํด๋ผ์ด์ธํธ๊ฐ ์์ฒญํ ๋ฐ์ดํฐ๋ฅผ ๊ฒ์ฆํ ์ ์๋ ๋ฐฉ๋ฒ ์์๋ณด๊ธฐ
HttpServletRequest์์ ์ง์ํ๋ HttpSession ์์๋ณด๊ธฐ
๊ณตํต ๊ด์ฌ์ฌ๋ณ๋ก ๋์ผํ ์์
์ ์ํํ๊ฒ ํ๋ ํํฐ์ ์ธํฐ์
ํฐ ์์๋ณด๊ธฐ
์์ธ ํ์ด์ง๋ฅผ ์ ๊ณตํ๋ ๋ฐฉ๋ฒ๊ณผ Rest API์ ์์ธ ์ฒ๋ฆฌ ๋ฐฉ๋ฒ ์์๋ณด๊ธฐ
๋์ผํ ํ์
๋ณํ์ ์ฌ๋ฌ๋ฒ ํด์ผ ํ ๋ ๊ฐ๋จํ๊ฒ ํ ์ ์๋ ๋ฐฉ๋ฒ ์์๋ณด๊ธฐ
Spring์์ ํ
์คํธ, ์ด๋ฏธ์ง ๋ฑ์ ํ์ผ์ ์ ์กํ๋ ๋ฐฉ๋ฒ ์์๋ณด๊ธฐ
์๋ณธ ๋งํฌ
33๊ฑด์ ํญ๋ชฉ
2025๋
2์ 14์ผ
Multipart์ ์ ์ Http์์ฒญ์์ ํ์ผ ์
๋ก๋๋ฅผ ํ ๋ ์ฃผ๋ก ์ฌ์ฉ๋๋ฉฐ, ์์ฒญ ๋ฐ์ดํฐ์ ์๋ ํ์ผ์ด Multipart ํํ๋ก ๋ค์ด์ค๊ฒ ๋ฉ๋๋ค. Multipart์ ์ข
๋ฅ Content-Type์ค๋ช
multipart/form-dataํ์ผ ์
๋ก๋ ์ ์ฌ์ฉ (HTML ํผ์์ <input type=โfileโ>์ ์ฌ์ฉํ ๋)multipart/mixed์๋ก ๋ค๋ฅธ ๋ฐ์ดํฐ ํ์
์ ํฌํจํ ๋ ์ฌ์ฉmultipart/alternative๋์ผํ ๋ด์ฉ์ ์ฌ๋ฌ ํ์(์: HTML, Plain Text)์ผ๋ก ๋ณด๋ผ ๋ ์ฌ์ฉmultipart/related๋ณธ๋ฌธ๊ณผ ์ฐ๊ด๋ ๋ฐ์ดํฐ๋ฅผ ํจ๊ป ๋ณด๋ผ ๋ (์: HTML ๋ณธ๋ฌธ๊ณผ ์ด๋ฏธ์ง ์ฒจ๋ถ) multipart/form-data ์์ POST /upload HTTP/1.1 Host: example.com Content-Type: multipart/form-data; boundary=----WebKitFormBoundary ------WebKitFormBoundary Content-Disposition: form-data; name="username" john_doe ------WebKitFormBoundary Content-Disposition: form-data; name="file"; filename="image.jpg" Content-Type: image/jpeg (binary data) ------WebKitFormBoundary-- Spring์์ Multipart๊ด๋ จ ์ค์ 1.
2025๋
2์ 13์ผ
Formatter์ ์ ์ ์ค์ ๊ฐ๋ฐ ํ๊ฒฝ์์๋ ๋ฌธ์์ด์ ๋ค๋ฅธ ํ์
์ผ๋ก ๋ณํํ๊ฑฐ๋, ๋ค๋ฅธ ํ์
์ ๋ฌธ์์ด๋ก ๋ณํํ๋ ์ํฉ์ด ๋น๋ฒํ ๊ฒ์
๋๋ค. ์ด ๋๋ฌธ์ ๊ฐ์ฒด๋ฅผ ํน์ ํ ํฌ๋งท์ ๋ง์ถ์ด ๋ฌธ์๋ก ์ถ๋ ฅํ๊ฑฐ๋ ๋๋ ๊ทธ ๋ฐ๋์ ์ญํ ์ ํ๋ ๊ฒ์ ํนํ๋ ๊ธฐ๋ฅ์ ๊ฐ์ง Formatter๊ฐ ์๊ฒผ์ต๋๋ค.
2025๋
2์ 13์ผ
Converter์ ์ ์ Spring์์ ๋ณํํด์ผ ํ ํ์
์ ๋ํ ์ ์๋ฅผ ๋ฏธ๋ฆฌ ๊ตฌํํด๋์ ๊ฒ ์ด๋ฅผ ํธ์ถํ์ฌ ์์ฃผ ๋ณํํ๋ ํ์
์ ๋ํด ๊ฐํธํ๊ฒ ํ์
๋ณํ์ ํ ์ ์์ต๋๋ค. Converter ์์ฑ ์์๋ก ip์ port ์ ๋ณด๋ฅผ ์
๋ ฅ๋ฐ์ผ๋ฉด 127.0.0.1:8080๊ณผ ๊ฐ์ ํํ๋ก ํ์
์ ๋ณํํ๊ฑฐ๋ ๊ทธ ๋ฐ๋๋ก ๋ณํํ๋ Converter๋ฅผ ๋ง๋ค์ด๋ณด๊ฒ ์ต๋๋ค.
2024๋
9์ 18์ผ
Spring์์ ๊ธฐ๋ณธ์ ์ผ๋ก ์๋ฌ๊ฐ ๋ฐ์ํ์ ๋ ๊ฐ๋ก์ฑ์ ๋ด๊ฐ ์ํ๋๋๋ก ์ฒ๋ฆฌํด์ค ์ ์๋๋ก ํ๋ ExceptionResolver๋ฅผ ์ง์ํฉ๋๋ค. Spring์ด ๊ธฐ๋ณธ์ ์ผ๋ก ์ง์ํ๋ ExceptionResolver๋ 3์ข
๋ฅ๊ฐ ์์ผ๋ฉฐ, ๊ทธ ์ค ResponseStatusExceptionResolver๋ ์๋ ๋๊ฐ์ง์ ๊ฒฝ์ฐ๋ฅผ ์๋์ผ๋ก ์ฒ๋ฆฌํ ์ ์์ต๋๋ค.
2024๋
9์ 16์ผ
์์ธ ๋ฐ์ ๋ฐฉ๋ฒ ์ฐ์ ์์ธ๋ฅผ ๋ฐ์์ํค๋ ๋ฐฉ๋ฒ์ ๋ํด ์์์ผ ์์ธ ์ฒ๋ฆฌ๋ฅผ ํ ์ ์์ต๋๋ค. Exception ํด๋์ค ์ ์ธ throw new RuntimeException("์์ธ ๋ฐ์!"); Exception ํด๋์ค๋ค์ ์๋ก ์ ์ธํ์ฌ ์๋ฌ๋ฅผ ๋ฐ์์ํฌ ์ ์์ต๋๋ค.
2024๋
9์ 14์ผ
Interceptor์ ์ ์ Interceptor๋ Spring MVC๊ฐ ์ ๊ณตํ๋ ๊ธฐ์ ์
๋๋ค. Filter์ ์ญํ ์ ๊ฐ์ง๋ง ์ ์ฉ๋๋ ์์์ ๋ฒ์, ์ฌ์ฉ๋ฐฉ๋ฒ์ด ๋ค๋ฆ
๋๋ค. Filter์์ ์ฐจ์ด์ HTTP ์์ฒญ -> WAS -> ํํฐ -> ์๋ธ๋ฆฟ -> ์คํ๋ง ์ธํฐ์
ํฐ -> ์ปจํธ๋กค๋ฌ ์คํ๋ง์ด ์คํ๋๋ ํ๋ฆ์ ๋ณด๋ฉด ํํฐ๋ ์๋ธ๋ฆฟ ์ ์, ์ธํฐ์
ํฐ๋ ์๋ธ๋ฆฟ ํ์ ํธ์ถ๋ฉ๋๋ค.
2024๋
9์ 14์ผ
Filter์ ์ ์ Servlet์ด ์ง์ํ๋ ๊ธฐ๋ฅ์ผ๋ก, ๊ณตํต์ ์ธ ๊ด์ฌ์ฌ๋ฅผ ๊ฐ์ง๊ณ ์๋ ๊ธฐ๋ฅ๋ค์ด ์คํ๋๊ธฐ ์ ๊ณตํต์ ์ผ๋ก ์ํํ ์์
์ ์งํํ ์ ์์. Filter์ ์ธํฐํ์ด์ค ํํ public interface Filter { public default void init(FilterConfig filterConfig) throws ServletException {} public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException; public default void destroy() {} } init(): ํํฐ ์ด๊ธฐํ ๋ฉ์๋, ์๋ธ๋ฆฟ ์ปจํ
์ด๋๊ฐ ์์ฑ๋ ๋ ํธ์ถ๋ฉ๋๋ค.
2024๋
8์ 29์ผ
@SessionAttribute์ ์ ์ Spring์์ Session์ ๋ฐ์ดํฐ๋ฅผ ๋ ๊ฐํธํ๊ฒ ๊ฐ์ ธ์ฌ ์ ์๋๋ก ํ๋ ๊ธฐ๋ฅ์
๋๋ค. ์ด ๊ธฐ๋ฅ์ ์ฌ์ฉํ๋ฉด ์ธ์
์ ๋ฐ๋ก ์์ฑํ์ง ์๊ณ ๊ฐ์ ๊ฐ์ ธ์ค๊ธฐ๋ง ํฉ๋๋ค. ์ฌ์ฉ ์์ public String home( @SessionAttribute(name = "loginMember", required = false) Member loginMember, ...) { ..
2024๋
8์ 29์ผ
์ธ์
์ด๋? ๋ก๊ทธ์ธ์ ์์๋ก ๋ค๋ฉด ์ฌ์ฉ์์ ID๊ฐ ์์ฒด๋ฅผ ์ฟ ํค์ ์ ์ฅํด๋ฒ๋ฆฌ๋ฉด ๋ณด์์ด ์ทจ์ฝํด์ง๋๋ค. ์ด๋ฅผ ๋ณด์ํ๊ธฐ ์ํด ๋ธ๋ผ์ฐ์ ์ชฝ์์ ์ ์ฅ๋์ด์๋ ์ฟ ํค ์ ์ฅ์์๋ ๋น์ทํ์ง๋ง ๋ค๋ฅด๊ฒ ์๋ฒ์ชฝ์์ ์ ์ฅ๋๋ Key-Valueํ์
์ ์ ์ฅ๊ณต๊ฐ์ ์ธ์
(Session) ์ด๋ผ๊ณ ๋ถ๋ฆ
๋๋ค. ์ฌ์ฉ์์ ID๊ฐ์ ์ธ์
์ ์ฅ์์ value๋ก ์ ์ฅ์ด ๋๊ณ , ์ด์ ๋ํด ๋๋คํ๊ฒ ์์ฑ๋ Session ID๊ฐ์ ์ฟ ํค ์ ์ฅ์์์ ์ ์ฅ๋๊ฒ ํจ์ผ๋ก์จ ์ฟ ํค ์ ์ฅ์์๋ ์ฌ์ฉ์์ ๋ํ ์ง์ ์ ์ธ ์ ๋ณด๋ฅผ ์ ์ฅํ์ง ์๋๋ก ํ์์ต๋๋ค.
2024๋
8์ 21์ผ
์ ์ ์๋ธ๋ฆฟ์ ์น ์ปดํฌ๋ํธ ์ค ํ๋๋ก, ์น ์๋ฒ ๋๋ ์ ํ๋ฆฌ์ผ์ด์
์๋ฒ์์ ํด๋ผ์ด์ธํธ์ ์์ฒญ์ ์ฒ๋ฆฌํ๊ณ ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ํด๋ผ์ด์ธํธ์๊ฒ ๋ค์ ์ ์กํ๋ ์ญํ ์ ํฉ๋๋ค. ์๋ธ๋ฆฟ ์์ฑ ์์ HttpServlet ํด๋์ค๋ฅผ extendsํ์ฌ service()ํจ์๋ฅผ ๊ตฌํํด ์ฌ์ฉํ ์ ์์ต๋๋ค.
2024๋
8์ 21์ผ
Spring์ Model-View-Controller(MVC) ์ ์ฒด ๊ตฌ์กฐ ๋์ ์์ ํธ๋ค๋ฌ ์กฐํ: ํธ๋ค๋ฌ ๋งคํ์ ํตํด ์์ฒญ URL์ ๋งคํ๋ ํธ๋ค๋ฌ(์ปจํธ๋กค๋ฌ)๋ฅผ ์กฐํํ๋ค. ํธ๋ค๋ฌ ์ด๋ํฐ ์กฐํ: ํธ๋ค๋ฌ๋ฅผ ์คํํ ์ ์๋ ํธ๋ค๋ฌ ์ด๋ํฐ๋ฅผ ์กฐํํ๋ค. ํธ๋ค๋ฌ ์ด๋ํฐ ์คํ: ํธ๋ค๋ฌ ์ด๋ํฐ๋ฅผ ์คํํ๋ค.
2024๋
8์ 21์ผ
Spring์ Message๊ด๋ฆฌ ์ ์ Spring ํ๋ก์ ํธ์์ ์ฌ๋ฌ ๊ณณ์ ์ฌ์ฉ๋๋ ๋ค์ํ ๋ฌธ๊ตฌ๋ฅผ ํ ๊ณณ์์ ๊ด๋ฆฌํ ์ ์๋๋ก ํ๋ ๊ธฐ๋ฅ๊ตญ๊ฐ๋ณ๋ก ์ค์ ํ ์๋ ์์ด์ ๊ตญ์ ํ ๊ธฐ๋ฅ์ ์ ๊ณตํฉ๋๋ค. ์ด๊ธฐ ์ค์ Spring์์ ResourceBundleMessageSource๋ฅผ Bean์ผ๋ก ๋ฑ๋กํด์ฃผ๋ฉด ๋ฉ๋๋ค.
2024๋
8์ 21์ผ
HttpServletResponse์ ์ ์ Servlet์์ HTTP ์๋ฒ๊ฐ ํด๋ผ์ด์ธํธ์๊ฒ ์๋ต์ ๋ณด๋ด๋ ๋ฐ ์ฌ์ฉ๋๋ ๊ฐ์ฒด HTTP ์๋ต ์ํ ์ฝ๋๋ฅผ ์ค์ ํ๊ณ , ์๋ต ํค๋๋ฅผ ์ถ๊ฐํ๊ฑฐ๋ ์์ ํ๋ฉฐ, ํด๋ผ์ด์ธํธ๋ก ๋ณด๋ผ ๋ฐ์ดํฐ๋ฅผ ์ธ ์ ์์ต๋๋ค. HTTP ์๋ต ์ค์ 1. ์ํ ์ฝ๋ ์ค์ response.setStatus(HttpServletResponse.SC_OK); // ์ ์ ์ํ์ฝ๋(200)์ ์ค์ 2.
2024๋
8์ 21์ผ
HttpServletRequest์ ์ ์ Servlet์ ๊ฐ๋ฐ์๊ฐ HTTP ์์ฒญ ๋ฉ์์ง๋ฅผ ํธ๋ฆฌํ๊ฒ ์ฌ์ฉํ ์ ์๋๋ก ๊ฐ๋ฐ์ ๋์ ์ HTTP ์์ฒญ ๋ฉ์์ง๋ฅผ ํ์ฑํ๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ HttpServletRequest ๊ฐ์ฒด์ ๋ด์์ ์ ๊ณตํฉ๋๋ค. HTTP ์์ฒญ ๋ฉ์์ง ์กฐํ http://localhost:8080/hello?username=hello์ ์ ์ํ ์ํฉ 1.
2024๋
8์ 21์ผ
BindingResult์ ์ ์ ์ฌ์ฉ์ ์
๋ ฅ์ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ ๊ณผ์ ์์ ๋ฐ์ํ๋ ์ค๋ฅ๋ฅผ ๊ด๋ฆฌํ๊ณ ์ถ์ ํ๋ ์ธํฐํ์ด์ค ์ด๋ฅผ ํตํด ์
๋ ฅ ๋ฐ์ดํฐ์ ์ ํจ์ฑ ๊ฒ์ฌ ๊ฒฐ๊ณผ๋ฅผ ์ ์ฅํ๊ณ , ํ์ํ ๋ ์ค๋ฅ๋ฅผ ์ฝ๊ฒ ์ฒ๋ฆฌํ ์ ์๋๋ก ๋์์ค๋๋ค. ์ฃผ์์ฌํญ Controller์ ํจ์์ ๋งค๊ฐ๋ณ์๋ก ๋ค์ด๊ฐ๋ BindingResult๋ @ModelAttribute์ด ๋ถ์ ๋งค๊ฐ๋ณ์ ๋ฐ๋ก ๋ค์ ์์ผ ํฉ๋๋ค.
2024๋
8์ 21์ผ
Bean Validation์ ์ ์ Bean Validation 2.0(JSR-380)์ด๋ผ๋ ๊ธฐ์ ํ์ค์ผ๋ก ๊ฒ์ฆ ์ ๋
ธํ
์ด์
๊ณผ ์ฌ๋ฌ ์ธํฐํ์ด์ค์ ๋ชจ์์
๋๋ค. ์ฌ์ ์์
์์กด๊ด๊ณ ์ถ๊ฐ Bean Validation์ ์ฌ์ฉํ๊ธฐ ์ํด์ ์ฐ์ build.gradle์์ ์์กด๊ด๊ณ๋ฅผ ์ถ๊ฐํด์ค์ผํฉ๋๋ค.
2024๋
8์ 21์ผ
Bean์ ์ ์ Spring IoC(Inversion of Control) ์ปจํ
์ด๋๊ฐ ๊ด๋ฆฌํ๋ ๊ฐ์ฒด์
๋๋ค.์์กด์ฑ ์ฃผ์
(DI)์ ํตํด ์ธ๋ถ์์ ๊ด๋ฆฌ ๋ฐ ์ฃผ์
์ด ๊ฐ๋ฅํฉ๋๋ค. ์ ์ธ ๋ฐฉ๋ฒ 1. XML์ ํตํ ์ ์ธ <beans> <bean id="myBean" class="com.example.MyClass"/> </beans> 2.
2024๋
8์ 21์ผ
ApplicationContext์ ์ ์ Spring Bean์ ๊ด๋ฆฌํ๊ณ ์กฐํํ๋ Spring Container์ ์ต์์ ์ธํฐํ์ด์ค์ธ BeanFactory๋ผ๋ ์ธํฐํ์ด์ค์ ๊ธฐ๋ฅ์ ๋ชจ๋ ์์๋ฐ์์ ์ ๊ณตํ๋ ์ญํ ์ ํฉ๋๋ค.๊ฑฐ๊ธฐ๋ค๊ฐ ์ถ๊ฐ์ ์ผ๋ก ํธ๋ฆฌํ ๋ถ๊ฐ๊ธฐ๋ฅ์ ๋ ์ ๊ณตํฉ๋๋ค ...
2024๋
8์ 21์ผ
๊ฒ์ฆ ๊ธฐ๋ฅ์ ์ ์ ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ๋ฐ์ ๋ฐ์ดํฐ๊ฐ ๋น์ฆ๋์ค ๊ท์น์ ๋ง์กฑํ๋์ง ํ์ธํ๊ฑฐ๋, ๋ฐ์ดํฐ๊ฐ ์ผ๊ด์ฑ ์๊ณ ์์ ์ ์ธ ์ํ๋ฅผ ์ ์งํ ์ ์๋๋ก ๋ณด์ฅํด์ฃผ๋ ๊ธฐ๋ฅ ์ค๋ฅ ๊ด๋ฆฌ์ ์ถ์ 1. BindingResult ThymeLeaf์์ ๊ฒ์ฆ ์ค๋ฅ ํํ ThymeLeaf์ ๊ฒ์ฆ ๊ด๋ จ ๊ธฐ๋ฅ ์ฐธ๊ณ (๊ด๋ จ ๊ธ ์ค๋น ์ค) Message ๊ธฐ๋ฅ ์์ฉ Message ๊ธฐ๋ฅ์ ์ฌ์ฉํ์ฌ ์ค๋ฅ ๋ฉ์์ง๋ฅผ ํจ๊ณผ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์์ต๋๋ค.
2024๋
8์ 20์ผ
@ResponseBody์ ์ ์ Controller์ ๋ฉ์๋๊ฐ returnํ๋ ๋ฐ์ดํฐ ์์ฒด๊ฐ ํด๋ผ์ด์ธํธ์๊ฒ ์ ๋ฌ๋๋๋ก ํ๋ Annotation ๋จ์ ํ
์คํธ ์๋ต @ResponseBody @GetMapping("/response-body-string") public String responseBody() ...
2024๋
8์ 20์ผ
@Validated์ ์ ์ ๊ฒ์ฆ๊ธฐ๋ฅผ ์คํํ๋ผ๋ AnnotationWebDataBinder ์ ๋ฑ๋กํ ๊ฒ์ฆ๊ธฐ๋ฅผ ์ฐพ์์ ์คํํฉ๋๋ค. ๊ฒ์ฆ๊ธฐ ์ ์ ๋ฐฉ๋ฒ ์ฌ๋ฌ ๊ฒ์ฆ๊ธฐ ์ค ์ด๋ค ๊ฒ์ฆ๊ธฐ๋ฅผ ์ ์ฉํ๊ฒ ๋ ์ง๋ supports()์์ ์ง์ ํ ํด๋์ค๋ฅผ ํตํด ์ ํด์ง๋๋ค. ์ฌ์ฉ ์์ ๊ฒ์ฆ๊ธฐ(๋ณ๋ ์์) @Component public class ItemValidator implements Validator { @Override public boolean supports(Class<?> clazz) { return Item.class.isAssignableFrom(clazz); } @Override public void validate(Object target, Errors errors) { ..
2024๋
8์ 20์ผ
@RequestParam์ ์ ์ ํด๋ผ์ด์ธํธ๊ฐ ๋ณด๋ธ ์์ฒญ ์ค ํ๋ผ๋ฏธํฐ์ ๊ฐ์ ๋ถ๋ฌ์ค๊ณ ์ถ์ ๋ ์ฌ์ฉํ๋ Annotation ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ http://localhost:8080/request-param?username=hello&age=20 ๊ฒฝ๋ก๋ก ์์ฒญํ์ ๋ @ResponseBody @RequestMapping("/request-param") ...
2024๋
8์ 20์ผ
@RequestMapping์ ์ ์ Spring์์ ์ฃผ๋ก ์ฌ์ฉํ๋ ์ ๋
ธํ
์ด์
๊ธฐ๋ฐ์ ์ปจํธ๋กค๋ฌ๋ฅผ ์ง์ํ๋ ํธ๋ค๋ฌ ๋งคํ๊ณผ ์ด๋ํฐ ์ฌ์ฉ ์์ @Controller public class Controller { @RequestMapping("/hello/form", method = RequestMethod.GET) ...
2024๋
8์ 20์ผ
@RequestBody์ ์ ์ HTTP ์์ฒญ์ Body๋ฅผ ์๋ฐ ๊ฐ์ฒด๋ก ๋ณํํ์ฌ ๊ทธ ๋ฉ์๋์ ํ๋ผ๋ฏธํฐ๋ก ์ ๋ฌํ๋๋ก ํฉ๋๋ค.์ฆ, ํด๋ผ์ด์ธํธ๋ก๋ถํฐ ๋ฐ์ ์์ฒญ ๋ณธ๋ฌธ์ ์๋์ผ๋ก ์ํ๋ ํ์
์ ๊ฐ์ฒด๋ก ๋งคํํด์ฃผ๋ ์ญํ JSON๊ณผ ๊ฐ์ ํํ์ ๊ฐ์ฒด ์์ฑ @Data public class HelloData { private String username; ...
2024๋
8์ 20์ผ
@Qualifier์ ์ ์ ์กฐํํ๊ณ ์ถ์ Bean๊ณผ ํ์
์ด ๋์ผํ ๋ค๋ฅธ Bean ์ด ํ ๊ฐ ์ด์ ์๋ค๋ฉด ์ค๋ฅ๊ฐ ๋ฉ๋๋ค.์ด๋ด ๋ ์ํ๋ ๋น์๊ฒ ์กฐํํ ๋ ์ฌ์ฉํ ๋ณ๋ช
์ @Qualifier๋ก ์ง์ ํด์ค ์ ์์ต๋๋ค. ์ฌ์ฉ ์์ ๋น ๋ฑ๋ก @Component @Qualifier("mainDiscountPolicy") // public class RateDiscountPolicy implements DiscountPolicy { } ์์กด์ฑ ์ฃผ์
@Autowired public OrderServiceImpl( MemberRepository memberRepository, @Qualifier("mainDiscountPolicy") DiscountPolicy discountPolicy ) { this.memberRepository = memberRepository; this.discountPolicy = discountPolicy; } ์ฝ๋ ์ค๋ช
๋น ๋ฑ๋กํ ๋ ํด๋น ๋น์ @Qualifier๋ฅผ ํตํด ๋ณ๋ช
์ ์ง์ด์ฃผ๊ณ , ์์กด์ฑ์ ์ฃผ์
ํ ๋ @Qualifier๋ฅผ ์ด์ฉํ์ฌ ์ง์ด๋์ ๋ณ๋ช
์ ๋ฃ์ด ์์กด์ฑ ์ฃผ์
์ ํฉ๋๋ค.
2024๋
8์ 20์ผ
@Primary์ ์ ์ ์กฐํํ๊ณ ์ถ์ Bean๊ณผ ํ์
์ด ๋์ผํ ๋ค๋ฅธ Bean ์ด ํ ๊ฐ ์ด์ ์๋ค๋ฉด ์ค๋ฅ๊ฐ ๋ฉ๋๋ค.์ด๋ด ๋ ์ฐ์ ์ ์ผ๋ก ์กฐํํ ๋น์ @Primary๋ฅผ ํตํด ์ง์ ํด์ค ์ ์์ต๋๋ค. ์ฌ์ฉ ์์ ๋น ๋ฑ๋ก @Component @Primary public class RateDiscountPolicy implements DiscountPolicy { } ์์กด์ฑ ์ฃผ์
@Autowired public OrderServiceImpl( MemberRepository memberRepository, DiscountPolicy discountPolicy ) { this.memberRepository = memberRepository; this.discountPolicy = discountPolicy; } ์ฝ๋ ์ค๋ช
๋น ๋ฑ๋กํ ๋ ์ฐ์ ์ ์ผ๋ก ์ ์ธํ ๋น์๊ฒ @Primary ์ด๋
ธํ
์ด์
์ ์ถ๊ฐํด์ฃผ๋ฉด ๋ณ ๋ค๋ฅธ ์์
์์ด ํด๋น ํ์
์ ๋น์ ์์กด์ฑ ์ฃผ์
ํ๋ฉด @Primary๋ฅผ ์ ์ฉํ RateDiscoutPolicyํ์
์ ๋น์ด ์ฃผ์
๋ฉ๋๋ค.
2024๋
8์ 20์ผ
@ModelAttribute์ ์ ์ @RequestParam์ผ๋ก ํ๋กํผํฐ ๊ฐ์ ํ๋ ํ๋ ๋ฐ์์ค๊ธฐ ์ฝ์ง ์์ ์ํฉ์ด ๋ง์ต๋๋ค.์ด๋ด ๋ ํ๋ผ๋ฏธํฐ๋ค์ ๊ฐ์ ํ๋กํผํฐ ์ ๊ทผ๋ฒ(setXXX)์ผ๋ก ์์์ ์
๋ ฅํ์ฌ ๊ฐ์ฒด๋ก ๊ฐ๋ค์ ๋ฐ์ ์ ์๊ฒ ํด์ค๋๋ค. ๋ฐ์ดํฐ๊ฐ ๋ค์ด๊ฐ ๊ฐ์ฒด ์์ฑ @Data public class HelloData { private String username; private int age; } ๊ธฐ๋ณธ ์ฌ์ฉ๋ฒ @ResponseBody @RequestMapping("/model-attribute-v1") public String modelAttributeV1(@ModelAttribute HelloData helloData) { log.info("username={}, age={}", helloData.getUsername(), helloData.getAge()); return "ok"; }.
2024๋
8์ 20์ผ
@ComponentScan์ ์ ์ ์ง์ xml์ด๋ java์ฝ๋๋ก ์ค์ ํ์ผ์ ์์ฑํ ํ์ ์์ด @Component ์ด๋
ธํ
์ด์
์ด ๋ถ์ ํด๋์ค๋ค์ ์คํ๋ง ๋น์ผ๋ก ๋ฑ๋กํด์ฃผ๋ Annotation @Autowired๋ฅผ ์ด์ฉํ์ฌ ์ค์ ํ๋ ์์กด๊ด๊ณ๋ ๊ทธ๋๋ก ์ ์ฉ๋ ์ฑ๋ก Bean์ ๋ฑ๋ก๋ฉ๋๋ค ...
2024๋
8์ 20์ผ
@Configuration์ ์ ์ ๋ฐ๋ก ๋น ์ ์์ฉ ํด๋์ค๋ฅผ ํ๋ ์์ฑํด์ ๋น์ ์ ์ํ๊ณ ์์กด์ฑ์ ์ค์ ํ๋ ๋ฐฉ์ ์ด ์ด๋
ธํ
์ด์
์ด ๋ถ์ ํด๋์ค ๋ด์์ @Bean ์ด๋
ธํ
์ด์
์ด ๋ถ์ ๋ฉ์๋๋ฅผ ํตํด Bean์ ์ ์ํ๊ณ , ์ปจํ
์ด๋์ ๋ฑ๋กํ ์ ์์ต๋๋ค. ์ฌ์ฉ ์์ @Configuration public class AppConfig { @Bean public MemberRepository memberRepository() { return new MemoryMemberRepository(); } @Bean public DiscountPolicy discountPolicy() { return new RateDiscountPolicy(); } @Bean public MemberService memberService() { return new MemberServiceImpl(memberRepository()); } @Bean public OrderService orderService() { return new OrderServiceImpl(memberRepository(), discountPolicy()); } } ์ฝ๋ ์ค๋ช
memberRepository, discoutPolicy, memberService, orderService ์ด 4๊ฐ์ Bean์ด ์์ฑ๋ฉ๋๋ค.
2024๋
8์ 20์ผ
@Autowired์ ์ ์ Annotation์ ํตํด Bean์ผ๋ก ์ง์ ๋ ํด๋์ค๋ค์ ์์กด์ฑ ์ฃผ์
(DI)์ ์ํด ์ฌ์ฉํ๋ Annotation ์ฌ์ฉ ์์ 1. ์์ฑ์ ์ฃผ์
@Component public class OrderServiceImpl implements OrderService { private final MemberRepository memberRepository; private final DiscountPolicy discountPolicy; @Autowired public OrderServiceImpl(MemberRepository memberRepository, DiscountPolicy discountPolicy) { this.memberRepository = memberRepository; this.discountPolicy = discountPolicy; } } ์ฝ๋ ์ค๋ช
OrderServiceImpl์์ฑ์์ @Autowired๋ฅผ ์ถ๊ฐํ์ฌ OrderServiceImpl์ด ์์ฑ๋๋ฉด์ ๋์์ memberRepository์ discountPolicy์ ์์กด์ฑ์ ์ฃผ์
๋ฐ์ 2.
2024๋
8์ 20์ผ
1. Bean Spring Bean์ด ๋ฌด์์ธ์ง ์์๋ณด๊ธฐ 2. Servlet Spring MVC ์ด์ ๋จ๊ณ์ Controller๋ฅผ ์์ฑํ ๋ฐฉ๋ฒ ์์๋ณด๊ธฐ 3. MVC in Spring Spring์์ ์ฃผ๋ก ์ฐ๋ MVC ํจํด๊ณผ ๊ด๋ จ๋ Annotation๋ค ์์๋ณด๊ธฐ 4. Message ๊ด๋ฆฌ ์์ฃผ ์ฌ์ฉํ๋ ๋ฌธ๊ตฌ๋ฅผ ๋ฐ๋ก propertiesํ์ผ๋ก ๊ด๋ฆฌํ๋ ๋ฐฉ๋ฒ ์์๋ณด๊ธฐ 5.