[Spring] ๐ŸŒฑ Spring์˜ ํ•ต์‹ฌ ๊ฐœ๋… (Spring Container, Spring Bean)

์กฐ๋ฏผ๊ฒฝยท2025๋…„ 3์›” 13์ผ
0

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
11/13

Chapter 1 : ๐Ÿ“ฆ Spring Container


๐Ÿ“š Spring Container์€ Spring์œผ๋กœ ๊ตฌ์„ฑ๋œ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์—์„œ ๊ฐ์ฒด(Bean)๋ฅผ ์ƒ์„ฑ, ๊ด€๋ฆฌ, ์†Œ๋ฉธํ•˜๋Š” ์—ญํ• ์„ ๋‹ด๋‹นํ•œ๋‹ค. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹œ์ž‘ ์‹œ, ์„ค์ • ํŒŒ์ผ์ด๋‚˜ Annotation์„ ์ฝ์–ด Bean์„ ์ƒ์„ฑํ•˜๊ณ  ์ฃผ์ž…ํ•˜๋Š” ๋ชจ๋“  ๊ณผ์ •์„ ์ปจํŠธ๋กคํ•œ๋‹ค.



๐Ÿš€ Java ๊ฐ์ฒด ์ƒ์„ฑ ๋ฐฉ์‹ ๋น„๊ต

1๏ธโƒฃ ๊ธฐ์กด Java ๊ฐ์ฒด ์ƒ์„ฑ (์ง์ ‘ ์ƒ์„ฑ)

โœ… ์‚ฌ์šฉํ•˜๋Š” ํด๋ž˜์Šค์—์„œ ์ง์ ‘ ๊ฐ์ฒด๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

โœ… ๊ฐ์ฒด ๊ฐ„์˜ ์˜์กด์„ฑ์ด ๋†’์•„์ง€๊ณ  ๊ฒฐํ•ฉ๋„๊ฐ€ ์ฆ๊ฐ€ โ†’ ์œ ์ง€๋ณด์ˆ˜ ์–ด๋ ค์›€

public class OrderService {
    private final PaymentService paymentService = new PaymentService(); // ์ง์ ‘ ์ƒ์„ฑ
}

2๏ธโƒฃ Spring Container๋ฅผ ํ™œ์šฉํ•œ ๊ฐ์ฒด ์ƒ์„ฑ

โœ… Spring Container๊ฐ€ ๊ฐ์ฒด๋ฅผ ๋Œ€์‹  ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌ

โœ… ๋А์Šจํ•œ ๊ฒฐํ•ฉ(OCP, DIP ์›์น™ ์ค€์ˆ˜)์œผ๋กœ ์œ ์—ฐํ•œ ์„ค๊ณ„ ๊ฐ€๋Šฅ

@Component
public class OrderService {
    private final PaymentService paymentService;

    @Autowired
    public OrderService(PaymentService paymentService) { // ์˜์กด์„ฑ ์ฃผ์ž…
        this.paymentService = paymentService;
    }
}

โœ… @Autowired๋ฅผ ํ†ตํ•ด Spring Container๊ฐ€ ์ž๋™์œผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž…




๐Ÿ”— Spring Container์˜ ์—ญํ• 

๐Ÿ“Œ ๊ฐ์ฒด(Bean)๋ฅผ ์ƒ์„ฑ ๋ฐ ๊ด€๋ฆฌํ•˜๊ณ  ์˜์กด์„ฑ์„ ์ฃผ์ž…ํ•˜๋Š” ์—ญํ• ์„ ๋‹ด๋‹นํ•œ๋‹ค.


๐Ÿฝ๏ธ ์ฃผ๋ฐฉ์˜ Chef์˜ ์—ญํ• ๊ณผ ๊ฐ™๋‹ค

