ArrayList 생성과 사용
Program.java
package spring.di;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.di.entity.Exam;
import spring.di.entity.NewIecExam;
import spring.di.ui.ExamConsole;
import spring.di.ui.GridExamConsole;
import spring.di.ui.InlineExamConsole;
public class Program {
public static void main(String[] args) {
/* 스프링에게 지시하는 방법으로 코드를 변경
* Exam exam = new NewIecExam();
* Exam exam = new NewIecExam(10,10,10,10);
* ExamConsole console = new GridExamConsole();
*
* console.setExam(exam);
*/
//지시서 읽어들이기
ApplicationContext context = new ClassPathXmlApplicationContext("spring/di/setting.xml");
Exam exam = context.getBean(Exam.class);
System.out.println(exam.toString());
//ExamConsole console = (ExamConsole) context.getBean("console"); //id로 꺼내기
ExamConsole console = context.getBean(ExamConsole.class); //(가르키는 인터페이스 형식에 찹조하는 객체를 찾아준다.) 클래스명으로 가져오기
console.print();
List<Exam> exams = (List<Exam>) context.getBean("exams"); //new ArrayList<>();
exams.add(new NewIecExam(1,1,1,1));
for(Exam e : exams)
System.out.println(e);
}
}
setting.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!-- Exam exam = new NewIecExam(); -->
<bean id="exam" class="spring.di.entity.NewIecExam" p:kor="10" p:eng="10" />
<!--
<bean id="exam" class="spring.di.entity.NewIecExam" >
<constructor-arg name="kor" value="10" />
<constructor-arg name="math" value="10" />
<constructor-arg name="com" value="10" />
<constructor-arg name="eng" value="10" />
</bean>
-->
<!-- ExamConsole console = new GridExamConsole(); -->
<bean id="console" class="spring.di.ui.GridExamConsole" >
<!-- console.setExam(exam); -->
<property name="exam" ref="exam" />
</bean>
<!-- ArrayList를 생성자로 넘기는 방식 -->
<bean id="exams" class="java.util.ArrayList">
<constructor-arg>
<list>
<bean class="spring.di.entity.NewIecExam" p:kor="10" p:eng="10" /> <!-- 직접생성 -->
<ref bean="exam" /> <!-- 위에 참조 -->
</list>
</constructor-arg>
</bean>
</beans>
컬랙션을 개별적으로 만드는 방법 - Namespaces util
별도의 Namespaces 이용
Program.java
package spring.di;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import spring.di.entity.Exam;
import spring.di.entity.NewIecExam;
import spring.di.ui.ExamConsole;
import spring.di.ui.GridExamConsole;
import spring.di.ui.InlineExamConsole;
public class Program {
public static void main(String[] args) {
/* 스프링에게 지시하는 방법으로 코드를 변경
* Exam exam = new NewIecExam();
* Exam exam = new NewIecExam(10,10,10,10);
* ExamConsole console = new GridExamConsole();
*
* console.setExam(exam);
*/
//지시서 읽어들이기
ApplicationContext context = new ClassPathXmlApplicationContext("spring/di/setting.xml");
Exam exam = context.getBean(Exam.class);
System.out.println(exam.toString());
ExamConsole console = (ExamConsole) context.getBean("console"); //id로 꺼내기
//ExamConsole console = context.getBean(ExamConsole.class); //(가르키는 인터페이스 형식에 찹조하는 객체를 찾아준다.) 클래스명으로 가져오기
console.print();
List<Exam> exams = (List<Exam>) context.getBean("exams"); //new ArrayList<>();
//exams.add(new NewIecExam(1,1,1,1));
for(Exam e : exams)
System.out.println(e);
}
}
setting.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"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.3.xsd">
<!-- Exam exam = new NewIecExam(); -->
<bean id="exam" class="spring.di.entity.NewIecExam" p:kor="10" p:eng="10" />
<!--
<bean id="exam" class="spring.di.entity.NewIecExam" >
<constructor-arg name="kor" value="10" />
<constructor-arg name="math" value="10" />
<constructor-arg name="com" value="10" />
<constructor-arg name="eng" value="10" />
</bean>
-->
<!-- ExamConsole console = new GridExamConsole(); -->
<bean id="console" class="spring.di.ui.GridExamConsole" >
<!-- console.setExam(exam); -->
<property name="exam" ref="exam" />
</bean>
<!-- 컬랙션 실제로 객체를 만드는 능력 -->
<util:list id="exams" list-class="java.util.ArrayList">
<bean class="spring.di.entity.NewIecExam" p:kor="1" p:eng="1" /> <!-- 직접생성 -->
<ref bean="exam" /> <!-- 위에 참조 -->
</util:list>
<!-- ArrayList를 생성자로 넘기는 방식 -->
<!-- <bean id="exams" class="java.util.ArrayList">
<constructor-arg>
<list>
<bean class="spring.di.entity.NewIecExam" p:kor="10" p:eng="10" /> 직접생성
<ref bean="exam" /> 위에 참조
</list>
</constructor-arg>
</bean> -->
</beans>