MapleStory Finger Point

๐ŸŸค JAVA/๐ŸŸค Spring

[Spring] IoC (์ œ์–ด์˜์—ญ์ „), DI (์˜์กด์„ฑ์ฃผ์ž…)

HYEJU01 2024. 8. 6. 13:18

๐Ÿ’ป  IoC (Inversion of Control) ์ œ์–ด์˜ ์—ญ์ „

  • ๊ฐ์ฒด์˜ ์ƒ์„ฑ๊ณผ ์˜์กด์„ฑ ๊ด€๋ฆฌ์˜ ์ œ์–ด๋ฅผ ํ”„๋ ˆ์ž„์›Œํฌ / ์ปจํ…Œ์ด๋„ˆ์— ๋งก๊น€ (๊ฐœ๋ฐœ์ž๋Š” ๊ด€์—ฌํ•˜์ง€ ์•Š๋Š”๋‹ค)
  • ๊ฐ์ฒด๋ฅผ ํ•„์š”ํ•  ๋•Œ ์ƒ์„ฑํ•ด์„œ ์‚ฌ์šฉํ•˜๋˜ ๋ฐฉ์‹์„ ๋ฏธ๋ฆฌ ์ƒ์„ฑํ•ด ๋†“๊ณ  ๊บผ๋‚ด์„œ ์‚ฌ์šฉํ•˜๋Š” ํ˜•ํƒœ๋กœ ๋ณ€๊ฒฝ

 

 

 


 

 

๐Ÿ’ป DI (Dependency Injection) ์˜์กด์„ฑ ์ฃผ์ž…

DI : Dependency Injection

 

  • IoC ๋ฅผ ๊ตฌํ˜„ํ•˜๋Š” ๋ฐฉ์‹ ์ค‘ ํ•˜๋‚˜
  • ์ž์‹ ์˜ ์˜์กด์„ฑ์„ ์™ธ๋ถ€์—์„œ ์ฃผ์ž… ๋ฐ›๋Š” ๋ฐฉ์‹์ด๋‹ค.
  • ์Šคํ”„๋ง Container ์— ๋งŒ๋“ค์–ด๋‘” ๊ฐ์ข… ํด๋ž˜์Šค(bean)๋“ค์€ ์„œ๋กœ ์˜์กด์ ์ด๋‹ค.
  • ํ”„๋กœ๊ทธ๋ž˜๋ฐ์—์„œ ๊ฐ์ฒด๋ฅผ ๋งŒ๋“ค์–ด์„œ ์™ธ๋ถ€์—์„œ ๋”ฐ๋กœ ์ฃผ์ž…ํ•˜๋Š” ๋ฐฉ๋ฒ•

 

 

 

 

 

๐Ÿ’ป ์˜์กด์„ฑ ์ฃผ์ž… 2๊ฐ€์ง€ ๋ฐฉ๋ฒ•

1) ์ƒ์„ฑ์ž๋ฅผ ํ†ตํ•œ ์˜์กด์„ฑ ์ฃผ์ž…

2) setter๋ฅผ ํ†ตํ•œ ์˜์กด์„ฑ ์ฃผ์ž… 

 

์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ์‹

 

pom.xml ์— ์ข…์†์„ฑ ์ถ”๊ฐ€ (spring framework)

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.25</version>
    </dependency>
</dependencies>

 

 

์˜์กด์„ฑ ์ฃผ์ž…์„ ์œ„ํ•œ ์ธํ„ฐํŽ˜์ด์Šค / ํด๋ž˜์Šค ์ •์˜ 

// Engine.java
public interface Engine {
    void start();
}

// EngineImpl.java
public class EngineImpl implements Engine {
    public void start() {
        System.out.println("Engine started");
    }
}

// Car.java
public class Car {
    private Engine engine;
    
    // ์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ์‹์œผ๋กœ DI ์ฒ˜๋ฆฌ
    public Car(Engine engine) {
        this.engine = engine;
    }
    
    public void drive() {
        engine.start();
        System.out.println("Car is driving");
    }
}

 

 

`ApplicationContext` = IoC ์ปจํ…Œ์ด๋„ˆ ๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์˜์กด์„ฑ ์ฃผ์ž… ์„ค์ •

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class Main {
    public static void main(String[] args) {
        // IoC ์ปจํ…Œ์ด๋„ˆ ์„ค์ •
        ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");

        // ์˜์กด์„ฑ ์ฃผ์ž…๋œ Car ๊ฐ์ฒด ๊ฐ€์ ธ์˜ค๊ธฐ
        Car car = context.getBean(Car.class);
        car.drive();
    }
}

 

 

XML ์„ค์ • ํŒŒ์ผ์—์„œ Car ๊ฐ์ฒด์™€ Engine ๊ฐ์ฒด๋ฅผ ์„ค์ •ํ•˜๊ณ  ์˜์กด์„ฑ์„ ์ฃผ์ž…

 

<!-- beans.xml -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Engine ๋นˆ ์ •์˜ -->
    <bean id="engine" class="EngineImpl" />

    <!-- Car ๋นˆ ์ •์˜ (์ƒ์„ฑ์ž ์ฃผ์ž… ๋ฐฉ์‹) -->
    <bean id="car" class="Car">
        <constructor-arg ref="engine" />
    </bean>

