์ ์ฒด ๊ธ (71) ์ธ๋ค์ผํ ๋ฆฌ์คํธํ Spring์ผ๋ก AOP ๊ตฌํ (Around Advice) ๋ณด์กฐ ์ ๋ฌด์ ์ฌ์ฉ์ ๋ฐ๋ผ ์คํ๋ง์ Before Advice, After returnning Advice, After throwing Advice, Around Advice ๋ค ๊ฐ์ง๋ฅผ ์ ๊ณตํ๋ค. ์ด์ ํฌ์คํ ์์ ์์ ์๋ฐ๋ก๋ง AOP๋ฅผ ๊ตฌํํ๋ ์ฝ๋๋ฅผ ์์ ํ์ฌ Around Advice๋ฅผ ๊ตฌํํด ๋ณด๋๋ก ํ๊ฒ ๋ค. (unounou.tistory.com/48) Program.java public class Program { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("spring/aop/setting.xml"); Exam proxy = (Exam) context.getBe.. ์์ ์๋ฐ๋ก AOP ๊ตฌํํด๋ณด๊ธฐ ์ฌ์ฉ์์ ์๊ตฌ์ ๋ฐ๋ผ kor, eng, math, com ๋ค ๊ณผ๋ชฉ์ ์ด์ ์ ๊ณ์ฐํด ์ถ๋ ฅํ๋ ํ๋ก๊ทธ๋จ์ด ์๋ค๊ณ ํด๋ณด์. ํ๋ก๊ทธ๋จ์ด ๋๋ฌด ๋๋ฆฌ๋ค๋ ํผ๋๋ฐฑ์ด ์์ ๋ ๊ฐ๋ฐ์๋ค์ ์คํ์ ์๊ฐ์ด ์ผ๋ง๋ ๊ฑธ๋ฆฌ๋์ง ์ ํ์๊ฐ ์์ ๊ฒ์ด๊ณ , ๊ทธ๋ฌ๊ธฐ ์ํด ๊ณ์ฐํ๋ ์์ค๋ฅผ ๋ผ์๋ฃ๊ฒ ๋์๋ค. ์ด๋ ์ฌ์ฉ์์ ์๊ตฌ์ฌํญ๊ณผ๋ ๋ณ๊ฐ๋ก ๊ฐ๋ฐ์๋ค์ด ํ์์ ์ํด ์ฝ์ ํ ๋ถ๋ถ์ด๋ค. (=>๋ณด์กฐ ์ ๋ฌด) ** ์์ค ๊ตฌ์ฑ Exam์ NewlecExam์ ์ธํฐํ์ด์ค Program.java public class Program { public static void main(String[] args) { Exam exam = new NewlecExam(1,1,1,1); System.out.printf("total is %d\n", exam.total(.. AOP(Aspect Oriented Programming)์ ๊ฐ๋ ์คํ๋ง์ ๊ณต๋ถํ ๋ DI์ ํจ๊ป ํญ์ ๊ฑฐ๋ก ๋๋ ๊ฒ์ด ๋ฐ๋ก AOP์ด๋ค. AOP๋ ๊ด์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ด๋ผ๋ ํ๋์ ๋ฐฉ๋ฒ๋ก ์ด๋ค. ์คํ๋ง์ ์ฌ์ฉํ๋ฉด AOP ๊ตฌํ์ ์ฝ๊ฒ ํ ์ ์๋ค. ์ง๊ธ๊น์ง๋ Object Oriented Programming์๋ง ๊ด์ฌ์ ๋๊ณ ์์๋ค. ๊ฐ์ฒด ์งํฅ ํ๋ก๊ทธ๋๋ฐ์ ํตํด ์ฌ์ฉ์์ ์ํ๋ ์ ๋ฌด๋ฅผ ๋ถ์ํด์ ๊ทธ์ ๋ํ ๋ก์ง์ ๊ตฌํํด์๋ค๋ ์๋ฏธ์ด๋ค. ํ์ง๋ง ์ฌ์ฉ์์ ์๊ตฌ์ฌํญ ์ธ์๋ ๊ฐ๋ฐ์ ๋๋ ๊ด๋ฆฌ์๊ฐ ๊ฐ๋ฐ๊ณผ ์ด์์ ์ํด ํ์๋กํ๋ ์ฝ๋๊ฐ ์๊ณ , ์ด๋ฌํ ์ฃผ ์ ๋ฌด ๋ก์ง ์ธ์ ์ฝ๋์๋ ๊ด์ฌ์ ๊ฐ์ง๊ฒ ๋์๋ค. => ๊ด์ ์งํฅ ํ๋ก๊ทธ๋๋ฐ ์ฃผ ์ ๋ฌด(์ฌ์ฉ์์ ๊ด์ )์์ ๋ค๋ฅธ ๊ด์ (๋ก๊ทธ ์ฒ๋ฆฌ, ๋ณด์ ์ฒ๋ฆฌ, ํธ๋์ญ์ ์ฒ๋ฆฌ ๋ฑ)์ผ๋ก๋ ๋ด์ฉ์ด ํ์ฅ๋๋ค. ์ด๋ฐ ๋ก์ง๋ค์ ์ฌ์ฉ์์ ์๊ตฌ์ฌํญ ์ ๋ค์ ์์นํ๋ ๋ณด์กฐ .. XML Configuration์ Java Configuration์ผ๋ก ๋ณ๊ฒฝํ๊ธฐ ํ๋ก๊ทธ๋จ์ ์์ ์ฌํญ์ด ์๊ฒผ์ ๋ ์์ค ์ฝ๋๋ฅผ ์์ ํ๋ ๊ฒ๋ณด๋ค ๊ฐ์ฒด๋ฅผ ์์ฑ-์ฃผ์ ํ๋ ์ค์ ๋ถ๋ถ์ ์์ค ์ฝ๋์ ์ธ๋ถ์ธ xml์ผ๋ก ๋นผ๋๋ ๊ฒ์ด ์ ์ง ๋ณด์์ ํธ๋ฆฌํ๊ธฐ ๋๋ฌธ์ ์ฒ์์๋ ์ด๋ฌํ ๋ฐฉ๋ฒ์ด ๋๋ฆฌ ์ฐ์๋ค. ํ์ง๋ง ๊ฐ๋ฐ์๋ค์ ๊ทธ ๋ฐฉ๋ฒ๋ง์ ๋ xml์ ์์ ํด์ผ ํ๋ ๋ฒ๊ฑฐ๋ก์์ด ์๋ค๋ ํ๋จ์ ํ๊ณ ๊ทธ ๊ฒฐ๊ณผ ์์ฆ์๋ ์๋ฐ ์ฝ๋ ์์ฒด์ ์ด๋ ธํ ์ด์ ์ ๋ฌ์์ฃผ๋ ๋ฐฉ๋ฒ์ด ๋ ๋ง์ด ์ฐ์ด๊ณ ์๋ค. ๋ ๊ฐ์ง ๋ฐฉ๋ฒ์ ์ฅ๋จ์ ์ด ์์ผ๋ ํ์์ ๋ง๊ฒ ์ฌ์ฉํ๋ฉด ๋๋ค. ๋ค๋ง ํ ๊ฐ์ง ๋ฐฉ๋ฒ์ผ๋ก ๋ฐฉ์์ ํต์ผํด์ฃผ๋ ํธ์ด ์ข๋ค. ์์ ์์ฑํ ํฌ์คํ ์์๋ ์คํ๋ง ์์ด DI, xml ์ค์ ํ์ผ ์ถ๊ฐ, xml์ ์ด๋ ธํ ์ด์ ์ผ๋ก ๋ฐ๊พธ๋ ๋ฐฉ๋ฒ์ ๋ชจ๋ ์ดํด๋ณด์๋ค. ์ด์ ๋ถํฐ๋ xml์ ์ด๋ ธํ ์ด์ ์ผ๋ก ๋ฐ๊พธ๋ ๋ฐฉ์์ ๋ง๋ฌด๋ฆฌ ์ง์ด๋ณด๋๋ก ํ๊ฒ ๋ค. ๊ฐ์ฒด์ ์์ฑ์ @Co.. ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ ๊ฐ์ฒด ์์ฑ(@Component) ์ด์ ํฌ์คํ ์ ํตํด ์ด๋ ธํ ์ด์ ์ ํตํด DI๋ฅผ ํ๋ ๊ณผ์ ์ ๊ณต๋ถํด ๋ณด์๋ค. (unounou.tistory.com/44?category=1183877) ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ DI (@Autowired, @Qualifier) ์ด์ ํฌ์คํ ์์ ๋น์ ์์ฑ๊ณผ ์ฃผ์ ์ ๊ด๋ จํ ๋ถ๋ถ์ ์์ค ์ฝ๋ ์ธ๋ถ์ธ xml์ผ๋ก ๋นผ๋์ ๊ณผ์ ์ ์ดํด๋ณด์๋ค. ์์ค์ ์์ ์ฌํญ์ด ์์ ๋ ์์ค ์์ฒด๋ฅผ ์์ ํ์ง ์์๋ ๋๊ณ xml์ ์ค์ ๋ถ๋ถ๋ง ์์ unounou.tistory.com ๋น์ ์์ฑํ๋ ๋ถ๋ถ์ xml์ ์๊ณ ์๋ฐ ์์ค์ @Autowired๋ฅผ ๋ถ์ฌ ์์ฑ๋ ๋น์ ์ธํ ํ๋ ๊ณผ์ ์ด๋ค. ๊ทธ๋ ๋ค๋ฉด ์ด๋ฒ์๋ ๋น ์์ฑ ๊ณผ์ ์ญ์ ์๋ฐ ์์ค์์ ์ด๋ ธํ ์ด์ ์ผ๋ก ์ฒ๋ฆฌํด ๋ณด๋๋ก ํ๊ฒ ๋ค. GridExamConsole.java @Component public.. ์ด๋ ธํ ์ด์ ์ ์ด์ฉํ DI (@Autowired, @Qualifier) ์ด์ ํฌ์คํ ์์ ๋น์ ์์ฑ๊ณผ ์ฃผ์ ์ ๊ด๋ จํ ๋ถ๋ถ์ ์์ค ์ฝ๋ ์ธ๋ถ์ธ xml์ผ๋ก ๋นผ๋์ ๊ณผ์ ์ ์ดํด๋ณด์๋ค. ์์ค์ ์์ ์ฌํญ์ด ์์ ๋ ์์ค ์์ฒด๋ฅผ ์์ ํ์ง ์์๋ ๋๊ณ xml์ ์ค์ ๋ถ๋ถ๋ง ์์ ํ๋ฉด ๋๋ค๋ ์ด์ ์ด ์์๋ค. ์๋ฅผ ๋ค์ด, B2๋ผ๋ ์์ค ์ฝ๋๊ฐ B3์ผ๋ก ๋ฐ๋์ด์ผ ๋๋ค๋ ์ํฉ์ ๊ฐ์ ํด ๋ณด์. xml์ ๋ค์๊ณผ ๊ฐ์ ๋น ์์ฑ ์ฝ๋๊ฐ ์์์ ๋, B2๋ฅผ B3๋ก ์์ ํ๋ฉด ๋๋ ๊ฒ์ด๋ค. (์์ ํ) ํ์ง๋ง ์ด ๊ณผ์ ์ญ์ ๋ฒ๊ฑฐ๋ก์์ด ์์๊ณ , ์ฝ๋์ ์์ ๋ฉํ ๋ฐ์ดํฐ๋ฅผ ์ค์ ํ๋ ๋ฐฉ๋ฒ์ ๊ณ ์ํ๊ฒ ๋์๋ค. ์ค์ ํ์ผ์ ๋ฐ๊ฟ ํ์๊ฐ ์์ด, @Componect๊ฐ ๋ถ์ ๊ฐ์ฒด ์์ฒด๋ฅผ ๋ฐ๊ฟ๋ฒ๋ฆฌ๋ ๊ฒ์ด๋ค. => ์ด๋ ธํ ์ด์ => ์ค์ ํ์ผ์ ๋ฐ์ผ๋ก ๋นผ๋๋ xml ๋ฐฉ์์์, ์ค์ ์ ์์ ์ฝ๋์ ์ฌ์ด๋๋ ์ด๋ ธํ ์ด์ ๋ฐฉ์์ผ๋ก ํธ๋ .. xml์ ์ด์ฉํ ์คํ๋ง DI ์ง์์ ์์ฑ(Spring Bean Configuration), IoC ์ปจํ ์ด๋ ์ฌ์ฉํ๊ธฐ ์์ ์คํ๋ง ์์ด ์ง์ DI๋ฅผ ํด๋ณธ ์ฝ๋๋ฅผ ๋ค์ ํ๋ฒ ์ดํด๋ณด๊ฒ ๋ค. Program.java public class Program { public static void main(String[] args) { /* ์คํ๋ง์๊ฒ ์ง์ํ๋ ๋ฐฉ๋ฒ์ผ๋ก ์ฝ๋๋ฅผ ๋ณ๊ฒฝํ๋ค. Exam exam = new NewlecExam(); //์์ฑ //ExamConsole console = new GridExamConsole(exam); //์์ฑ์ ์ด์ฉํ ์กฐ๋ฆฝ ExamConsole console = new GridExamConsole(); console.setExam(exam); //setter๋ฅผ ํตํ ์กฐ๋ฆฝ(๊ฒฐํฉ) */ ExamConsole console = ?; console.print(); } } ๊ฐ์ฒด๋ฅผ ์์ฑ(new ์ฐ์ฐ์ ์ด์ฉ),.. ์คํ๋ง ์์ด DI ํด๋ณด๊ธฐ ** ์์ค ๊ตฌ์ฑ Exam์ NewlecExam์ ์ธํฐํ์ด์ค ExamConsole์ InlineExamConsole๊ณผ GridExamConsole์ ์ธํฐํ์ด์ค (์ ์ฒด ์์ค ์ฝ๋๋ www.newlecture.com/ ์ฐธ๊ณ ) public class Program { public static void main(String[] args) { Exam exam = new NewlecExam(); ExamConsole console = new InlineExamConsole(exam); //InlineExamConsole์ด exam ๊ฐ์ฒด๋ฅผ ์กฐ๋ฆฝํ๊ณ ์๋ค => DI //ExamConsole console = new GridExamConsole(exam); console.print(); //์ด์ ์ ์ถ๋ ฅํ๋ ํจ์ } } .. ์ด์ 1 2 3 4 5 6 7 ยทยทยท 9 ๋ค์