๐๋ฐฑ์๋ : BackEnd
์์ฑ์ injection์ ์ฐ๋๊ฒ ์ข๋ค ์์ฑ์ injection์ ํ๋ฉด setter๋ก entity๋ฅผ ๋ฐ๊พธ๋ ๊ฒฝ์ฐ๋ฅผ ๋ฐฉ์งํ ์ ์๋ค ์์ฑ์๊ฐ ํ๋๋ง ์๋๊ฒฝ์ฐ @Autowired๋ฅผ ์๋ตํด๋ ๋๋ค MemberRepository๋ ๋ณ๊ฒฝ๋ ์ผ์ด ์๊ธฐ ๋๋ฌธ์ final๋ก ํด์ฃผ๋๊ฒ ์ข๋ค test case๋ฅผ ์์ฑํ ๋ ์ปดํ์ผ ๋จ๊ณ์์ injection์ ๋ช
ํํ๊ฒ ์ ์ ์๋ค ( ์๋ ์ฝ๋์ ๊ฐ์ด ) @RequiredArgsConstructor final์ด ์๋ ํ๋๋ง์ ๊ฐ์ง๊ณ ์์ฑ์๋ฅผ ๋ง๋ค์ด ์ค๋ค ๊ฒฐ๊ตญ ์๋์ผ๋ก ์์ฑ์๋ฅผ ํ๋ ๋ง๋ค๊ณ injection์ ๋ฐ์ ์ ์์ @PersistenceContext -> @Autowired @PersistenceContext ๋ฅผ @Autowired๋ก ๋์ฒดํ ์ ์์ (SpringBo..
๐๋ฐฑ์๋ : BackEnd
Spring์ด๋? ์ ํํ ํํ์ผ๋ก๋ '์คํ๋ง ํ๋ ์์ํฌ(Spring Framework) ์์กด์ฑ ์ฃผ์
(DI) ์ ์ด์ญ์ (IOC) ๊ด์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ(AOP) ์ด ๊ฐ์ฅ ์ค์ํ ์์ ์ ์์๋ค์ ํตํด ๋์จํ ๊ฒฐํฉ์ ๋ฌ์ฑํ ์ ์์ ์์ ๊ฐ์ด ๋์จํ ๊ฒฐํฉ์ผ๋ก ๊ฐ๋ฐํ ์ดํ๋ฆฌ์ผ์ด์
์ ๋จ์ ํ
์คํธ๋ฅผ ์ํํ๊ธฐ ์ฉ์ดํจ ๊ด์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ(AOP)๋? AOP๋ ์ฝ๊ฒ ๋งํด, OOP(๊ฐ์ฒด์งํฅ ํ๋ก๊ทธ๋๋ฐ)์ ๋ณด์ํ๋ ์๋จ์ผ๋ก, ์ฌ๋ฌ ๊ณณ์ ์ฐ์ด๋ ๊ณตํต ๊ธฐ๋ฅ์ ๋ชจ๋ํํ์ฌ ํ์ํ ๊ณณ์ ์ฐ๊ฒฐํจ์ผ๋ก์จ ์ ์ง๋ณด์ ๋๋ ์ฌ์ฌ์ฉ์ ์ฉ์ดํ๋๋ก ํ๋ ๊ฒ์ ์๋ฏธ AOP๋ฅผ ํตํด ๊ธฐ์กด ํ๋ก์ ํธ์ ๋ค์ํ ๊ธฐ๋ฅ์ ๋ก์ง ์์ ์์ด ์ถ๊ฐํ ์ ์์ ์ด๋ฐ ๊ฐ๋ฐ ๋ฐฉ์์ ํตํด ๊ฒฐํฉ๋๋ฅผ ๋ฎ์ถ ๊ฐ๋ฐ์ด ๊ฐ๋ฅํจ SpringBoot๊ฐ ๋์ค๊ฒ ๋ ์ด์ ์คํ๋ง์ ๋ค์ํ ๊ธฐ๋ฅ์..
๐๋ฐฑ์๋ : BackEnd
@Autowired์ ์์น setter ์ setterํจ์๊ฐ ํธ์ถ๋๋ฉด์ injection์ด ์ํ๋จ field ์ ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ํธ์ถํ๋ฉด์ injection์ด ์ํ๋จ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ์๋ค๋ฉด error๋ฐ์(์ ์ฝ๋์ ๊ฐ์ ๊ฒฝ์ฐ ์ค๋ฅ ๋ฐ์) overload ์์ฑ์ ์ overload ์์ฑ์์์ ๊ฐ์ฒด๋ฅผ ๋ฐ์ธ๋ฉ ์์ ๊ฐ์ดํ๊ฒ ๋๋ฉด @Qualifier์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ค overload์์ฑ์ ๊ฐ์ ๊ฒฝ์ฐ์ Exam์ ๋ ๊ฐ ์ด์ ๋ฃ์ ์ ์๋ค๊ณ ์๊ฐํด์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๊ฒ ๋๋ ๊ฒ! ๐ ํ๋ผ๋ฏธํฐ ๊ฐ์์ @Qualifier์ ์ค์ ํด์ ํด๊ฒฐ @Autowired์ required ์์ฑ exam2๋ผ๋ bean์ด ์๋๊ฒฝ์ฐ ์ด๋ป๊ฒ ๋์ํ๋์ง ์ค์ ํ ์ ์๋ ์์ฑ์ด๋ค. required=false๋ก ์ค์ ์ ํ๊ฒ ๋๋ฉด ํด๋น bean์ด ์๋..
๐๋ฐฑ์๋ : BackEnd
์ ์ ์ฐจ์ด์ ์ด๋ฏธ ๋ฑ๋ก๋ bean์ ๋ํด์๋ง Annotation์ ํ์ฑํ ํ๋ค. ๋ฐ๋ผ์ ์ ํ๊ทธ๋ฅผ ์ฌ์ฉํ๋๋ผ๋ xml์ bean์ ๋ฐ๋์ ์ ์ธํด์ผ ํ๋ค. @Autowired์ @Qualifier ๋ ๊ฐ์ง๋ง ํด๊ฒฐํ๋ค. bean์ ๋ฑ๋ก์ฌ๋ถ์ ๊ด๊ณ์๋ค. ์คํ๋ง์ด ์์์ bean์ค์บ์ ํตํด Annotation์ ํด์ํ๊ณ ํ์ฑํํ๋ค. @Autowired์ @Qualifier๋ฟ๋ง ์๋๋ผ @Service, @Component, @Controller, @Repository ๋ฑ ๋ชจ๋ ํด๋์ค๋ฅผ ์ค์บํ๊ณ bean์ ์์ฑํ๋ค. ๋ค๋ง base-package๋ฅผ ํตํด ์คํ๋ง์ด ์ค์บํ ํจํค์ง ์์น๋ฅผ ์ง์ ํด๋ฌ์ผ ํ๋ค.
๐๋ฐฑ์๋ : BackEnd
Spring @Autowired๋ ์๋ฃํ์ ๊ธฐ์ค์ผ๋ก ์ฐพ๋๋ค Exam๊ฐ์ฒด๋ฅผ ์์ฑํ๋ ์ id="exam"์ ์ง์ ๋ค. ์คํํ๋ฉด ์ฑ๊ณต์ ์ผ๋ก ์๋ํ๋ค. ๐ @Autowired๋ ์๋ฃํ์ ๊ธฐ์ค์ผ๋ก ์ฐ๊ฒฐํ๋ค(๊ธฐ๋ณธ๋์) ๋ณ์๋ช
์ ๊ธฐ์ค์ผ๋ก ์ฐ๊ฒฐํ๊ธฐ๋ ํจ (๋ฐ์ ๋์ด) @Qualifier public class InlineExamConsole implements ExamConsole { private Exam exam; ... @Autowired @Qualifier("exam1") @Override public void setExam(Exam exam) { this.exam = exam; } } @Qualifier๋ฅผ ์ฌ์ฉํ์ฌ exam1์ Autowiredํ๋๋ก ์ค์ ํ๋ค. exam๊ฐ์ฒด exam1๊ณผ exam2๊ฐ ๋ ๊ฐ ์๋ค..
๐๋ฐฑ์๋ : BackEnd
Spring โญXml->Annotation (์ถ์ฒ:https://www.youtube.com/watch?v=S065KRjXRSY&list=PLq8wAnVUcTFUHYMzoV2RoFoY2HDTKru3T&index=12&t=90s) XML์ ์ด์ฉํ๋ค๋ฉด B2๋ฅผ B3๋ก ๋ฐ๊พธ๊ณ ์ถ์๋ ์ค์ ํ์ผ ์ฝ๋๋ฅผ ์์ ํด์ผ ํ๋ค. ์์ค์ฝ๋๋ฅผ ๋ฐ๊พธ์ง ์๊ณ ์ค์ ํ์ผ๋ง ์์ ํ ์ ์๋ค๋ ์ ์ด ์์ ์๋ ์ข์์ง๋ง, ์ค์ ํ์ผ์ ์์ ํ๋๊ฒ์ ๋ถํธํจ์ ๋๋ผ๊ฒ ๋์๋ค. ์์ค์ฝ๋์ ์ค์ ์ ์ฌ๋ ์ด๋
ธํ
์ด์
์ ํ์ฉํ๊ธฐ ์์ํ๋ค. โญ@Autowired์ ์ด์ฉํ DI (์ถ์ฒ:https://www.youtube.com/watch?v=S065KRjXRSY&list=PLq8wAnVUcTFUHYMzoV2RoFoY2HDTKru3T&index=12&t=90s) ..
๐๋ฐฑ์๋ : BackEnd
Spring toString (์๋ฐ์ ๊ฐ์) ๊ฐ์ฒด์งํฅ ์ธ์ด๋ค์ ๊ฐ์ฒด์ toString์ด๋ผ๋ ๋ฉ์๋๋ฅผ ๊ธฐ๋ณธ์ ์ผ๋ก ์ ๊ณตํ๋ค. ์ผ๋ฐ์ ์ผ๋ก toString์ ๊ทธ ๊ฐ์ฒด๋ฅผ ์ค๋ช
ํด์ฃผ๋ ๋ฌธ์์ด์ ๋ฆฌํดํ๋ค. ๊ทธ๋ฆฌ๊ณ ๊ฐ์ฒด์ toString์ ๋ฎ์ด์ฐ๊ธฐ(overriding)ํ๋ฉด ๋ค๋ฅธ ํ์์ ๋ฌธ์์ด์ ๋ฆฌํดํ ์ ์๋ค. ๋ฌธ์์ด์ด ๊ธฐ๋๋๋ ๊ณณ์์ ๋ฌธ์์ด์ด ์๋ ๊ฐ์ฒด๋ฅผ ์ฌ์ฉํ๋ฉด ์์คํ
์ ์์์ ์ผ๋ก toString์ ํธ์ถํ๋ค. โญ์์ java ์ฝ๋ ์
์์ฑ public class Program { public static void main(String[] args) { List exams = new ArrayList(); exams.add(new NewlecExam(1,1,1,1)); for(Exam e : exams) System.o..
๐๋ฐฑ์๋ : BackEnd
Spring โญSpring์ฌ์ฉํ์ง ์์ DI package spring.di; public class Program { public static void main(String[] args) { Exam exam = new NewlecExam(1,2,3,4); ExamConsole console = new GridExamConsole(); console.setExam(exam); // DI console.print(); } } >>> โโโโโโโโโโโฌโโโโโโโโโโ โ total โ avg โ โโโโโโโโโโโผโโโโโโโโโโค โ 10 โ 2.50 โ โโโโโโโโโโโดโโโโโโโโโโspring์ ์ฌ์ฉํ์ง ์๊ณ exam์ด๋ผ๋ dependecy๋ฅผ ๋ง๋ค์ด์ console์ injectionํ..