[Spring] xml 사용하기2

    Student.java

    package com.studentEx;
    
    public class Student {
    
    	private String name;
    	private String age;
    	private String gradeNum;
    	private String classNum;
    
    	public Student(String name, String age, String gradeNum, String classNum) {
    		super();
    		this.name = name;
    		this.age = age;
    		this.gradeNum = gradeNum;
    		this.classNum = classNum;
    	}
    
    	public String getName() {
    		return name;
    	}
    
    	public String getAge() {
    		return age;
    	}
    
    	public String getGradeNum() {
    		return gradeNum;
    	}
    
    	public String getClassNum() {
    		return classNum;
    	}
    
    }
    

     

     

    StudentInfo.java

    package com.studentEx;
    
    public class StudentInfo {
    
    	private Student student;
    
    	public StudentInfo(Student student) {
    		this.student = student;
    	}
    	
    	public void getStudentInfo() {
    		if(student != null) {
    			System.out.println("이름 : "+student.getName());
    			System.out.println("나이 : "+student.getAge());
    			System.out.println("학년 : "+student.getGradeNum());
    			System.out.println("반 : "+student.getClassNum());
    			System.out.println("===============================");
    		}
    	}
    	
    	public void setStudent(Student student) {
    		this.student = student;
    	}
    }
    

     

     

    MainClass

    package com.studentEx;
    
    import org.springframework.context.support.AbstractApplicationContext;
    import org.springframework.context.support.GenericXmlApplicationContext;
    
    public class MainClass {
    
    	public static void main(String[] args) {
    		String configlocationin = "classpath:StudentCTX.xml";
    		AbstractApplicationContext ctx = new GenericXmlApplicationContext(configlocationin);
    
    		StudentInfo studentInfo = ctx.getBean("studentInfo", StudentInfo.class);
    		studentInfo.getStudentInfo();
    
    		Student student2 = ctx.getBean("student2", Student.class);
    		studentInfo.setStudent(student2);
    		studentInfo.getStudentInfo();
    
    		ctx.close();
    	}
    }
    

     

     

    StudentCTX.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-2.0.xsd">
    
    	<bean id="student1" class="com.studentEx.Student">
    		<constructor-arg>
    			<value>홍길동</value>
    		</constructor-arg>
    		<constructor-arg>
    			<value>10살</value>
    		</constructor-arg>
    		<constructor-arg>
    			<value>3학년</value>
    		</constructor-arg>
    		<constructor-arg>
    			<value>20번</value>
    		</constructor-arg>
    	</bean>
    
    	<bean id="student2" class="com.studentEx.Student">
    		<constructor-arg>
    			<value>슈퍼맨</value>
    		</constructor-arg>
    		<constructor-arg>
    			<value>9살</value>
    		</constructor-arg>
    		<constructor-arg>
    			<value>2학년</value>
    		</constructor-arg>
    		<constructor-arg>
    			<value>15번</value>
    		</constructor-arg>
    	</bean>
    
    	<bean id="studentInfo" class="com.studentEx.StudentInfo">
    		<constructor-arg>
    			<ref bean="student1" />
    		</constructor-arg>
    
    	</bean>
    
    </beans>
    

    Student의 생성자 값을 여러개를 넣기 위해서는 <constructor-arg>를 사용한다.

    어노테이션 효과를 지닌 이 테그는 해당 생성자를 자동으로 매치시켜서 값을 넣어주는 역할을 해준다.

    순서대로 값이 들어가므로 그 점만 주의해서 하면 된다.

    반응형

    'Web > Spring' 카테고리의 다른 글

    [Spring] Xml Java vs XML  (0) 2020.10.21
    [Spring] xml 활용하기  (0) 2020.10.21
    [Spring] xml사용하기1  (0) 2020.10.20
    [Spring] xml 사용하기3  (0) 2020.10.20
    [Spring] 설치하기  (0) 2020.10.20

    댓글

    Designed by JB FACTORY