본문 바로가기
웹 프로그래밍(풀스택-->java)/웹프로그래밍(백엔드-->java)

56. Setter 메서드를 통한 주입

by 백엔드개발자0107 2021. 12. 20.

우리가 지난시간에 생성자를 통해서 객체를 생성할때 값을 주입하는 것에 대해서 알아보았다.

 

이번에는 Setter메서드를 통해서 값을 주입하는 방법에 대해서 알아보자.

 

 

MainClass.java

 

package kr.co.softcampus.main;

import org.springframework.context.support.ClassPathXmlApplicationContext;

import kr.co.softcampus.beans.TestBean;

public class MainClass {

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		TestBean obj1 = new TestBean();
		
		obj1.setData1(100);
		
		System.out.printf("obj.data1 : %d\n",obj1.getData1());
		
		ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("kr/co/softcampus/config/beans.xml");
		
		TestBean t1 = ctx.getBean("t1",TestBean.class);
		System.out.printf("t1.data1 : %d\n", t1.getData1());
		System.out.printf("t1.data2 : %d\n", t1.getData2());
		System.out.printf("t1.data3 : %d\n", t1.isData3());
		System.out.printf("t1.data4 : %d\n", t1.getData4());
		System.out.printf("t1.data5: %d\n", t1.getData5());
		System.out.printf("t1.data6: %d\n", t1.getData6());
		
		
		ctx.close();
	}

}

 

beans.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.xsd"
	                	   default-init-method="default-init" default-destroy-method="default-destroy">

	<bean id="t1" class="kr.co.softcampus.beans.TestBean">
	<property name="data1" value="100"></property>
	<!-- setData1() 메소드와 같다.  -->
	<property name="data2" value="11.11"></property>
	<!-- setData1() 메소드와 같다.  -->
	<property name="data3" value="true"></property>
	<!-- setData1() 메소드와 같다.  -->
	<property name="data4" value="안녕하세요"></property>
	<!-- setData1() 메소드와 같다.  -->
	<property name="data5" value="안녕하세요">
	<!-- setData1() 메소드와 같다.  -->
	<bean class="kr.co.softcampus.beans.DataBean"/>
	</property>
	
	<property name="data6" ref="data_bean"></property>
	<!-- //이미 정의된 bean객체를 여기다가 주입할때는 ref를 사용하면 된다.
	 --></bean>


<bean id="data_bean" class="kr.co.softcampus.beans.DataBean"/>
</beans>

 

TestBean.java

 

package kr.co.softcampus.beans;

public class TestBean {

	private int data1;
	private double data2;
	private boolean data3;
	private String data4;
	private DataBean data5;
	private DataBean data6;
	
	
	public int getData1() {
		return data1;
	}

	public void setData1(int data1) {
		this.data1 = data1;
	}

	public double getData2() {
		return data2;
	}

	public void setData2(double data2) {
		this.data2 = data2;
	}

	public boolean isData3() {
		return data3;
	}

	public void setData3(boolean data3) {
		this.data3 = data3;
	}

	public String getData4() {
		return data4;
	}

	public void setData4(String data4) {
		this.data4 = data4;
	}

	public DataBean getData5() {
		return data5;
	}

	public void setData5(DataBean data5) {
		this.data5 = data5;
	}

	public DataBean getData6() {
		return data6;
	}

	public void setData6(DataBean data6) {
		this.data6 = data6;
	}
	
	
	
	
}

 

DataBean.java

 

package kr.co.softcampus.beans;

public class DataBean {

}