EEALL@ONCE

๐ŸŒฑ ์ˆœ์ˆ˜ํ•œ ์ž๋ฐ” ์ฝ”๋“œ๋กœ DIํ•œ ๋ถ€๋ถ„ ์Šคํ”„๋ง์œผ๋กœ ์ „ํ™˜ํ•˜๊ธฐ (์Šคํ”„๋ง ์ปจํ…์ŠคํŠธ/ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ) ๋ณธ๋ฌธ

Spring๐ŸŒฑ

๐ŸŒฑ ์ˆœ์ˆ˜ํ•œ ์ž๋ฐ” ์ฝ”๋“œ๋กœ DIํ•œ ๋ถ€๋ถ„ ์Šคํ”„๋ง์œผ๋กœ ์ „ํ™˜ํ•˜๊ธฐ (์Šคํ”„๋ง ์ปจํ…์ŠคํŠธ/ ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ)

์˜ฌ์—ฃ์›์Šค 2023. 7. 23. 14:57
728x90

@Configuration

 @Configuration์€ Java ์ฝ”๋“œ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋นˆ ๊ตฌ์„ฑ์„ ์ •์˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•ด์ฃผ๋Š” Spring์˜ ๊ฐ•๋ ฅํ•œ ์ฃผ์„

Java ํด๋ž˜์Šค์—์„œ @Configuration ์ฃผ์„์„ ์‚ฌ์šฉํ•˜๋ฉด ํด๋ž˜์Šค์— Spring Bean ๊ตฌ์„ฑ ๋ฉ”์†Œ๋“œ๊ฐ€ ํฌํ•จ๋˜์–ด ์žˆ์Œ์„ ๋‚˜ํƒ€๋ƒ„ 

์ฆ‰ ์—ฌ๊ธฐ์— Bean๋“ค์ด ์žˆ์–ด์š”! ๋ผ๊ณ  ์•Œ๋ ค์ฃผ๋Š” ์—ญํ• 

@Bean

@Bean์„ ์‚ฌ์šฉํ•˜๋ฉด Annotated ๋ฉ”์†Œ๋“œ๊ฐ€ ๋ฆฌํ„ดํ•œ ๊ฐ์ฒด๋ฅผ Spring Bean์œผ๋กœ ๊ด€๋ฆฌํ•˜๋„๋ก Spring์— ์ง€์‹œ
์ฆ‰ ์—ฌ๊ธฐ์— ํ•ด๋‹น ์ด๋ฆ„์˜ Bean์œผ๋กœ ๊ด€๋ฆฌํ•  ๊ฑฐ ์žˆ์–ด์š” ! ๋ผ๊ณ  ์•Œ๋ ค์ฃผ๋Š” ์—ญํ•  

๋ฉ”์„œ๋“œ๋Š” ๋นˆ์„ ์ƒ์„ฑํ•˜๊ณ  ๊ตฌ์„ฑํ•˜๊ธฐ ์œ„ํ•œ ํŒฉํ† ๋ฆฌ ์—ญํ• ์„ ํ•จ.
๋ฉ”์†Œ๋“œ์˜ ๋ฆฌํ„ด ํƒ€์ž…์€ ์ƒ์„ฑํ•  ๋นˆ์˜ ํƒ€์ž…์„ ๋‚˜ํƒ€๋ƒ„

 

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class AppConfig {
    /* ๊ฐ์ž ๋ถ€๋ถ„์„ ์ •ํ™•ํžˆ ๋‚˜๋ˆ” */

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

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

    @Bean
    public MemberService memberService() {
        return new MemberServiceImpl(memberRepository());
    }

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


}

 

๊ทธ๋Ÿผ main method์—์„œ ์–ด๋–ป๊ฒŒ ์‚ฌ์šฉํ• ๊นŒ? 

 

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);
        MemberService memberService = ac.getBean("memberService", MemberService.class);
        Member member = new Member(1L, "memberA", Grade.VIP);
        memberService.join(member);
        Member findMember = memberService.findMember(1L);
    }
}

 

AnnotationConfigApplicationContext๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด XML ๊ธฐ๋ฐ˜ ๊ตฌ์„ฑ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  Spring ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ์Œ
์Šคํ”„๋ง ์ปจํ…์ŠคํŠธ
: Spring ์ปจํ…์ŠคํŠธ๋Š” Spring ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋Ÿฐํƒ€์ž„ ํ™˜๊ฒฝ์„ ๋‚˜ํƒ€๋‚ด๋Š” ์ƒ์œ„ ์ˆ˜์ค€ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ์‘์šฉ ํ”„๋กœ๊ทธ๋žจ ๋‚ด์—์„œ ๋‹ค์–‘ํ•œ ๊ตฌ์„ฑ ์š”์†Œ ๋ฐ ๋นˆ์— ์•ก์„ธ์Šคํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ œ๊ณตํ•ฉ๋‹ˆ๋‹ค. Spring ์ปจํ…์ŠคํŠธ๋Š” ApplicationContext
์ธํ„ฐํŽ˜์ด์Šค์˜ ์ธ์Šคํ„ด์Šค์ด๋ฉฐ ์ข…์†์„ฑ ์ฃผ์ž…, ๋นˆ ์ˆ˜๋ช… ์ฃผ๊ธฐ ๊ด€๋ฆฌ, ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ ๋“ฑ์„ ํฌํ•จํ•˜์—ฌ Spring์˜ ํ•ต์‹ฌ ๊ธฐ๋Šฅ์—์„œ ์ค‘์š”ํ•œ ์—ญํ• ์„ ํ•ฉ๋‹ˆ๋‹ค.
์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ
: Spring ์ปจํ…Œ์ด๋„ˆ๋Š” ํŠนํžˆ Spring ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋‚ด์—์„œ Bean ๋ฐ ํ•ด๋‹น ์ข…์†์„ฑ์„ ๊ด€๋ฆฌํ•˜๋Š” ๋ฉ”์ปค๋‹ˆ์ฆ˜์„ ์ฐธ์กฐํ•˜๋Š” ์ €์ˆ˜์ค€ ๊ฐœ๋…์ž…๋‹ˆ๋‹ค. ์ปจํ…Œ์ด๋„ˆ๋Š”ApplicationContext ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ตฌํ˜„์ด๋ฉฐ ์ฃผ์š” ์ฑ…์ž„์€ ๊ตฌ์„ฑ ํŒŒ์ผ(XML ๋˜๋Š” Java ๊ธฐ๋ฐ˜ ๊ตฌ์„ฑ) ๋˜๋Š” ์ฃผ์„์„ ํ†ตํ•ด ์ œ๊ณต๋œ ์ •๋ณด๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ๋นˆ์„ ์ƒ์„ฑ, ๊ตฌ์„ฑ ๋ฐ ๊ด€๋ฆฌํ•˜๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

