Ioc Container ApplicationContext 코드

ColinSong·2021년 1월 19일
0

spring-newlecture

목록 보기
2/5
post-thumbnail

목차

1. Ioc Container

  • Inversion of Control
  • 조립에 대한 순서가 역순
  • 생성 + 결합
ApplicationContext context = new ClassPathXmlApplicationContext

1.1 ApplicationContext 종류

  • ClassPathXmlApplicationContext ✔ 가장 보편적
  • FileSystemXmlApplicationContext
  • XmlWebApplicationContext
  • AnnotationConfigApplicationContext

1.1.1. Program (Main)

1.1.2. Exam.java (interface)

package spring.di.entity;

public interface Exam {
	public int total();
	public float avg();
}

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

package spring.di.entity;

public class NewlecExam implements Exam {
	
	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;
	}

}

1.2.4. ExamConsole.java (interface)

  • 결과를 어떤 방식으로 콘솔에 출력할 것인지 나타냄
package spring.di.ui;

import spring.di.entity.Exam;

public interface ExamConsole {
	public void print();
	public void setExam(Exam exam);
}

1.2.5. GridExamConsole.java (ExamConsole interface 구현체 클래스)

package spring.di.ui;

import spring.di.entity.Exam;

public class GridExamConsole implements ExamConsole {
	private Exam exam;
	
	public GridExamConsole() {
		
	}
	
	public GridExamConsole(Exam exam) {
		this.exam = exam;
	}
	
	@Override
	public void print() {
		System.out.println("┌──────────────────┐");
		System.out.println("│"+exam.total()+"│"+exam.avg()+"│");
		System.out.println("└──────────────────┘");
	}

	@Override
	public void setExam(Exam exam) {
		this.exam = exam;
		
	}

}

1.2.6. InLineExamConsole.java (ExamConsole interface 구현체 클래스)

package spring.di.ui;

import spring.di.entity.Exam;

public class InLineExamConsole implements ExamConsole{
	private Exam exam;
	
	public InLineExamConsole() {
	
	}
	
	public InLineExamConsole(Exam exam) {
		this.exam = exam;
	}
	
	@Override
	public void print() {
		System.out.printf("total is InLine %d, %f", exam.total(), exam.avg());
		
	}

	@Override
	public void setExam(Exam exam) {
		this.exam = exam;
		
	}

}

1.2.7 setting.xml

  • new -> other -> Spring Bean Configuration File
<?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">
	
	
</beans>

1.2.7.1. 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"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
	
	<!-- Exam exam = new NewlecExam(); -->
	<bean id="exam" class="spring.di.entity.NewlecExam"/>

	<!-- ExamConsole console = new GridExamConsole();  -->
	<bean id="console" class="spring.di.ui.InLineExamConsole">
		<!-- console.setExam(exam); -->
		<property name="exam" ref="exam"></property>
	</bean>
	
</beans>

<property name="exam" ref="exam"></property>

  • Java 코드 -> setExam(Exam exam)
  • property name="exam" setExam -> set을 빼고 E를 소문자로 바꾸는 것이 약속
  • setter가 꼭 존재해야 함.

1.2. 꺼내오는 방법

  1. xml 파일에서 id="" 로 가져오기 때문에 어떠한 객체인지 알 수 없기에 object 형식으로 꺼내옴 -> 형변환
ExamConsole console = (ExamConsole)context.getBean("console");
  1. 자료형으로 받아오기
ExamConsole console = context.getBean(ExamConsole.class);

Reference

  • 뉴렉처: Spring
  • 영상을 보고 정리한 내용입니다.
  • 🎈2021.01.19

profile
안녕하세요:)

0개의 댓글