본문 바로가기
Kosta DevOps 과정 280기/Java

어제 복습

by 롯슈83 2024. 8. 1.

어제 복습(Setter 에 의하여 초기화한 DI 를 이용한 객체 생성)

package exam01;

public class DeptVO {
	private int dno;
	private String dname;
	private String dloc;
	
	public void setDno(int dno) {
		this.dno = dno;
	}
	public void setDname(String dname) {
		this.dname = dname;
	}
	public void setDloc(String dloc) {
		this.dloc = dloc;
	}
	@Override
	public String toString() {
		return "DeptVO [dno=" + dno + ", dname=" + dname + ", dloc=" + dloc + "]";
	}
}
package exam01;

public class DeptDAO {
	private DeptVO deptVO;

	public void setDeptVO(DeptVO deptVO) {
		this.deptVO = deptVO;
	}

	public void insert() {
		System.out.println("부서를 등록하였습니다.");
		System.out.println(deptVO);
	}
	
	@Override
	public String toString() {
		return "DeptDAO [deptVO=" + deptVO + "]";
	}
}
<?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="vo" class="exam01.DeptVO">
		<property name="dno" value="1" />
		<property name="dname" value="부서 1팀" />
		<property name="dloc" value="종각" />
	</bean>
	<bean id="dao" class="exam01.DeptDAO">
		<property name="deptVO" ref="vo" />
	</bean>
</beans>
package exam01;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("exam01/beans.xml");
		DeptDAO dao = (DeptDAO)context.getBean("dao");
		dao.insert();
	}
}

 

어제 복습2(생성자에 의하여 초기화한 DI 를 이용한 객체 생성)

package exam02;

public class GoodsVO {
	private int no;
	private String item;
	private int qty;
	private int price;
	public GoodsVO(int no, String item, int qty, int price) {
		super();
		this.no = no;
		this.item = item;
		this.qty = qty;
		this.price = price;
	}
	
	@Override
	public String toString() {
		return "GoodsVO [no=" + no + ", item=" + item + ", qty=" + qty + ", price=" + price + "]";
	}
}
package exam02;

public class GoodsDAO {
	private GoodsVO goodsVO;

	public void setGoodsVO(GoodsVO goodsVO) {
		this.goodsVO = goodsVO;
	}
	
	public void insert() {
		System.out.println("상품을 등록하였습니다.");
		System.out.println(goodsVO);
	}
}
<?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="goodsVO" class="exam02.GoodsVO">
		<constructor-arg value="1"/>
		<constructor-arg value="지우개"/>
		<constructor-arg value="20"/>
		<constructor-arg value="500"/>
	</bean>
	<bean id="goodsDAO" class="exam02.GoodsDAO">
		<property name="goodsVO" ref="goodsVO" />
	</bean>
</beans>
package exam02;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("exam02/beans.xml");
		GoodsDAO dao=(GoodsDAO)context.getBean("goodsDAO");
		dao.insert();
	}
}

 

 

'Kosta DevOps 과정 280기 > Java' 카테고리의 다른 글

어노테이션 기반의 DI  (0) 2024.08.01
DI와 XML 이용-2  (0) 2024.08.01
DI 의 필요성  (0) 2024.07.31
스프링 베이직 시작 : 환경설정  (0) 2024.07.31
jquery와 Ajax  (0) 2024.07.22