JiwonDev

#5 ์Šคํ”„๋ง์œผ๋กœ ์ „ํ™˜ํ•˜๊ธฐ

by JiwonDev

2021.07.25 - [Backend/Spring Core] - #0 ์Šคํ”„๋ง, ์ž๋ฐ”์˜ ๋ด„์ด ์˜ค๊ธฐ๊นŒ์ง€

 

#0 ์Šคํ”„๋ง, ์ž๋ฐ”์˜ ๋ด„์ด ์˜ค๊ธฐ๊นŒ์ง€

ํ•ด๋‹น ๊ธ€์€ ๊น€์˜ํ•œ๋‹˜์˜ ์ธํ”„๋Ÿฐ ๊ฐ•์˜๋ฅผ ๊ณต๋ถ€ํ•˜๊ณ  ์ •๋ฆฌํ•œ ๋‚ด์šฉ์ž…๋‹ˆ๋‹ค. # EJB ์ง€์˜ฅ ์ž๋ฐ”๊ฐ€ ๋‚˜์˜จ 2000๋…„๋Œ€, ๊ทธ ๋‹น์‹œ ์ž๋ฐ” ์ง„ํ˜•์—์„œ๋Š” EJB (Enterprise Java Beans) ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ๋น„์ฆˆ๋‹ˆ์Šค ๊ธฐ์ˆ  ์ŠคํŽ™์œผ๋กœ ์ œ๊ณตํ•˜

jiwondev.tistory.com

์Šคํ”„๋ง์ด ๊ฐ€์ง„ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์€, ์›น ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ๋งŒ๋“ค๊ฑฐ๋‚˜ DB๋ฅผ ํŽธํ•˜๊ฒŒ ์ ‘๊ทผํ•ด์ฃผ๊ธฐ ์œ„ํ•œ ๋„๊ตฌ๊ฐ€ ์•„๋‹ˆ๋‹ค. ์ด๋Š” ๊ฐ์ฒด์ง€ํ–ฅ์ ์œผ๋กœ ์„ค๊ณ„ํ•ด์„œ ์–ป๋Š” ์ด์ ์ผ ๋ฟ์ด๊ณ  ์Šคํ”„๋ง์˜ ๋ณธ์งˆ์€ '์ˆœ์ˆ˜ํ•œ Java๋กœ ์ข‹์€ ๊ฐ์ฒด์ง€ํ–ฅ์„ ์„ค๊ณ„ํ•˜๋„๋ก ๋„์™€์ฃผ๋Š” ํ”„๋ ˆ์ž„์›Œํฌ' ์ด๋‹ค.

 


# AppConfig๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋กœ ๋งŒ๋“ค๊ธฐ

๊ฐ„๋‹จํ•˜๋‹ค. ์–ด๋…ธํ…Œ์ด์…˜(@)์„ ์ด์šฉํ•ด์„œ @Configuration์„ ์ ์–ด์ฃผ๊ณ , ๊ฐ๊ฐ์˜ ๋ฉ”์„œ๋“œ์— @Bean์„ ๋ถ™์—ฌ์ค€๋‹ค. @Bean์„ ๋ถ™์—ฌ์ค€ ๋ฉ”์„œ๋“œ์˜ ๋ฐ˜ํ™˜ ๊ฐ’์ด ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ์Šคํ”„๋ง ๋นˆ(๊ตฌํ˜„์ฒด)์œผ๋กœ ๋“ฑ๋ก๋œ๋‹ค.

@Configuration
public class AppConfig {

    // ๋ณดํ†ต ๊ธฐ๋ณธ๊ฐ’์„ ์‚ฌ์šฉํ•˜์ง€๋งŒ, ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋  ์ด๋ฆ„์„ ๋ฐ”๊ฟ€ ์ˆ˜๋„ ์žˆ๋‹ค.
    @Bean(name="newName")
    public MemberService memberService() {
        return new MemberServiceImpl(memberRepository());
    }

    @Bean
    public OrderService orderService() {
        return new OrderServiceImpl(
                memberRepository(),
                discountPolicy());
    }

    @Bean
    public MemberRepository memberRepository() {
        return new MemoryMemberRepository();
    }

    @Bean
    public DiscountPolicy discountPolicy() {
        return new RateDiscountPolicy();
    }
}

 


# Main, ์•ฑ ์ง„์ž…์ ์„ ์Šคํ”„๋ง์œผ๋กœ ๋ฐ”๊พธ๊ธฐ

