이클립스 Spring DI (XML)

Codren·2021년 6월 16일
0

Section 1. Setter DI

1. Spring Bean Configuration File

  • bean 태그를 통해서 생성할 객체를 지시함
  • class - 어떤 객체를 쓸 것인지 클래스 이름 지정 (패키지명까지 명시)
  • id - 그 객체를 무슨 이름으로 사용할 것인지 지정
  • property - Setter 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">
	
    	// DI 지시 부분
    	<bean id ="exam" class = "spring.di.entity.Exam">
		<property name="kor" value="10" />	// Exam 클래스 kor 멤버변수에 값 초기화
		<property name="eng" value="10" />
		<property name="math" value="10" />
	</bean>
	<bean id ="console" class = "spring.di.ui.GridExamConsole">
		<property name="exam" ref="exam"/>	//  name = exam 은 SetExam() setter 함수를 의미 (규칙: set 제거, 소문자)
	</bean>						//  console 클래스에 SetExam() setter 메서드 존재해야함
</beans>




2. ref type

  • 객체를 dependency 주입
  • name - setExam() setter 메서드를 의미하며 set을 빼고 exam 소문자로 지정 (규칙)
  • ref - 주입할 객체 id 명
<bean id ="exam" class = "spring.di.entity.Exam"></bean>
<bean id ="console" class = "spring.di.ui.GridExamConsole">
	<property name="exam" ref="exam"/>
</bean>	




3. value type

  • 속성의 값을 dependency 주입
  • name - 해당 클래스의 멤버변수에 대한 setter 메서드를 의미
  • value - 초기화 값
<bean id ="exam" class = "spring.di.entity.Exam">
      <property name="kor" value="10" />
      <property name="eng" value="10" />
      <property name="math" value="10" />
</bean>




4. ApplicationContext

  • BeanFactory 인터페이스를 상속 받은 하위 인터페이스
  • 실제로 DI 구현하는 객체

    ① FileSystemXml - 파일 경로로 지정된 곳의 xml을 읽어 설정 정보를 로딩
    ② XmlWeb - 웹 어플리케이션에 위치한 곳에서 xml파일을 읽어 설정 정보를 로딩
    ③ AnnotationConfig - @Configuration 어노테이션이 붙은 클래스를 이용하여 설정 정보로 로딩
    ④ ClassPathXml - ClassPath에 위치한 xml 파일을 읽어 설정 정보를 로딩, root로부터 경로를 지정함




5. main 코드 부분

  • getBean 메서드는 Object 타입으로 반환
public class Program {
	public static void main(String[] args) {
	    
       // ioc 컨테이너 생성 (Spring Bean Configuration File 에 지정된 대로 객체 생성)
       ApplicationContext context = new ClassPathXmlApplicationContext("spring/di/settig.xml");
       
       ExamConsole console = (ExameConsole) context.getBean("console")
       
       // ExamConsole 자료형으로 참조 가능한 객체를 찾아서 반환함 (두 개 이상이면 구분 필요)
       ExamConsole console = context.getBean(ExamConsole.class)
       
       console.print();
       
	}
}



Section 2. Constructor DI

1. Constructor-arg

  • 생성자 DI 를 구현할 때 사용하는 태그

//index 지정방법
<bean id ="exam" class = "spring.di.entity.Exam">
      <constructor-arg index="1"  value="1000"/>
      <constructor-arg index="3"  value="20"/>
      <constructor-arg index="2"  value="30"/>
</bean>


//name 지정방법
<bean id ="exam" class = "spring.di.entity.Exam">
      <constructor-arg name="kor" value="10"/>
      <constructor-arg name="eng" value="20"/>
      <constructor-arg name="math" value="30"/>
</bean>


//name + type 지정방법 (오버로딩 구분)
<bean id ="exam" class = "spring.di.entity.Exam">
      <constructor-arg name="kor" type="float" value="10"/>
      <constructor-arg name="eng" type="float" value="20"/>
      <constructor-arg name="math" type="float" value="30"/>
</bean>




2. p Namespace 사용하여 생성자 DI

  • p Namespcae 불러오기



  • 속성 지정
  • 값을 지정하지 않은 속성은 0으로 초기화됨 (int형)
<bean id ="exam" class = "spring.di.entity.Exam" p:kor="101" p:eng="10"/>




3. Namespace

  • 특정 처리기가 해당 요소를 처리하도록 지정하는 기능 제공
  • 태그를 식별하기 위한 기능 제공
  • 기본 네임스페이스의 하위 요소들은 모두 기본 네임스페이스 처리기에 처리됨
  • 단, 지정된 네임스페이스의 요소는 적용되지 않고 지정된 처리기에 처리됨

// beans 라는 이름으로 기본 네임스페이스 (처리기) 지정
beans xmlns="http://www.springframework.org/schema/beans"

// 기본 네임스페이스 외 접두어가 있는 네임스페이스 지정 (beans 처리기에 의해 처리되지 않음)
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

// 기본 네임스페이스 외 접두어가 있는 네임스페이스 지정 (beans 처리기에 의해 처리되지 않음)
xmlns:p="http://www.springframework.org/schema/p"
<bean id ="exam" class = "spring.di.entity.Exam" p:kor="101" p:eng="10"/>
	

- 참고 사이트

0개의 댓글