IoC#

DL(Dependency Lookup)#

  • 사용 컨테이너 : EJB, Spring
  • 개념 : 저장소에 저장되어 있는 bean에 접근하기 위해 개발자들이 컨테이너에 제공하는 API를 이용하여 사용하고자 하는 bean을 lookup하는 방법

public static void main(String[] argsthrows Exception {
  // get the bean factory
  BeanFactory factory = getBeanFactory();

  MessageRenderer mr = (MessageRendererfactory.getBean("renderer");
  mr.render();
}

DI(Dependency Injection)#

  • 사용 컨테이너 : Spring

Setter Injection#

  • 개념 : setter를 통해 의존성이 있는 객체를 삽입하는 방법

public class SetterInjection {
    
    private Dependency dep;
    
    public void setMyDependency(Dependency dep) {
        this.dep = dep;
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
            http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
            http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean id="setterInjection" class="SetterInjection">
        <property name="dep" ref="dep"/>
    </bean>
</beans>

Constructor Injection#

  • 개념 : 생성자를 통해 의존성이 있는 객체를 삽입하는 방법

public class ConstructorInjection {
    
    private Dependency dep;
    
    public ConstructorInjection(Dependency dep) {
        this.dep = dep;
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
            http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
            http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean id="constructorInjection" class="ConstructorInjection">
        <constructor-arg ref="dep"/>
    </bean>
</beans>

Method Injection#

  • 개념 : 일반 메소드를 통해 의존성이 있는 객체를 삽입하는 방법

public class MethodInjection {
    
    private Dependency dep;
    
    public void processDependency(Dependency dep) {
        this.dep = dep;
    }
}

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" 
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns:aop="http://www.springframework.org/schema/aop" 
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="http://www.springframework.org/schema/beans 
      http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
            http://www.springframework.org/schema/aop 
      http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
            http://www.springframework.org/schema/tx 
      http://www.springframework.org/schema/tx/spring-tx-2.0.xsd">

    <bean id="methodInjection" class="MethodInjection">
        <property name="dep" ref="dep"/>
    </bean>
</beans>

Add new attachment

Only authorized users are allowed to upload new attachments.
« This page (revision-3) was last changed on 14-Jun-2007 15:12 by DongGukLee