클래스 생성자의 매개변수 개수가 동일하면 어디에 값을 넣게될까?
↓ 샘플 클래스
public class SampleBean5 {
int num;
String str;
public SampleBean5() {
num = 0;
str = null;
}
public SampleBean5(int num) {
this.num = num;
this.str = null;
}
public SampleBean5(String str) {
this.num = 0;
this.str = str;
}
public SampleBean5(int num, String str) {
this.num = num;
this.str = str;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getStr() {
return str;
}
public void setStr(String str) {
this.str = str;
}
}
>>> 생성자 중 각각 int num과 String str을 매개변수로 받는 클래스가 있음
<bean id="sampleBean5_2" class="com.app.beans.SampleBean5">
<constructor-arg value="30"></constructor-arg>
</bean>
>>> 30이라는 값을 넣어봄
@RequestMapping("/sb5")
public String sb5() {
SampleBean5 sb5 = (SampleBean5)applicationContext.getBean("sampleBean5_2");
System.out.println(sb5.getNum());
System.out.println(sb5.getStr());
return "checkBean";
}
>>> 컨트롤러를 통해 출력 결과를 확인하고자함!
↓ 출력결과
>>> String이 우선순위로 잡혀 str 변수에 값이 들어간 것을 볼 수 있음!
매개변수에 따라 다르게 값을 넣어보자!
<!-- type으로 지정 -->
<bean id="sampleBean5_2" class="com.app.beans.SampleBean5">
<constructor-arg value="30" type="int"></constructor-arg>
</bean>
<!-- value 값을 두 개 넣으면 두개의 매개변수로 지정 -->
<bean id="sampleBean5_3" class="com.app.beans.SampleBean5">
<constructor-arg value="10"></constructor-arg>
<constructor-arg value="20"></constructor-arg>
</bean>
↓ 컨트롤러
@RequestMapping("/sb5")
public String sb5() {
SampleBean5 sb5 = (SampleBean5)applicationContext.getBean("sampleBean5_2");
System.out.println(sb5.getNum());
System.out.println(sb5.getStr());
SampleBean5 sb5_2 = (SampleBean5)applicationContext.getBean("sampleBean5_3");
System.out.println(sb5_2.getNum());
System.out.println(sb5_2.getStr());
return "checkBean";
}
↓ 출력결과
↓ GetSetBean.java 클래스
public class GetSetBean {
int var1;
String var2;
String var3;
// getter setter는 반드시 기본형 이름이어야 함!
public int getVar1() {
return var1;
}
public void setVar1(int var1) {
this.var1 = var1;
}
public String getVar2() {
return var2;
}
public void setVar2(String var2) {
this.var2 = var2;
}
public String getVar3() {
return var3;
}
public void setVar3(String var3) {
this.var3 = var3;
}
@Override
public String toString() {
return "GetSetBean [var1=" + var1 + ", var2=" + var2 + ", var3=" + var3 + "]";
}
}
↓ root-context.xml
<!-- Setter를 통한 주입 -->
<bean id="gsb1" class="com.app.beans.GetSetBean">
<property name="var1" value="500"></property>
<property name="var2" value="600"></property>
<property name="var3" value="문자"></property>
</bean>
>>> property 태그 사용
↓ 컨트롤러
@RequestMapping("/getset")
public String getset() {
GetSetBean gsb = (GetSetBean)applicationContext.getBean("gsb1");
System.out.println(gsb.toString());
return "checkBean";
}
↓ 출력결과
↓ GetSetBean2.java 클래스
public class GetSetBean2 {
//기본타입3개 + 객체타입 2개
int var1;
String var2;
String var3;
SampleBean1 sb1;
SampleBean1 sb2;
public int getVar1() {
return var1;
}
public void setVar1(int var1) {
this.var1 = var1;
}
public String getVar2() {
return var2;
}
public void setVar2(String var2) {
this.var2 = var2;
}
public String getVar3() {
return var3;
}
public void setVar3(String var3) {
this.var3 = var3;
}
public SampleBean1 getSb1() {
return sb1;
}
public void setSb1(SampleBean1 sb1) {
this.sb1 = sb1;
}
public SampleBean1 getSb2() {
return sb2;
}
public void setSb2(SampleBean1 sb2) {
this.sb2 = sb2;
}
@Override
public String toString() {
return "GetSetBean2 [var1=" + var1 + ", var2=" + var2 + ", var3=" + var3 + ", sb1=" + sb1 + ", sb2=" + sb2
+ "]";
}
}
↓ root-context.xml
<bean id="sb1" class="com.app.beans.SampleBean1"/>
<bean id="sb2" class="com.app.beans.SampleBean1"/>
<bean id="gsb2" class="com.app.beans.GetSetBean2">
<property name="var1" value="500"></property>
<property name="var2" value="600"></property>
<property name="var3" value="문자"></property>
<property name="sb1" ref="sb1"></property>
<property name="sb2" ref="sb2"></property>
</bean>
<!-- ref=""로 객체를 부를 수 있지만, property 태그 안에 bean 태그를 넣어 코드를 간소화할 수 있음! -->
<!--
<property name="sb1">
<bean class="com.app.beans.SampleBean1"/>
</property>
-->
↓ 컨트롤러
@RequestMapping("/getset")
public String getset() {
GetSetBean gsb = (GetSetBean)applicationContext.getBean("gsb1");
System.out.println(gsb.toString());
GetSetBean2 gsb2 = (GetSetBean2)applicationContext.getBean("gsb2");
System.out.println(gsb2.toString());
return "checkBean";
}
↓ 출력결과