๋ณธ์งˆ์ ์œผ๋กœ Spring ์ปจํ…Œ์ด๋„ˆ๋Š” Spring ์ปจํ…์ŠคํŠธ์˜ ํ•„์ˆ˜์ ์ธ ๋ถ€๋ถ„์ž…๋‹ˆ๋‹ค. ์ปจํ…์ŠคํŠธ๋Š” ์ปจํ…Œ์ด๋„ˆ ๋ฐ ์ถ”๊ฐ€ ๊ธฐ๋Šฅ์„ ํฌํ•จํ•˜๋Š” ์ „์ฒด ๋Ÿฐํƒ€์ž„ ํ™˜๊ฒฝ์„ ๋‚˜ํƒ€๋‚ด๋ฉฐ ์ปจํ…Œ์ด๋„ˆ๋Š” ๊ฐœ์ฒด ์ƒ์„ฑ, ์ข…์†์„ฑ ์ฃผ์ž… ๋ฐ ๋นˆ ๊ด€๋ฆฌ์˜ ๊ธฐ๋ณธ ์ž‘์—…์„ ๋‹ด๋‹นํ•ฉ๋‹ˆ๋‹ค.

  • Spring Context๋Š” ์ปจํ…Œ์ด๋„ˆ, ์ด๋ฒคํŠธ ์ฒ˜๋ฆฌ, ๊ตญ์ œํ™” ์ง€์› ๋“ฑ๊ณผ ๊ฐ™์€ ๋‹ค์–‘ํ•œ ๊ตฌ์„ฑ ์š”์†Œ ๋ฐ ์„œ๋น„์Šค๋ฅผ ํฌํ•จํ•˜์—ฌ Spring Framework์—์„œ ์ œ๊ณตํ•˜๋Š” ์ „์ฒด ๋Ÿฐํƒ€์ž„ ํ™˜๊ฒฝ์„ ์˜๋ฏธํ•ฉ๋‹ˆ๋‹ค.
  • Spring Container๋Š” ๊ตฌ์ฒด์ ์œผ๋กœ IoC(Inversion of Control) ๋ฐ DI(Dependency Injection) ์›์น™์— ๋”ฐ๋ผ Bean ๋ฐ ํ•ด๋‹น ์ข…์†์„ฑ์„ ๊ด€๋ฆฌํ•˜๋Š” ์ปจํ…์ŠคํŠธ ๋ถ€๋ถ„์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

์ฆ‰ ์ปจํ…์ŠคํŠธ ์†์˜ ์ปจํ…Œ์ด๋„ˆ๋‹ค. ์ „์ฒด์ ์ธ ์ฒ˜๋ฆฌ ๊ณผ์ •์—์„œ ์ €์žฅ๊ณ ๋ž„๊นŒ.. 

ApplicationContext ac = new AnnotationConfigApplicationContext(AppConfig.class);

๋‚ด ๋ง˜๋Œ€๋กœ ํ•ด์„: 

์–ด๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜์œผ๋กœ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ปจํ…์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ฑด๋ฐ, ํ•ด๋‹น ์ปจํ…์ŠคํŠธ ์†์—์„œ ๋‹ค์–‘ํ•œ ๊ตฌ์„ฑ ์š”์†Œ ๋ฐ ๋นˆ์— ์•ก์„ธ์Šคํ•˜๊ณ  ๊ด€๋ฆฌํ•ด์•ผํ•˜๋Š”๋ฐ, ๊ทธ ๊ตฌ์„ฑ ์š”์†Œ๋ž‘ ๋นˆ์ด ์žˆ์˜ ์ •๋ณด๊ฐ€ ์ ํ˜€ ์žˆ๋Š” ๊ณณ์ด AppCnfig.class ์•ผ.

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

 

๋‚ด ๋ง˜๋Œ€๋กœ ํ•ด์„: 

์ด์ œ bean๋“ค์„ ApplicationContext๊ฐ€ ๋‹ค ๊ด€๋ฆฌํ•˜๊ณ  ์žˆ๊ณ , ๊ฑฐ๊ธฐ์„œ ๋‚ด๊ฐ€ ์›ํ•˜๋Š” Bean์„ ๊บผ๋‚ด ์“ธ๊ฑด๋ฐ,
getBean(method name, ์›ํ•˜๋Š” ์œ ํ˜•์˜ ๋นˆ)์„ ์‚ฌ์šฉํ•˜๋ฉด Spring ์ปจํ…์ŠคํŠธ์—์„œ MemberService์œ ํ˜•์˜ ๋นˆ์„ ๊ฒ€์ƒ‰ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค. 

728x90