Spring_DI_Constructor

ColinSong·2021년 1월 20일
0

spring-newlecture

목록 보기
4/5
post-thumbnail

목차

2. 생성자

Exam exam = new Exam(10, 10, 10, 10);

2.1.1. Program (Main)

package spring.di;

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

import spring.di.entity.Exam;
import spring.di.entity.NewlecExam;
import spring.di.ui.ExamConsole;
import spring.di.ui.GridExamConsole;
import spring.di.ui.InLineExamConsole;


public class Program {

	public static void main(String[] args) {
		
		ApplicationContext context = 
				new ClassPathXmlApplicationContext("spring/di/setting.xml");
		
		
		Exam exam = context.getBean(Exam.class);
		System.out.println(exam.toString());
		
		
		ExamConsole console = (ExamConsole)context.getBean("console");
		console.print();

	}

}

2.1.2. NewlecExam.java (Exam interface 구현한 클래스)

package spring.di.entity;

public class NewlecExam implements Exam {

	public NewlecExam() {}
	
	public NewlecExam(int kor, int eng, int math, int com) {
		this.kor = kor;
		this.eng = eng;
		this.math = math;
		this.com = com;
	}

	private int kor;
	private int eng;
	private int math;
	private int com;
	
	public void setCom(int com) {
		this.com = com;
	}
	public int getKor() {
		return kor;
	}
	
	public void setKor(int kor) {
		this.kor = kor;
	}
	
	public int getEng() {
		return eng;
	}
	
	public void setEng(int eng) {
		this.eng = eng;
	}
	
	public int getMath() {
		return math;
	}
	
	public void setMath(int math) {
		this.math = math;
	}
	
	public int getCom() {
		return com;
	}

	@Override
	public int total() {
		// TODO Auto-generated method stub
		return kor+eng+math+com;
	}

	@Override
	public float avg() {
		// TODO Auto-generated method stub
		return total() / 4;
	}

	@Override
	public String toString() {
		return "NewlecExam [kor=" + kor + ", eng=" + eng + ", math=" + math + ", com=" + com + "]";
	}
}

2.1.3. setting.xml DI

<?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: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="exam" class="spring.di.entity.NewlecExam" p:kor="10" p:eng="20" p:math="30" p:com="40">
    </bean>

	<bean id="console" class="spring.di.ui.InLineExamConsole">
		<property name="exam" ref="exam"></property>
	</bean>
	
</beans>

방법1.

  • index 값 부여하기.
	<bean id="exam" class="spring.di.entity.NewlecExam">
		<constructor-arg index="0" value="10"/>
		<constructor-arg index="1" value="20"/>
		<constructor-arg index="3" value="30"/>
		<constructor-arg index="2" value="40"/>
	</bean>

방법2.

  • 명시적
  • 만약 메서드가 오버로딩이 있어 타입이 다를 시에, type="float", type="int"를 정해줄 수도 있다.
	<bean id="exam" class="spring.di.entity.NewlecExam">
		<constructor-arg name="kor" type="int" value="10"/>
		<constructor-arg name="eng" value="20"/>
		<constructor-arg name="math" value="30"/>
		<constructor-arg name="com" value="40"/>
	</bean>

방법3.

  • <beans></beans> tag 추가하기

2.1.4. 콘솔

Reference

profile
안녕하세요:)

0개의 댓글

관련 채용 정보