반응형
01. CollectionBean클래스
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 {
//1. 자료구조에 값을 저장하는 Field
private Set<String> nameSet;
private List<String> nameList;
//2. 자료구조에 객체를 저장하는 Field
//Collection 객체의 제네릭을 인터페이스로 설정하면 Collection 객체의 요소에는 인터페이스를
//상속받은 모든 자식 객체 저장 가능
private Set<Controller> controllerSet;
private List<Controller> controllerList;
private Map<String, Controller> controllerMap;
//Map 인터페이스를 상속받은 Map객체이지만, Entry(Key & Value)가 모두 String으로만 되어있기 때문에
//제네릭을 설정하지 않음
//즉, Properties객체는 Map<String, String>객체와 동일
private Properties controllerProperties;
public CollectionBean() {
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> 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;
}
public Properties getControllerProperties() {
return controllerProperties;
}
public void setControllerProperties(Properties controllerProperties) {
this.controllerProperties = controllerProperties;
}
}
02. 필드의 자료구조(Collection)에 저장할 객체들
(부모) Controller 인터페이스
package xyz.itwill05.di;
public interface Controller {
void handleRequest();
}
(자식) LoginController클래스
package xyz.itwill05.di;
public class LoginController implements Controller{
@Override
public void handleRequest() {
// TODO Auto-generated method stub
}
}
(자식) LogoutController클래스
package xyz.itwill05.di;
public class LogoutController implements Controller{
@Override
public void handleRequest() {
// TODO Auto-generated method stub
}
}
(자식) ListController클래스
package xyz.itwill05.di;
public class ListController implements Controller{
@Override
public void handleRequest() {
// TODO Auto-generated method stub
}
}
03. 💖Spring Bean Configuration File
[05-2_collection.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.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 : Collection 객체에 요소값을 추가하는 엘리먼트 -->
<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="controllerSet">
<set>
<!-- Collection 객체의 요소로 Spring Bean으로 등록된 클래스의 객체를 저장하기 위한 엘리먼트 -->
<!-- bean 속성 : 요소로 추가될 Spring Bean의 식별자를 속성값으로 설정 - 자동 완성 기능 사용 -->
<ref bean="loginController"/>
<ref bean="logoutController"/>
<ref bean="listController"/>
</set>
</property>
<property name="controllerList">
<list>
<ref bean="loginController"/>
<ref bean="logoutController"/>
<ref bean="listController"/>
</list>
</property>
<property name="controllerMap">
<!-- map : map 객체를 생성하여 필드에 저장하기 위한 엘리먼트 -->
<map>
<!-- entry : Map 객체에 엔트리(Entry - Key & Value)를 추가하기 위한 엘리먼트 : 메소드put과 동일한 역할 -->
<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>
04. 요청처리
CollectionBeanApp
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);
//Collection 객체에 저장된 필드값을 반환받아 출력
// => Collection 객체의 toString() 메소드 자동
//호출 - Collection 객체에 저장된 모든 요소값이 문자열로 바뀌어 저장
System.out.println("nameSet = "+bean.getNameSet());
System.out.println("nameList = "+bean.getNameList());
System.out.println("controllerset = "+bean.getControllerSet());
System.out.println("controllerList = "+bean.getControllerList());
System.out.println("controllerMap = "+bean.getControllerMap());
//Properties 객체 안에 저장된 값은 "객체"가 아닌 "문자열"이 저장되어 출력된 것임
System.out.println("controllerProperties = "+bean.getControllerProperties());
System.out.println("======================================================");
((ClassPathXmlApplicationContext)context).close();
}
}
반응형
'framework > spring IOC(제어의 역행)' 카테고리의 다른 글
[springIOC] 6. DI | 자동주입 | 자동으로 Inject 하는 법 (0) | 2024.07.28 |
---|---|
[springIOC] 5. DI | 수동주입 | Constructor Injection & Setter Injection (0) | 2024.07.28 |
[springIOC] 3. 스프링 Bean 클래스 생성법 | DL | 룩업기능 (feat. ApplicationContext 객체) (0) | 2024.07.27 |
[springIOC] 2. 결합도가 매우 낮은 프로그램 - 스프링 컨테이너의 역할 및 장점, 스프링빈 (0) | 2024.07.26 |
[springIOC] 1. 결합도가 높은 프로그램 vs 결합도가 낮은 프로그램(팩토리클래스의 역할) (0) | 2024.07.26 |