Spring/Spring ๊ธฐ๋ณธ

Spring/Spring ๊ธฐ๋ณธ

[์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ] - 11. ์ปจํ…Œ์ด๋„ˆ์—์„œ ๋นˆ ์กฐํšŒํ•˜๊ธฐ

์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋œ ๋ชจ๋“  ๋นˆ ์กฐํšŒ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์‹ค์ œ ์Šคํ”„๋ง ๋นˆ๋“ค์ด ์ž˜ ๋“ฑ๋ก ๋˜์—ˆ๋Š”์ง€ ํ™•์ธํ•ด๋ณด์ž. ์ปจํ…Œ์ด๋„ˆ ์ƒ์„ฑ public class ApplicationContextInfoTest { AnnotationConfigApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class); ApplicationContext ๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ผ ํ•œ๋‹ค. AppConfig.class ๋ฅผ ๊ตฌ์„ฑ ์ •๋ณด๋กœ ์ง€์ •ํ–ˆ๋‹ค. ๋ชจ๋“  ๋นˆ ์ถœ๋ ฅํ•˜๊ธฐ @Test @DisplayName("๋ชจ๋“  ๋นˆ ์ถœ๋ ฅํ•˜๊ธฐ") void findAllBean() { String[] beanDefinitionNames = ac.getBeanDefinitionNames(); for (S..

Spring/Spring ๊ธฐ๋ณธ

[์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ] - 10. ์Šคํ”„๋ง์œผ๋กœ ์ „ํ™˜ํ•˜๊ธฐ / ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ

์Šคํ”„๋ง์œผ๋กœ ์ „ํ™˜ํ•˜๊ธฐ ์ง€๊ธˆ๊นŒ์ง€ ์ˆœ์ˆ˜ํ•œ ์ž๋ฐ” ์ฝ”๋“œ๋งŒ์œผ๋กœ DI๋ฅผ ์ ์šฉํ–ˆ๋‹ค. ์ด์ œ ์Šคํ”„๋ง์„ ์‚ฌ์šฉํ•ด๋ณด์ž. @Configuration public class AppConfig { AppConfig์— ์„ค์ •์„ ๊ตฌ์„ฑํ•œ๋‹ค๋Š” ๋œป์˜ @Configuration ์„ ๋ถ™์—ฌ์ค€๋‹ค. ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” @Configuration ์ด ๋ถ™์€ AppConfig ๋ฅผ ์„ค์ •(๊ตฌ์„ฑ) ์ •๋ณด๋กœ ์‚ฌ์šฉํ•œ๋‹ค. @Bean public MemberRepository memberRepository() { return new MemoryMemberRepository(); } ๊ฐ ๋ฉ”์„œ๋“œ์— @Bean ์„ ๋ถ™์—ฌ์ค€๋‹ค. ์ด๋ ‡๊ฒŒ ํ•˜๋ฉด ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•œ๋‹ค. ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ApplicationContext ๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ผ ํ•œ๋‹ค. ApplicationC..

Spring/Spring ๊ธฐ๋ณธ

[์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ] - 9. IoC, DI, ์ปจํ…Œ์ด๋„ˆ

IoC, DI, ๊ทธ๋ฆฌ๊ณ  ์ปจํ…Œ์ด๋„ˆ ์ œ์–ด์˜ ์—ญ์ „ IoC(Inversion of Control) ํ”„๋กœ๊ทธ๋žจ์˜ ์ œ์–ด ํ๋ฆ„์„ ์ง์ ‘ ์ œ์–ดํ•˜๋Š” ๊ฒƒ์ด ์•„๋‹ˆ๋ผ ์™ธ๋ถ€์—์„œ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์„ ์ œ์–ด์˜ ์—ญ์ „(IoC)์ด๋ผ ํ•œ๋‹ค. public class MemberServiceImpl implements MemberService { private final MemberRepository memberRepository = new MemoryMemberRepository(); ๊ธฐ์กด ํ”„๋กœ๊ทธ๋žจ์€ ํด๋ผ์ด์–ธํŠธ ๊ตฌํ˜„ ๊ฐ์ฒด๊ฐ€ ์Šค์Šค๋กœ ํ•„์š”ํ•œ ์„œ๋ฒ„ ๊ตฌํ˜„ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•˜๊ณ , ์—ฐ๊ฒฐํ•˜๊ณ , ์‹คํ–‰ํ–ˆ๋‹ค. ๋ฐ˜๋ฉด์— AppConfig๊ฐ€ ๋“ฑ์žฅํ•œ ์ดํ›„์— ๊ตฌํ˜„ ๊ฐ์ฒด๋Š” ์ž์‹ ์˜ ๋กœ์ง์„ ์‹คํ–‰ํ•˜๋Š” ์—ญํ• ๋งŒ ๋‹ด๋‹นํ•œ๋‹ค. public class AppConfig { public MemberR..

Spring/Spring ๊ธฐ๋ณธ

[์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ] - 8. ๊ด€์‹ฌ์‚ฌ์˜ ๋ถ„๋ฆฌ / AppConfig

๊ด€์‹ฌ์‚ฌ๋ฅผ ๋ถ„๋ฆฌํ•˜์ž ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ํ•˜๋‚˜์˜ ๊ณต์—ฐ์ด๋ผ ์ƒ๊ฐํ•ด๋ณด์ž. ๊ฐ๊ฐ์˜ ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๋ฐฐ์—ญ(๋ฐฐ์šฐ ์—ญํ• )์ด๋ผ ์ƒ๊ฐํ•˜์ž. ๊ทธ๋Ÿฐ๋ฐ! ์‹ค์ œ ๋ฐฐ์—ญ ๋งž๋Š” ๋ฐฐ์šฐ๋ฅผ ์„ ํƒํ•˜๋Š” ๊ฒƒ์€ ๋ˆ„๊ฐ€ ํ•˜๋Š”๊ฐ€? ๋กœ๋ฏธ์˜ค์™€ ์ค„๋ฆฌ์—ฃ ๊ณต์—ฐ์„ ํ•˜๋ฉด ๋กœ๋ฏธ์˜ค ์—ญํ• ์„ ๋ˆ„๊ฐ€ ํ• ์ง€ ์ค„๋ฆฌ์—ฃ ์—ญํ• ์„ ๋ˆ„๊ฐ€ ํ• ์ง€๋Š” ๋ฐฐ์šฐ๋“ค์ด ์ •ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋‹ค. public class OrderServiceImpl implements OrderService { private final MemberRepository memberRepository = new MemoryMemberRepository(); // private final DiscountPolicy discountPolicy = new FixDiscountPolicy(); private final DiscountPolicy di..

Spring/Spring ๊ธฐ๋ณธ

[์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ] - 7. ์ƒˆ๋กœ์šด ํ• ์ธ ์ •์ฑ… ๊ฐœ๋ฐœ

์ƒˆ๋กœ์šด ํ• ์ธ ์ •์ฑ…์„ ํ™•์žฅํ•ด๋ณด์ž. ๊ฐ•์˜๋ฅผ ๋ณด๊ธฐ์ „์— ์Šค์Šค๋กœ ์ƒˆ๋กœ์šด ํ• ์ธ ์ •์ฑ… ( ์ •๋ฅ ํ• ์ธ ) ์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋Š” ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด ๋ณด์•˜๋‹ค. 1. RateDiscountPolicy ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ (๊ตฌํ˜„์ฒด ์ƒ์„ฑ) ์ด๊ฒƒ์€ DiscountPolicy Interface(์—ญํ• ) ๋ฅผ implements ํ•œ๋‹ค 2. RateDiscountPolicy ๊ตฌํ˜„์ฒด์˜ ๋ฉ”์„œ๋“œ๋ฅผ ์ž‘์„ฑํ•ด๋ณด์ž ( ์˜ค๋ฒ„๋ผ์ด๋”ฉ ) ์ฝ”๋“œ ๋”๋ณด๊ธฐ package hello2.demo.discount; import hello2.demo.member.Grade; import hello2.demo.member.Member; public class RateDiscountPolicy implements DiscountPolicy { private double DiscountRat..

Spring/Spring ๊ธฐ๋ณธ

[์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ] - 6. ์ฃผ๋ฌธ๊ณผ ํ• ์ธ ๋„๋ฉ”์ธ

์ฃผ๋ฌธ๊ณผ ํ• ์ธ ๋„๋ฉ”์ธ ์„ค๊ณ„ ์ฃผ๋ฌธ๊ณผ ํ• ์ธ ์ •์ฑ… ํšŒ์›์€ ์ƒํ’ˆ์„ ์ฃผ๋ฌธํ•  ์ˆ˜ ์žˆ๋‹ค. ํšŒ์› ๋“ฑ๊ธ‰์— ๋”ฐ๋ผ ํ• ์ธ ์ •์ฑ…์„ ์ ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. ํ• ์ธ ์ •์ฑ…์€ ๋ชจ๋“  VIP๋Š” 1000์›์„ ํ• ์ธํ•ด์ฃผ๋Š” ๊ณ ์ • ๊ธˆ์•ก ํ• ์ธ์„ ์ ์šฉํ•ด๋‹ฌ๋ผ. (๋‚˜์ค‘์— ๋ณ€๊ฒฝ ๋  ์ˆ˜ ์žˆ๋‹ค.) ํ• ์ธ ์ •์ฑ…์€ ๋ณ€๊ฒฝ ๊ฐ€๋Šฅ์„ฑ์ด ๋†’๋‹ค. ํšŒ์‚ฌ์˜ ๊ธฐ๋ณธ ํ• ์ธ ์ •์ฑ…์„ ์•„์ง ์ •ํ•˜์ง€ ๋ชปํ–ˆ๊ณ , ์˜คํ”ˆ ์ง์ „๊นŒ์ง€ ๊ณ ๋ฏผ์„ ๋ฏธ๋ฃจ๊ณ  ์‹ถ๋‹ค. ์ตœ์•…์˜ ๊ฒฝ์šฐ ํ• ์ธ์„ ์ ์šฉํ•˜์ง€ ์•Š์„ ์ˆ˜ ๋„ ์žˆ๋‹ค. (๋ฏธํ™•์ •) ์ฃผ๋ฌธ ๋„๋ฉ”์ธ ํ˜‘๋ ฅ, ์—ญํ• , ์ฑ…์ž„ 1. ์ฃผ๋ฌธ ์ƒ์„ฑ: ํด๋ผ์ด์–ธํŠธ๋Š” ์ฃผ๋ฌธ ์„œ๋น„์Šค์— ์ฃผ๋ฌธ ์ƒ์„ฑ์„ ์š”์ฒญํ•œ๋‹ค. 2. ํšŒ์› ์กฐํšŒ: ํ• ์ธ์„ ์œ„ํ•ด์„œ๋Š” ํšŒ์› ๋“ฑ๊ธ‰์ด ํ•„์š”ํ•˜๋‹ค. ๊ทธ๋ž˜์„œ ์ฃผ๋ฌธ ์„œ๋น„์Šค๋Š” ํšŒ์› ์ €์žฅ์†Œ์—์„œ ํšŒ์›์„ ์กฐํšŒํ•œ๋‹ค. 3. ํ• ์ธ ์ ์šฉ: ์ฃผ๋ฌธ ์„œ๋น„์Šค๋Š” ํšŒ์› ๋“ฑ๊ธ‰์— ๋”ฐ๋ฅธ ํ• ์ธ ์—ฌ๋ถ€๋ฅผ ํ• ์ธ ์ •์ฑ…์— ์œ„์ž„ํ•œ๋‹ค. 4...

Spring/Spring ๊ธฐ๋ณธ

[์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ] - 5. ํšŒ์› ๋„๋ฉ”์ธ ์‹คํ–‰๊ณผ ํ…Œ์ŠคํŠธ

ํšŒ์› ๋„๋ฉ”์ธ ์‹คํ–‰๊ณผ ํ…Œ์ŠคํŠธ ํšŒ์› ๋„๋ฉ”์ธ - ํšŒ์› ๊ฐ€์ž… main ๋”๋ณด๊ธฐ package hello2.demo; import hello2.demo.member.Grade; import hello2.demo.member.Member; import hello2.demo.member.MemberService; import hello2.demo.member.MemberServiceImpl; public class MemberApp { public static void main(String[] args) { MemberService memberService = new MemberServiceImpl(); Member member = new Member(1L, "memberA", Grade.VIP); memberServ..

Spring/Spring ๊ธฐ๋ณธ

[์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ] - 4. ํšŒ์› ๋„๋ฉ”์ธ ๊ฐœ๋ฐœ

ํšŒ์› ๋„๋ฉ”์ธ ๊ฐœ๋ฐœ ํšŒ์› ๋“ฑ๊ธ‰ (Grade.class) ๋”๋ณด๊ธฐ package hello2.demo.member; public enum Grade { BASIC, VIP } ํšŒ์› ์—”ํ‹ฐํ‹ฐ, ๋ฉค๋ฒ„ (Member) ๋”๋ณด๊ธฐ id, ์ด๋ฆ„, ๋“ฑ๊ธ‰ package hello2.demo.member; public class Member { private Long id; private String name; private Grade grade; public Member(Long id, String name, Grade grade) { this.id = id; this.name = name; this.grade = grade; } public Long getId() { return id; } public void setId(L..

hello_u
'Spring/Spring ๊ธฐ๋ณธ' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๊ธ€ ๋ชฉ๋ก (3 Page)