1. List 타입 프로퍼티 전달받기


1) ListCollection 

package com.model.di.exam06;


import java.util.List;


public class ListCollection {

private List<String> userName;

private List<String> getUserName(){

return userName;

}


public void setUserName(List<String> userName) {

this.userName = userName;

}

public void display() {

System.out.println("********** list 목록 ********** ");

for(String name : userName)

System.out.println(name);

}

}



2) 실행 클래스


package com.model.di.exam06;


import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.model.di.exam05.SystemMonitor;


public class Main {

public static void main(String[] args) {

AbstractApplicationContext context = 

new ClassPathXmlApplicationContext("applicationContext.xml");

context.registerShutdownHook();

ListCollection list = (ListCollection)context.getBean("list");

list.display();

context.close();

}

}




3) 설정 파일


<?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:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans.xsd">


<!-- com.model.di.exam06 -->

<!-- List 타입 프로퍼티 설정 -->

<bean id="list" class="com.model.di.exam06.ListCollection">

<property name="userName">

<list value-type="java.lang.String">

<value>홍길동</value>

<value>이순신</value>

<value>강감찬</value>

</list>

</property>

</bean>

</beans>





2. Map 타입 프로퍼티 전달받기


1) MapCollection 

package com.model.di.exam07;


import java.util.Map;


public class MapCollection {

private Map<String, Object> map;

public void setMap(Map<String, Object> map){ 

this.map = map;

}


public void display() {

System.out.println("********* map 목록 *********");

System.out.println(map);

}

}




2) 실행 클래스


package com.model.di.exam07;


import org.springframework.context.support.AbstractApplicationContext;

import org.springframework.context.support.ClassPathXmlApplicationContext;


import com.model.di.exam06.ListCollection;


public class Main {

public static void main(String[] args) {

AbstractApplicationContext context = 

new ClassPathXmlApplicationContext("applicationContext.xml");

context.registerShutdownHook();

MapCollection map = (MapCollection)context.getBean("map");

map.display();

context.close();

}

}





3) 설정 파일


<?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:p="http://www.springframework.org/schema/p"

xsi:schemaLocation="http://www.springframework.org/schema/beans 

http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- com.model.di.exam07 -->

<!-- Map 타입 프로퍼티 설정 -->

<bean id="map" class="com.model.di.exam07.MapCollection">

<property name="map">

<map key-type="java.lang.String" value-type="java.lang.Object">

<entry key="홍길동" value="20"></entry>

<entry key="이순신" value="30"></entry>

<entry key="강감찬" value="40"></entry>

</map>

</property>

</bean>



Posted by netyhobby
,