โœ… ์žฌ๋ฃŒ(Bean) ์„ ํƒ โ†’ ์š”๋ฆฌ(๊ฐ์ฒด ์ƒ์„ฑ ๋ฐ ๊ด€๋ฆฌ) โ†’ ์™„์„ฑ(์˜์กด์„ฑ ์ฃผ์ž… ๋ฐ ์ œ๊ณต)๊นŒ์ง€ ๋ชจ๋“  ๊ฒƒ์„ ๊ด€๋ฆฌํ•œ๋‹ค.

โœ… ๊ฐ์ฒด๋ฅผ ์ง์ ‘ ์ƒ์„ฑํ•˜๋Š” ๊ฒฝ์šฐ, ๊ฐ์ฒด ๊ฐ„์˜ ์˜์กด์„ฑ ๋ฐ ๊ฒฐํ•ฉ๋„๊ฐ€ ๋†’์•„์ง€๊ณ  OCP, DIP์„ ์œ„๋ฐ˜ํ•˜๊ฒŒ ๋œ๋‹ค.

โœ… Spring Container๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ์ธํ„ฐํŽ˜์ด์Šค์—๋งŒ ์˜์กดํ•˜๋Š” ์„ค๊ณ„๊ฐ€ ๊ฐ€๋Šฅํ•ด์ ธ OCP, DIP ์ค€์ˆ˜ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.




๐Ÿท๏ธ Spring Container์˜ ์ข…๋ฅ˜

๐Ÿ› ๏ธ BeanFactory

  • Spring Container์˜ ์ตœ์ƒ์œ„ ์ธํ„ฐํŽ˜์ด์Šค

  • Spring Bean์„ ๊ด€๋ฆฌํ•˜๊ณ  ์กฐํšŒํ•œ๋‹ค.


โšก ApplicationContext

  • BeanFactory์˜ ํ™•์žฅ๋œ ํ˜•ํƒœ(implements) => BeanFactory์˜ ๊ธฐ๋Šฅ์„ ๊ฐ€์ง€๊ณ  ์žˆ์Œ

  • Application ๊ฐœ๋ฐœ์— ํ•„์š”ํ•œ ๋‹ค์–‘ํ•œ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€์ ์œผ๋กœ ์ œ๊ณตํ•œ๋‹ค.

    • ๊ตญ์ œํ™”, ํ™˜๊ฒฝ๋ณ€์ˆ˜ ๋ถ„๋ฆฌ, ์ด๋ฒคํŠธ, ๋ฆฌ์†Œ์Šค ์กฐํšŒ

๐Ÿ’ก ์ผ๋ฐ˜์ ์œผ๋กœ ApplicationContext๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ๋•Œ๋ฌธ์— ApplicationContext๋ฅผ Spring Container๋ผ ํ‘œํ˜„ํ•œ๋‹ค.




Chapter 2 : ๐ŸŒฑ Spring bean


๐Ÿ“š Spring Bean์€ Spring ์ปจํ…Œ์ด๋„ˆ๊ฐ€ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด๋ฅผ ์˜๋ฏธํ•œ๋‹ค. ์ž๋ฐ” ๊ฐ์ฒด ์ž์ฒด๋Š” ํŠน๋ณ„ํ•˜์ง€ ์•Š์ง€๋งŒ, Spring์ด ์ด ๊ฐ์ฒด๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์ˆœ๊ฐ„๋ถ€ํ„ฐ Bean์ด ๋œ๋‹ค. Spring์€ Bean์„ ์ƒ์„ฑ, ์ดˆ๊ธฐํ™”, ์˜์กด์„ฑ ์ฃผ์ž… ๋“ฑ์„ ํ†ตํ•ด ๊ด€๋ฆฌํ•œ๋‹ค.



โœ”๏ธ Spring Bean์ด๋ž€?

Spring Container๊ฐ€ ์ƒ์„ฑํ•˜๊ณ  ๊ด€๋ฆฌํ•˜๋Š” Java ๊ฐ์ฒด

  • Bean์€ new ํ‚ค์›Œ๋“œ ๋Œ€์‹  ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

  • Spring Container๊ฐ€ ์ œ์–ดํ•œ๋‹ค.




