스프링(4)

서울IT코드정리 /kyChoi·2021년 11월 8일
0

스프링

목록 보기
7/17
package com.javalec.ex;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

public class MainClass {

	public static void main(String[] args) {
		
		String configLocation1 = "classpath:applicationCTX.xml";
		String configLocation2 = "classpath:applicationCTX1.xml";
		AbstractApplicationContext ctx = new GenericXmlApplicationContext(configLocation1,configLocation2);
		Student student1 =ctx.getBean("student1",Student.class);
			System.out.println(student1.getName());//홍길동
			System.out.println(student1.getHobbys());//수영요리
			
		StudentInfo studentInfo = ctx.getBean("studentInfo1",StudentInfo.class);
		Student student2 = studentInfo.getStudent();
		System.out.println(student2.getName());
		System.out.println(student2.getHobbys());
		
		if(student1.equals(student2)) {
			System.out.println("student1 == student2");
		}
		Student student3 = ctx.getBean("student3",Student.class);
		System.out.println(student3.getName());
		
		if(student1.equals(student3)) {
			System.out.println("student1 == student3");
		}else {
			System.out.println("student1 != student3");
		}
		Family family = ctx.getBean("family",Family.class);
		System.out.println(family.getPapaName());
		System.out.println(family.getMamiName());
		
	}

}

configLocation1,2 만들었습니다.
Generic에 넣어서 읽어오게 합니다. 모든 객체와 값들은 ctx에 담깁니다.
getBean 으로 student1 객체를 타겟팅 했습니다. student1의 객체는 Student 클래스를 가리킵니다. student1.getName(), student1.getHobbys() 해서 해당 메소드를 Student 클래스에 가서 호출합니다. studentInfo에 있는 getStudent를 호출합니다. return 값은 student 인데, 멤버변수로 Student student 가 선언되어 있습니다.
해당 멤버변수는 .xml 에서 student 가 student1 을 참조했기에 student1 이 가진 클래스 입니다. 이 부분 잘 이해해야 합니다.
참고로 Student studentInfo 에서 콘솔 로그 로 studentInfo 찍으면 studentInfo 주소가 나오고 studentInfo.getStudent() 하고 student2 를 찍으면 Student 의 주소가 나옵니다.
student 1과 2가 같음을 이해해야 합니다.

ctx에 있는 getBean 으로 studentInfo1 객체를 타겟팅 했습니다.

<?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">

	<bean id ="student1" class="com.javalec.ex.Student">
		<constructor-arg value="홍길동"/>
		<constructor-arg value="10"/>
		<constructor-arg>
		 <list>
		 	<value>수영</value>
		 	<value>요리</value>
		 </list>
		</constructor-arg>
		<property name="height" value="187"/>
		<property name="weight" value="84"/>
	</bean>
	<bean id="studentInfo1" class="com.javalec.ex.StudentInfo">
		<property name="student">
			<ref bean ="student1"/>
		</property>
	</bean>
</beans>

StudentInfo 의 객체 studentInfo1 은 student1 을 참조합니다. 즉 Student 클래스를 참조합니다

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

	 <bean id ="student3" class="com.javalec.ex.Student">
	 	<constructor-arg value="홍길자"/>
	 	<constructor-arg value="8"/>
	 	<constructor-arg>
	 	 <list>
	 	
	 	 	<value>줄넘기</value>
	 	 	<value>공기놀이</value>
	 	
	 	 </list>
	 	</constructor-arg>
	 	
	 	<property name="height">
	 		<value>126</value>
	 	</property>
	 	<property name="weight" value="21"/>
	 </bean>
	 <bean id="family" class="com.javalec.ex.Family" c:papaName="홍아빠" c:mamiName="홍엄마" p:sisterName="홍누나">
	 	<property name="brotherName" value="홍오빠"/>
	 </bean>
</beans>

Student는 student3 을 객체로 가집니다. Family 는 family 를 객체로 가집니다. c: 는 생성자, p: 프로퍼티, getter /setter 를 의미합니다 이렇게 사용하기 위해선
xmlns:c="http://www.springframework.org/schema/c"
xmlns:p="http://www.springframework.org/schema/p"
이 로직을 위에 붙여 넣기 해줘야 합니다 , 하지만 줄이 길어져서 잘 사용안함

package com.javalec.ex;

public class Family {
	String papaName;
	String mamiName;
	String sisterName;
	String brotherName;
	
	public String getPapaName() {
		return papaName;
	}

	public void setPapaName(String papaName) {
		this.papaName = papaName;
	}

	public String getMamiName() {
		return mamiName;
	}

	public void setMamiName(String mamiName) {
		this.mamiName = mamiName;
	}

	public String getSisterName() {
		return sisterName;
	}

	public void setSisterName(String sisterName) {
		this.sisterName = sisterName;
	}

	public String getBrotherName() {
		return brotherName;
	}

	public void setBrotherName(String brotherName) {
		this.brotherName = brotherName;
	}

	public Family(String papaName, String mamiName) {
		this.papaName = papaName;
		this.mamiName = mamiName;
	}


}
package com.javalec.ex;

public class StudentInfo {
	private Student student;
	
	public StudentInfo() {
		
	}
	public void setStudent(Student student) {
		this.student = student;
	}
	public Student getStudent() {
		return student;
	}
}

package com.javalec.ex;

import java.util.ArrayList;

public class Student{

private String name;
private int age;
private ArrayList<String> hobbys;
private double height;
private double weight;

public Student(String name,int age, ArrayList<String> hobbys) {
	this.name = name;
	this.age = age;
	this.hobbys=hobbys;
}

public String getName() {
	return name;
}

public int getAge() {
	return age;
}

public ArrayList<String> getHobbys() {
	return hobbys;
}
public void setHobbys(ArrayList<String> hobbys) {
	this.hobbys = hobbys;
}

public double getHeight() {
	return height;
}
public void setHeight(double height) {
	this.height = height;
}
public double getWeight() {
	return weight;
}
public void setWeight(double weight) {
	this.weight = weight;
}

}

profile
건물주가 되는 그날까지

0개의 댓글