[Spring] 의존성 주입(XML - Set객체)

김승현·2022년 1월 11일
0

Set 타입 매핑

  • 중복 값을 허용하지 않는 집합 객체를 사용할때에는 Set을 사용

setContext.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-4.3.xsd">

<bean id="setFactory" class="kr.or.iei.person.model.vo.PersonSetBeanFactory">
	<property name="set">
		<set>
			<value>홍길동</value>
			<value>김말똥</value>
			<value>고길똥</value>
			<value>김말똥</value>
		</set>
	</property>
</bean>

</beans>

SetServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

	AbstractApplicationContext context = new GenericXmlApplicationContext("/personBeanSetContext.xml");
	PersonSetBeanFactory setFactory = (PersonSetBeanFactory) context.getBean("setFactory");

	HashSet<String> set = setFactory.getSet();

	System.out.println(set);

}

PersonSetBeanFactory.java

public class PersonSetBeanFactory {

	private HashSet<String> set;

	public PersonSetBeanFactory() {
		super();
		// TODO Auto-generated constructor stub
	}

	public PersonSetBeanFactory(HashSet<String> set) {
		super();
		this.set = set;
	}

	public HashSet<String> getSet() {
		return set;
	}

	public void setSet(HashSet<String> set) {
		this.set = set;
	}

}
profile
개발자로 매일 한 걸음

0개의 댓글