Spring

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..

Spring/Spring ๊ธฐ๋ณธ

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

์„น์…˜ 2. ์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ ์ดํ•ด 1 - ์˜ˆ์ œ ๋งŒ๋“ค๊ธฐ ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ https://endyd1567.tistory.com/98 ์Šคํ”„๋ง ์ž…๋ฌธ - (1) ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ start.spring.io ์ดˆ๊ธฐ ์„ค์ • ์–ด๋–ค ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ๋‹น๊ฒจ์„œ ์“ธ ๊ฒƒ์ธ๊ฐ€ Spring Web : ์›น ํ”„๋กœ์ ํŠธ Thymeleaf : HTML์„ ๋งŒ๋“ค์–ด์ฃผ๋Š” ํƒฌํ”Œ๋ฆฟ ์—”์ง„ ์„ค์น˜ ํ›„ buuld.gradle ํŒŒ์ผ Open main / test ๋‚˜๋ˆ ์ ธ ์žˆ์Œ resources : xml ,html , ์„ค endyd1567.tistory.com ๋น„์ฆˆ๋‹ˆ์Šค ์š”๊ตฌ์‚ฌํ•ญ๊ณผ ์„ค๊ณ„ ํšŒ์› ํšŒ์›์„ ๊ฐ€์ž…ํ•˜๊ณ  ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค. ํšŒ์›์€ ์ผ๋ฐ˜๊ณผ VIP ๋‘ ๊ฐ€์ง€ ๋“ฑ๊ธ‰์ด ์žˆ๋‹ค. ํšŒ์› ๋ฐ์ดํ„ฐ๋Š” ์ž์ฒด DB๋ฅผ ๊ตฌ์ถ•ํ•  ์ˆ˜ ์žˆ๊ณ , ์™ธ๋ถ€ ์‹œ์Šคํ…œ๊ณผ ์—ฐ๋™ํ•  ์ˆ˜ ์žˆ๋‹ค. (๋ฏธํ™•์ •) ์ฃผ๋ฌธ๊ณผ ํ• ์ธ ์ •์ฑ… ..

Spring/Spring ๊ธฐ๋ณธ

[์Šคํ”„๋ง ํ•ต์‹ฌ ์›๋ฆฌ - ๊ธฐ๋ณธํŽธ] - 2. ์ข‹์€ ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์˜ 5๊ฐ€์ง€ ์›์น™(SOLID)

์•…๋• ๋ฉด์ ‘๊ด€์—๊ฒŒ ๊ฑธ๋ฆฌ๋ฉด ๋ฌผ์–ด๋ณผ ์ˆ˜๋„ ์žˆ๋‹ค๋Š” ์ข‹์€ ๊ฐ์ฒด ์ง€ํ–ฅ ์„ค๊ณ„์˜ 5๊ฐ€์ง€ ์›์น™ SOLID๋ฅผ ์•Œ์•„๋ณด์ž... SRP ๋‹จ์ผ ์ฑ…์ž„ ์›์น™ ( single responsibilty principle ) - ํ•œ ํด๋ž˜์Šค๋Š” ํ•˜๋‚˜์˜ ์ฑ…์ž„๋งŒ ๊ฐ€์ ธ์•ผ ํ•œ๋‹ค. - ํ•˜๋‚˜์˜ ์ฑ…์ž„์ด๋ผ๋Š” ๊ฒƒ์€ ๋ชจํ˜ธํ•˜๋‹ค. ํด ์ˆ˜ ์žˆ๊ณ , ์ž‘์„ ์ˆ˜ ์žˆ๋‹ค. ๋ฌธ๋งฅ๊ณผ ์ƒํ™ฉ์— ๋”ฐ๋ผ ๋‹ค๋ฅด๋‹ค. - ์ค‘์š”ํ•œ ๊ธฐ์ค€์€ ๋ณ€๊ฒฝ์ด๋‹ค. ๋ณ€๊ฒฝ์ด ์žˆ์„ ๋•Œ ํŒŒ๊ธ‰ ํšจ๊ณผ๊ฐ€ ์ ์œผ๋ฉด ๋‹จ์ผ ์ฑ…์ž„ ์›์น™์„ ์ž˜ ๋”ฐ๋ฅธ ๊ฒƒ์ด๋‹ค. OCP ๊ฐœ๋ฐฉ-ํ์‡„ ์›์น™ ( open/closed principle ) - ์†Œํ”„ํŠธ์›จ์–ด ์š”์†Œ๋Š” ํ™•์žฅ์—๋Š” ์—ด๋ ค์žˆ์œผ๋‚˜ ๋ณ€๊ฒฝ์—๋Š” ๋‹ซํ˜€์žˆ์–ด์•ผ ํ•œ๋‹ค. " ์–ด๋–ป๊ฒŒ ์ฝ”๋“œ์˜ ๋ณ€๊ฒฝ ์—†์ด ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ์„๊นŒ? " - ๋‹คํ˜•์„ฑ์„ ํ™œ์šฉํ•ด ๋ณด์ž - ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ๊ตฌํ˜„ํ•œ ์ƒˆ๋กœ์šด ํด๋ž˜์Šค๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด์„œ(๊ธฐ์กด..

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