개발하는 뚝딱이

스프링 7. 다양한 의존 객체 주입 본문

스프링 7. 다양한 의존 객체 주입

개발자뚝딱이 2019. 8. 29. 17:48

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

<bean id="studentDao" class="ems.member.dao.StudentDao" ></bean>

<bean id="registerService" class="ems.member.service.StudentRegisterService">
  <constructor-arg ref="studentDao" ></constructor-arg>
</bean>

<bean id="modifyService" class="ems.member.service.StudentModifyService">
  <constructor-arg ref="studentDao" ></constructor-arg>
</bean>

<bean id="deleteService" class="ems.member.service.StudentDeleteService">
  <constructor-arg ref="studentDao" ></constructor-arg>
</bean>

<bean id="selectService" class="ems.member.service.StudentSelectService">
  <constructor-arg ref="studentDao" ></constructor-arg>
</bean>

<bean id="allSelectService" class="ems.member.service.StudentAllSelectService">
  <constructor-arg ref="studentDao" ></constructor-arg>
</bean>

 

 

public StudentRegisterService(StudentDao studentDao) {
  this.studentDao = studentDao;
}
public StudentModifyService(StudentDao studentDao) {
  this.studentDao = studentDao;
}
public StudentDeleteService(StudentDao studentDao) {
  this.studentDao = studentDao;
}
public StudentSelectService(StudentDao studentDao) {
  this.studentDao = studentDao;
}
public StudentAllSelectService(StudentDao studentDao) {
  this.studentDao = studentDao;
}

 

 

 

setter를 이용한 의존 객체 주입

  • property name에, setter의 set을 떼고 소문자로 표현한다
  • setJdbcUrl  property name = "jdbcUrl"
public void setJdbcUrl(String jdbcUrl) {
  this.jdbcUrl = jdbcUrl;
}

public void setUserId(String userId) {
  this.userId = userId;
}

public void setUserPw(String userPw) {
  this.userPw = userPw;
}

 

<bean id="dataBaseConnectionInfoDev" class="ems.member.DataBaseConnectionInfo">
  <property name="jdbcUrl" value="jdbc:oracle:thin:@localhost:1521:xe" />
  <property name="userId" value="scott" />
  <property name="userPw" value="tiger" />
</bean>

 

 

List타입 의존 객체 주입

  • property name 설정은 같다
  • value 대신에 list를 설정해서 value를 넣을 수 있다
public void setDevelopers(List<String> developers) {
  this.developers = developers;
}

 

<property name="developers">
  <list>
    <value>Cheney.</value>
    <value>Eloy.</value>
    <value>Jasper.</value>
    <value>Dillon.</value>
    <value>Kian.</value>
  </list>
</property>

 

 

 

 

Map타입 객체 주입

  • map이라는 태그를 만들고, entry 태그로 key, value를 묶는다
public void setAdministrators(Map<String, String> administrators) {
  this.administrators = administrators;
}

 

<property name="administrators">
  <map>
    <entry>
      <key>
        <value>Cheney</value>
      </key>
        <value>cheney@springPjt.org</value>
    </entry>
    <entry>
      <key>
        <value>Jasper</value>
      </key>
        <value>jasper@springPjt.org</value>
    </entry>
  </map>
</property>