티스토리 뷰

DI란

OOP 프로그램의 방법론, 외부에서 생성한 객체를 주입하는 것
모듈화를 통해 프로그램의 재사용성을 높이고 유지보수성을 향상시킨다.


방법

  • 생성자를 이용한 의존 객체를 주입

    • 생성자에서 한 번 정한 후 변경이 불가능
    public class Car(){
        private Wheel wheel;
        public Car(){
            wheel = new CarWheel();
        }
    }
  • setter를 이용한 의존 객체 주입

    • 값을 변경 할 수 있다
    public class Car(){
        private Wheel wheel;
        public Car(){ }
        public void setWheel(Wheel wheel){
            this.wheel = wheel;
        }
    }
  • 생성자setter를 이용한 의존 객체 주입

    • 가장 많이 쓰는 방법

    • 값을 변경 할 수 있다

    • 사용자가 원할 때 객체를 주입 할 수 있다

         public class Car(){
             private Wheel wheel;
             public Car(Wheel wheel){
                 this.wheel = wheel;
             }
             public void setWheel(Wheel wheel){
                 this.wheel = wheel;
             }
         }

Spring에서의 적용

  • 실제 사용
public class Car{
    GenericApplicationContext ctx = new GenericApplicationContext("classpath:applicationContext.xml");

    CarRegisterService registerService = ctx.getBean("registerService", CarRegisterService.class);
    CarMaintenanceService maintenanceService = ctx.getBean("maintenanceService", CarMaintenanceService.class);
}

 

  • 생성자에 주입

    • 자바 코드

      public CarRegisterService(CarDao carDao){
          this.carDao = carDao;
      }
      public CarMaintenanceService(CarDao carDao){
          this.carDao = carDao;
      }
      
    • 위의 자바코드를 변환한 applicationContext.xml

        <bean id="carDao" class="...CarDao"></bean>
        // 여러 객체가 carDao 공유
        <bean id="registerService" class="....CarRegisterService">
         <constructor-arg ref="carDao"></constructor>
        </bean>
        <bean id="maintenanceService" class="...CarMaintenanceService">
        <constructor-arg ref="carDao"></constructor>
        </bean>
  • setter에 주입

    • 자바 코드

      public void setJdbcUrl(String jdbcUrl){
          this.jdbcUrl = jdbcUrl;
      }
      public void setUserId(String userId){
          this.userId = userId;
      }
      public void setUserPw(Sting userPw){
          this.userPw = userPw;
      }
    • 위의 자바코드를 변환한 applicationContext.xml

       <bean id="dbConnectionInfo" class="....dbConnectionInfo">
           <property name="jdbcUrl" value="jdbc:...."/>
           <property name="userId" value="값"/>
           <property name="userPw" value="값"/>
       </bean>
  • List 타입 의존객체 주입

    • 자바 코드

      public void setCars(List<String> cars){
      	this.cars = cars;
      }
    • 위의 자바코드를 변환한 applicationContext.xml

        <property name="cars">
            <list>
                <value>truck</value>
                <value>suv</value>
                <value>sedan</value>
            </list>
        </property>
  • Map 타입 의존객체 주입

    • 자바 코드

      public void setAdmin(Map<String, String> admins){
        this.admins = admins;
      }
    • 위의 자바코드를 변환한 applicationContext.xml

      <property name="admins">
          <map>
              <entry>
                  <key>
                      <value>A</value>
                  </key>
                  <value>A@xxx.org</value>
              </entry>
              <entry>
                  <key>
                      <value>B</value>
                  </key>
                  <value>B@xxx.org</value>
              </entry>
          </map>
      </property>

[참고]

인프런 - 자바 스프링 프레임워크

https://www.inflearn.com/course/%EC%8A%A4%ED%94%84%EB%A7%81-%ED%94%84%EB%A0%88%EC%9E%84%EC%9B%8C%ED%81%AC_renew/dashboard

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/10   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30 31
글 보관함