</beans>

 

 

์„ธํ„ฐ ์ฃผ์ž… ๋ฐฉ์‹

// Car.java (์„ธํ„ฐ ์ฃผ์ž…)
public class Car {
    private Engine engine;

    // ์„ธํ„ฐ ๋ฉ”์„œ๋“œ๋กœ DI ์ฒ˜๋ฆฌ
    public void setEngine(Engine engine) {
        this.engine = engine;
    }

    public void drive() {
        engine.start();
        System.out.println("Car is driving");
    }
}

 

xml ํŒŒ์ผ์—์„œ ์ƒ์„ฑ์ž ์ฃผ์ž… ๋Œ€์‹  ์„ธํ„ฐ ์ฃผ์ž… ์‚ฌ์šฉ

<!-- beans.xml -->
<bean id="car" class="Car">
    <property name="engine" ref="engine" />
</bean>

 

 

๐Ÿ’ป DI ์„ค์ • ๋ฐฉ๋ฒ• 

 

 

 

 

๐Ÿ’ป ์˜์กด๊ฐ์ฒด ์ž๋™ ์ฃผ์ž…

<constructor-org> , <property> ํƒœ๊ทธ๋กœ ์˜์กด ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ๋ช…์‹œํ•˜์ง€ ์•Š์•„๋„

์ž๋™์œผ๋กœ ํ•„์š”ํ•œ ์˜์กด ๋Œ€์ƒ ๊ฐ์ฒด๋ฅผ ์ฐพ์•„์„œ, ํ•„์š”ํ•œ ๊ฐ์ฒด์—๊ฒŒ ์ฃผ์ž…ํ•ด์ฃผ๋Š” ๊ธฐ๋Šฅ

 

@Autowired

@Resource 

์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ์‰ฝ๊ฒŒ ๊ตฌํ˜„ ๊ฐ€๋Šฅ

 

 

 

๐Ÿ’ปDI ์ž๋™ ์ฃผ์ž… ์„ค์ • ๋ฐฉ๋ฒ•

 

 

๐Ÿ’ป์˜์กด ๊ฐ์ฒด ์ž๋™ ์ฃผ์ž… ํƒœ๊ทธ

 

@Autowired (required = false) ํƒ€์ž…์„ ๊ธฐ์ค€์œผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž…, ๊ฐ™์€ ํƒ€์ž…์˜ ๋นˆ์ด ๋‘๊ฐœ ์ด์ƒ ์žˆ์„ ๊ฒฝ์šฐ "๋ณ€์ˆ˜์ด๋ฆ„"์œผ๋กœ ๋นˆ์„ ์ฐพ์Œ ์†์„ฑ๊ฐ’, ์„ธํ„ฐ, ์ƒ์„ฑ์ž ์ ์šฉ๊ฐ€๋Šฅ Spring ์–ด๋…ธํ…Œ์ด์…˜
@Qualifier ๋นˆ์˜ ์ด๋ฆ„์œผ๋กœ ์˜์กด์„ฑ ์ฃผ์ž…
@Autowired ์™€ ๊ฐ™์ด ์‚ฌ์šฉ
๋ชจํ˜ธํ•œ bean์˜ ๊ฐ•์ œ ์—ฐ๊ฒฐ
๋™์ผํ•œ ๊ฐ์ฒด๊ฐ€ 2๊ฐœ ์ด์ƒ์ด๋ฉด ์˜ˆ์™ธ๋ฐœ์ƒ (์ž๋™ ์ฃผ์ž… ๋Œ€์ƒ ๊ฐ์ฒด ํŒ๋‹จ ๋ถˆ๊ฐ€)
Spring ์–ด๋…ธํ…Œ์ด์…˜
@Resource name  ์†์„ฑ์„ ์ด์šฉํ•˜์—ฌ ๋นˆ์˜ ์ด๋ฆ„์„ ์ง์ ‘ ์ง€์ • ์†์„ฑ๊ฐ’, ์„ธํ„ฐ ์ ์šฉ๊ฐ€๋Šฅ javaSE์˜ ์–ด๋…ธํ…Œ์ด์…˜ (JDK9 ์—๋Š” ํฌํ•จ ์•ˆ๋จ)
@Inject @Autowired ๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ๊ณผ ๊ฐ™๋‹ค.   javaSE์˜ ์–ด๋…ธํ…Œ์ด์…˜ 

 

 

 

 

 

๐Ÿ’ป Bean ์ƒ์„ฑ  vs ์˜์กด์„ฑ ์ฃผ์ž… 

 

 

๐Ÿ’ป xml ํŒŒ์ผ -> java ํŒŒ์ผ๋กœ ๋ณ€๊ฒฝ

@Configuration : ์Šคํ”„๋ง ์ปจํ…Œ์ด๋„ˆ๋ฅผ ๋Œ€์‹  ์ƒ์„ฑํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜

@Bean  : ๋นˆ์œผ๋กœ ๋“ฑ๋กํ•˜๋Š” ์–ด๋…ธํ…Œ์ด์…˜