์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” ์•ฑ์˜ ์ „์ฒด ํ๋ฆ„์„ ์ œ์–ดํ•œ๋‹ค๋Š” ์˜๋ฏธ๋กœ ApplicationContext ๋ผ๋Š” ์ด๋ฆ„์„ ์‚ฌ์šฉํ•œ๋‹ค.

์•ฑ ์‹œ์ž‘์ ์„ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ด์šฉํ•˜๊ฒŒ ๋ณ€๊ฒฝํ•˜๋ ค๋ฉด ์•„๋ž˜์™€ ๊ฐ™์ด ํ•ด์ฃผ๋ฉด ๋œ๋‹ค. ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ ๊ฐ์ฒด์— AppConfig.class๋ฅผ ๋„˜๊ฒจ์ฃผ๊ฒŒ ๋˜๋Š”๋ฐ, ์ด๋Š” ํ•ด๋‹น ๊ฐ์ฒด๊ฐ€ ๊ฐ€์ง€๊ณ ์žˆ๋Š” 'Class' ๊ฐ์ฒด๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค. (์ฐธ๊ณ ๋กœ Class ๊ฐ์ฒด๋Š” ์ž๋ฐ”์—์„œ ํŠน์ • ํด๋ž˜์Šค์˜ ์ •๋ณด๋ฅผ ์ €์žฅํ•˜์—ฌ ๋„˜๊ฒจ์ค„ ์ˆ˜ ์žˆ๋„๋ก ๋งŒ๋“  ๊ฐ์ฒด์ด๋‹ค.)

import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;

public class MemberApp {
    public static void main(String[] args) {

        // AppConfig appConfig = new AppConfig();
        // MemberService memberService = appConfig.memberService();
        ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);
        
        // ๊ฐ์ฒด์—์„œ @Bean ์œผ๋กœ ๋“ฑ๋ก๋œ ๋ฉ”์„œ๋“œ๋ฅผ ๋ฌธ์ž์—ด๋กœ ์ฐพ์•„ ๊ฐ€์ ธ์˜จ๋‹ค. -> memberService() ๋ฉ”์„œ๋“œ
        MemberService memberService = ac.getBean("memberService", MemberService.class);
		
        // ๊ธฐ์กด์˜ ํ…Œ์ŠคํŠธ ๋ฐฉ๋ฒ•. ์ด๋Ÿฐ ๋ฌด์‹ํ•œ ๋ฐฉ๋ฒ• ๋Œ€์‹ ์— ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜์ž.
        Member member = new Member(1L, "memberA", Grade.VIP);
        memberService.join(member);

        Member findMember = memberService.findMember(1L);
        System.out.println("new member = " + member.getName());
        System.out.println("findMember = " + findMember.getName());
    }
}
public class OrderApp {
    public static void main(String[] args) {
        // AppConfig appConfig = new AppConfig();
        // MemberService memberService = appConfig.memberService();
        // OrderService orderService = appConfig.orderService();

        ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

        MemberService memberService = ac.getBean("memberService", MemberService.class);
        OrderService orderService = ac.getBean("orderService", OrderService.class);

        
        // ๊ธฐ์กด์˜ ํ…Œ์ŠคํŠธ ๋ฐฉ๋ฒ•. ์ด๋Ÿฐ ๋ฌด์‹ํ•œ ๋ฐฉ๋ฒ• ๋Œ€์‹ ์— ํ…Œ์ŠคํŠธ ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•˜์ž.
        long memberId = 1L;
        Member member = new Member(memberId, "memberA", Grade.VIP);
        memberService.join(member);

        Order order = orderService.createOrder(memberId, "itemA", 10000);
        System.out.println("order = " + order);
    }
}

ํ•ด๋‹น ์ฝ”๋“œ๋ฅผ ๋Œ๋ ค๋ณด๋ฉด ์Šคํ”„๋ง์— ์ปจํ…Œ์ด๋„ˆ(AppConfig ์—ญํ• )์— ๋“ฑ๋ก ๋˜์—ˆ์Œ์„ ์ฝ˜์†”์— ํ™•์ธ ํ•  ์ˆ˜์žˆ๋‹ค.

singleton bean์œผ๋กœ ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋ก๋˜์—ˆ๋‹ค.


# ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ

