반응형

Spring Autowiring 'byName'


(원문 위치 : http://www.tutorialspoint.com/spring/spring_autowiring_byname.htm )

속성(property) 이름에 의한 autowiring을 지정한다. Spring container는 XML 설정파일에서 auto-wire 속성(attribute)이 'byName'으로 설정된 bean을 찾는다. 그리고 동일 이름으로 정의되어진 bean을 찾고 이것의 속성(property)를 묶는다. 만약 일치하는 것을 찾으면 검색된 bean이 삽입될 것이다. 못찾으면 exception이 발생한다.

예를 들어, 설정파일에 autowire byName으로 설정된 bean이 있다면 그리고 이것은 spellChecker 속성(property - 즉 setSpellChecker(...) 함수를 갖는다.)을 포함한다면, Spring은 spellChecker로 이름지어진 bean 정의를 찾는다. 그리고 속성에 이것을 사용한다. 여전히 <property> tag를 사용하여 나머지 속성을 wire할 수 있다. 아래 예제는 이 개념을 나타낸다.

[003] HelloWorld Example 강좌를 참조하여 프로젝트를 생성한다.

TextEditor.java:

package com.tutorialspoint;

public class TextEditor {
   private SpellChecker spellChecker;
   private String name;

   public void setSpellChecker( SpellChecker spellChecker ){
      this.spellChecker = spellChecker;
   }
   public SpellChecker getSpellChecker() {
      return spellChecker;
   }

   public void setName(String name) {
      this.name = name;
   }
   public String getName() {
      return name;
   }

   public void spellCheck() {
      spellChecker.checkSpelling();
   }
}

SpellChecker.java:

package com.tutorialspoint;

public class SpellChecker {
   public SpellChecker() {
      System.out.println("Inside SpellChecker constructor." );
   }

   public void checkSpelling() {
      System.out.println("Inside checkSpelling." );
   }
   
}

MainApp.java:

package com.tutorialspoint;

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

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = 
             new ClassPathXmlApplicationContext("Beans.xml");

      TextEditor te = (TextEditor) context.getBean("textEditor");

      te.spellCheck();
   }
}

Beans.xml일반적인 설정의 Beans.xml

<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor">
      <property name="spellChecker" ref="spellChecker" />
      <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

설된된  만약 'byName' 모드의 autowiring을 사용한다면, 아래와 같다.

<?xml version="1.0" encoding="UTF-8"?>

<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-3.0.xsd">

   <!-- Definition for textEditor bean -->
   <bean id="textEditor" class="com.tutorialspoint.TextEditor" 
      autowire="byName">
      <property name="name" value="Generic Text Editor" />
   </bean>

   <!-- Definition for spellChecker bean -->
   <bean id="spellChecker" class="com.tutorialspoint.SpellChecker">
   </bean>

</beans>

실행 결과는 아래와 같다.

Inside SpellChecker constructor.
Inside checkSpelling.


반응형

+ Recent posts