๐Ÿ”— Spring Bean์˜ ์—ญํ• 

  • Chef์ธ Spring Container๊ฐ€ ์š”๋ฆฌํ•  ์Œ์‹์— ์‚ฌ์šฉ๋  ์žฌ๋ฃŒ(Bean)

    • ์š”๋ฆฌ(Application)์˜ ํ•ต์‹ฌ์„ ์ด๋ฃจ๋Š” ์žฌ๋ฃŒ(Bean)




โšก Spring Bean์˜ ํŠน์ง•

โœ… Spring ์ปจํ…Œ์ด๋„ˆ์— ์˜ํ•ด ๊ด€๋ฆฌ๋จ

โœ… ๊ธฐ๋ณธ์ ์œผ๋กœ Singleton(๋‹จ์ผ ๊ฐ์ฒด๋กœ ๊ด€๋ฆฌ)์œผ๋กœ ์„ค์ •๋œ๋‹ค.

โœ… DI(์˜์กด์„ฑ ์ฃผ์ž…)๋ฅผ ํ†ตํ•ด ๋‹ค๋ฅธ ๊ฐ์ฒด์™€ ์—ฐ๊ฒฐ ๊ฐ€๋Šฅ

โœ… ์ƒ์„ฑ โ†’ ์ดˆ๊ธฐํ™” โ†’ ์‚ฌ์šฉ โ†’ ์†Œ๋ฉธ์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง




๐Ÿ“ Bean ๋“ฑ๋ก ๋ฐฉ๋ฒ•

๐Ÿ“Œ 1. XML ๋ฐฉ์‹

<beans>
    <!-- myBean์ด๋ผ๋Š” ์ด๋ฆ„์˜ Bean ์ •์˜ -->
    <bean id="myBean" class="com.example.MyBean" />
</beans>
public class MyApp {
    public static void main(String[] args) {
        // Spring ์ปจํ…Œ์ด๋„ˆ์—์„œ Bean์„ ๊ฐ€์ ธ์˜ด
        ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
        MyService myService = context.getBean("myService", MyService.class);
        myService.doSomething();
    }
}

๐Ÿ“Œ 2. Annotation ๋ฐฉ์‹

๐Ÿ” @Component ์ž๋™ ๋“ฑ๋ก + @Autowired ์˜์กด์„ฑ ์ฃผ์ž…

// ์ด ํด๋ž˜์Šค๋ฅผ Bean์œผ๋กœ ๋“ฑ๋ก
// @Controller, @Service, @Repository
@Component
public class MyService {

    public void doSomething() {
        System.out.println("Spring Bean ์œผ๋กœ ๋™์ž‘");
    }
    
}
@Component
public class MyApp {

    private final MyService myService;

    @Autowired // ์˜์กด์„ฑ ์ž๋™ ์ฃผ์ž…
    public MyApp(MyService myService) {
        this.myService = myService;
    }

    public void run() {
        myService.doSomething();
    }
}
// com.example ํŒจํ‚ค์ง€๋ฅผ ์Šค์บ”ํ•˜์—ฌ Bean ๋“ฑ๋ก
@Configuration
@ComponentScan(basePackages = "com.example")
public class AppConfig {

    public static void main(String[] args) {
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        MyApp app = context.getBean(MyApp.class);
        app.run();
    }
    
}

๐Ÿ“Œ 3. Java ์„ค์ • ํŒŒ์ผ ๋ฐฉ์‹

๐Ÿ”น @Bean ์‚ฌ์šฉํ•˜์—ฌ ์ˆ˜๋™ ๋“ฑ๋ก

@Configuration
public class AppConfig {

    @Bean
    public MyService myService() {
        return new MyService();
    }
}
public class MyApp {
    public static void main(String[] args) {
        // Spring ์ปจํ…Œ์ด๋„ˆ์—์„œ Bean์„ ๊ฐ€์ ธ์˜ด
        AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
        MyService myService = context.getBean(MyService.class);
        myService.doSomething();
    }
}

0๊ฐœ์˜ ๋Œ“๊ธ€