์ง์ ‘ Java์ฝ”๋“œ๋กœ ์ž‘์„ฑํ•˜๋˜ ๊ฒƒ์„ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋กœ ์ „ํ™˜ํ•˜๋ฉด์„œ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐ”๋€Œ์—ˆ๋‹ค.

  • ์šฐ์„  ApplicationContext๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ผ๊ณ  ํ•œ๋‹ค.
  • ๊ธฐ์กด์—๋Š” AppConfig ์ฝ”๋“œ๋ฅผ ์ง์ ‘ ์ž‘์„ฑํ•˜์—ฌ ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ–ˆ๋‹ค๋ฉด, ์ด๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋กœ ๋Œ€์ฒดํ•˜๋Š” ๊ฒƒ์ด๋‹ค.
  • ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋Š” @Configuration์ด ๋ถ™์€ ๊ฐ์ฒด๋ฅผ ์„ค์ • ์ •๋ณด(AppConfig)๋กœ ์‚ฌ์šฉํ•œ๋‹ค.
  • ํ•ด๋‹น ๊ฐ์ฒด์—์„œ @Bean์ด ๋ถ™์€ ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•ด ๋ฐ˜ํ™˜๋œ ๊ฐ์ฒด๋ฅผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋“ฑ๋กํ•œ๋‹ค.
    - ์ด๋ ‡๊ฒŒ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๋ฉ”์„œ๋“œ ํ˜ธ์ถœ๋กœ ๋“ฑ๋ก๋œ ๊ฐ์ฒด๋ฅผ Spring Bean์ด๋ผ๊ณ  ํ•œ๋‹ค.
    - ์Šคํ”„๋ง ๋นˆ์€ ๊ด€๋ก€์ ์œผ๋กœ ๋ฉ”์„œ๋“œ๋ช…์„ ์ด๋ฆ„์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค. (name=์œผ๋กœ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.)

  • ApplicationContext์˜ getBean() ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด ์›ํ•˜๋Š” ๋นˆ์„ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
  • ๊ธฐ์กด์—๋Š” ๊ฐœ๋ฐœ์ž๊ฐ€ ์ง์ ‘ ๊ตฌ์„ฑ์˜์—ญ / ์‹คํ–‰์˜์—ญ์„ ๋ถ„๋ฆฌํ•˜์˜€๋‹ค๋ฉด, ์ด์ œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์— ๊ฐ์ฒด๋ฅผ ์Šคํ”„๋ง ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜์—ฌ ํ•„์š”ํ•  ๋•Œ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ(AppContext)์—์„œ ํ•„์š”ํ•œ ๊ตฌํ˜„์ฒด(Spring Bean)์„ ์ฐพ์•„ ์‚ฌ์šฉํ•˜๋„๋ก ๋ณ€๊ฒฝ๋˜์—ˆ๋‹ค.

# ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ์˜ ์žฅ์ 

์ด๋ ‡๊ฒŒ ํ•˜๋ฉด์„œ ์–ด๋…ธํ…Œ์ด์…˜(@)์„ ์‚ฌ์šฉํ•˜๊ฒŒ ๋˜์—ˆ๊ณ , ์ฝ”๋“œ๊ฐ€ ์กฐ๊ธˆ ๋” ๋ณต์žกํ•ด์ง„ ๊ฒƒ ๊ฐ™๋‹ค. ๊ทธ๋Ÿผ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์‚ฌ์šฉํ•ด์„œ ์–ป๋Š” ์žฅ์ ์ด ๋ฌด์—‡์ผ๊นŒ?

 

๊ฒฐ๋ก ๋ถ€ํ„ฐ ๋งํ•˜์ž๋ฉด ์ด๋ ‡๊ฒŒ ๋ฒ”์šฉ ํ”„๋ ˆ์ž„์›Œํฌ๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด์„œ ์ •๋ง ์–ด๋งˆ์–ด๋งˆํ•œ ์žฅ์ ์„ ์–ป์„ ์ˆ˜ ์žˆ๋Š”๋ฐ, ๋‹ค ์„ค๋ช…ํ•˜๊ธฐ์—๋Š” ๋‚ด์šฉ์ด ๋„ˆ๋ฌด ๊ธธ์–ด์ง„๋‹ค. ์ด๋Š” ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ์ดํ•ดํ•˜๋ฉด์„œ ํ•˜๋‚˜์”ฉ ์•Œ์•„๋ณด๋„๋ก ๋” ํ•˜์ž. 

๋ธ”๋กœ๊ทธ์˜ ์ •๋ณด

JiwonDev

JiwonDev

ํ™œ๋™ํ•˜๊ธฐ