📃CollectionBean
※ xyz.itwill05.di 패키지에 CollectionBean.java 클래스 생성
package xyz.itwill05.di; // import java.util.List; import java.util.Map; import java.util.Properties; import java.util.Set; // public class CollectionBean { private Set<String> nameSet; private List<String> nameList; //Collection 객체의 자료형 제네릭을 인터페이스로 설정하면 Collection 객체의 요소에는 인터페이스를 상속받은 모든 자식클래스의 객체 저장 가능 private Set<Controller> controllerSet; private List<Controller> controllerList; private Map<String, Controller> controllerMap; private Properties controllerProperties; public Properties getControllerProperties() { return controllerProperties; } public void setControllerProperties(Properties controllerProperties) { this.controllerProperties = controllerProperties; } // public CollectionBean() { // TODO Auto-generated constructor stub System.out.println("### CollectionBean 클래스의 기본 생성자 호출 ###"); } public Set<String> getNameSet() { return nameSet; } public void setNameSet(Set<String> nameSet) { this.nameSet = nameSet; } public List<String> getNameList() { return nameList; } public void setNameList(List<String> nameList) { this.nameList = nameList; } public Set<Controller> getControllersSet() { return controllerSet; } public void setControllersSet(Set<Controller> controllersSet) { this.controllerSet = controllersSet; } public List<Controller> getControllersList() { return controllerList; } public void setControllersList(List<Controller> controllersList) { this.controllerList = controllersList; } public Set<Controller> getControllerSet() { return controllerSet; } public void setControllerSet(Set<Controller> controllerSet) { this.controllerSet = controllerSet; } public List<Controller> getControllerList() { return controllerList; } public void setControllerList(List<Controller> controllerList) { this.controllerList = controllerList; } public Map<String, Controller> getControllerMap() { return controllerMap; } public void setControllerMap(Map<String, Controller> controllerMap) { this.controllerMap = controllerMap; } }
📃CollectionBeanApp.java
※ xyz.itwill05.di 패키지에 CollectionBeanApp.java 클래스 생성
package xyz.itwill05.di; // import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; // public class CollectionBeanApp { public static void main(String[] args) { System.out.println("================== Spring Container 초기화 전 =================="); ApplicationContext context=new ClassPathXmlApplicationContext("05-2_collection.xml"); System.out.println("================== Spring Container 초기화 후 =================="); CollectionBean bean=context.getBean("collectionBean", CollectionBean.class); // //CollectionBean 객체에 저장된 필드값을 반환받아 출력 - toString() 메소드 자동 호출 //→ Collection 객체의 toString() 메소드 자동 호출 - Collection 객체에 저장된 모든 요소값이 문자열로 반환 System.out.println("nameSet = "+bean.getNameSet()); System.out.println("nameList = "+bean.getNameList()); System.out.println("controllerSet = "+bean.getControllerSet()); System.out.println("controllersList = "+bean.getControllerList()); System.out.println("controllersMap = "+bean.getControllerMap()); System.out.println("controllersProperties = "+bean.getControllerProperties()); System.out.println("================================================================"); ((ClassPathXmlApplicationContext)context).close(); } }
📃
※ src/main/resources 폴더에 05-1_di.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 https://www.springframework.org/schema/beans/spring-beans.xsd"> <!-- ================================================================================ --> <!-- Controller 인터페이스를 상속받은 자식클래스를 Spring Bean으로 등록 --> <bean class="xyz.itwill05.di.LoginController" id="loginController"/> <bean class="xyz.itwill05.di.LogoutController" id="logoutController"/> <bean class="xyz.itwill05.di.ListController" id="listController"/> <!-- ================================================================================ --> <bean class="xyz.itwill05.di.CollectionBean" id="collectionBean"> <property name="nameSet"> <!-- set : Set 객체를 생성하여 필드에 저장하기 위한 엘리먼트 --> <set> <!-- value : 요소값을 추가하는 엘리먼트 --> <value>홍길동</value> <value>임꺽정</value> <value>전우치</value> <value>홍길동</value> </set> </property> <property name="nameList"> <!-- list : List 객체를 생성하여 필드에 저장하기 위한 엘리먼트 --> <list> <value>홍길동</value> <value>임꺽정</value> <value>전우치</value> <value>홍길동</value> </list> </property> <!-- ================================================================================ --> <property name="controllersSet"> <set> <!-- ref : Collection 객체의 요소로 Spring Bean으로 등록된 클래스의 객체를 저장하기 위한 엘리먼트 --> <!-- bean 속성 : 요소로 추가될 Spring Bean의 식별자를 속성값으로 설정 - 자동 완성 기능 사용 --> <ref bean="loginController"/> <ref bean="logoutController"/> <ref bean="listController"/> </set> </property> <property name="controllersList"> <list> <ref bean="loginController"/> <ref bean="logoutController"/> <ref bean="listController"/> </list> </property> <!-- ================================================================================ --> <property name="controllerMap"> <!-- map : Map 객체를 생성하여 필드에 저장하기 위한 엘리먼트 --> <map> <!-- entry : Map 객체에 엔트리(Entry - Key & Value)를 추가하기 위한 엘리먼트 --> <entry> <!-- key : 엔트리의 맵키(String)를 설정하기 위한 엘리먼트 --> <key> <value>login</value> </key> <!-- ref : 엔트리의 맵값(Controller 객체)를 설정하기 위한 엘리먼트 --> <ref bean="loginController"/> </entry> <entry> <key> <value>logout</value> </key> <ref bean="logoutController"/> </entry> <entry> <key> <value>list</value> </key> <ref bean="listController"/> </entry> </map> </property> <!-- ================================================================================ --> <property name="controllerProperties"> <!-- props : Properties 객체를 생성하여 필드에 저장하기 위한 엘리먼트 --> <!-- → 필드의 자료형 Map<String,String>인 경우 props 엘리먼트로 객체 필드에 Map 객체를 생성하여 저장 가능 --> <props> <!-- prop : Properties 객체에 엔트리를 추가하는 메소드 --> <!-- 엘리먼트의 내용으로 엔트리에 저장될 값(문자열)을 설정 --> <!-- key 속성 : 엔트리를 구분하기 위한 식별자(문자열)를 속성값으로 설정 --> <prop key="login">xyz.itwill05.di.LoginController</prop> <prop key="logout">xyz.itwill05.di.LogoutController</prop> <prop key="list">xyz.itwill05.di.ListController</prop> </props> </property> </bean> </beans>
📃Controller.java
※ xyz.itwill05.di 패키지에 Controller.java 인터페이스 생성
package xyz.itwill05.di; // public interface Controller { void handlRequest(); }
📃LoginController.java
※ xyz.itwill05.di 패키지에 LoginController.java 클래스 생성
package xyz.itwill05.di; // public class LoginController implements Controller { @Override public void handlRequest() { // TODO Auto-generated method stub } }
📃LogoutController.java
※ xyz.itwill05.di 패키지에 LogoutController.java 클래스 생성
package xyz.itwill05.di; // public class LogoutController implements Controller { @Override public void handlRequest() { // TODO Auto-generated method stub } }
📃ListController.java
※ xyz.itwill05.di 패키지에 ListController.java 클래스 생성
package xyz.itwill05.di; // public class ListController implements Controller { @Override public void handlRequest() { // TODO Auto-generated method stub } }