우당탕탕 개발일지

62일차_applicationContext.xml 본문

카테고리 없음

62일차_applicationContext.xml

ujin302 2024. 10. 2. 16:33
반응형

 

1. Interface

MessageBean.java

package sample05;

public interface MessageBean {
	public void helloCall();
}

 

 

Outputter.java

package sample05;

public interface Outputter {
	public void output(String message);
}

 

2. Class

MessageBeanImpl.java

package sample05;

public class MessageBeanImpl implements MessageBean {
	private String name;
	private String phone;
	private Outputter outputter;

	public MessageBeanImpl(String name) {
		this.name = name;
		System.out.println("MessageBeanImpl(String " + name + ")");
	}

	public void setPhone(String phone) {
		this.phone = phone;
		System.out.println("setPhone(String" + phone + ")");
	}

	public void setOutputter(Outputter outputter) {
		this.outputter = outputter;
		System.out.println("setOutputter(Outputter" + outputter + ")");
	}

	@Override
	public void helloCall() {
		System.out.println("void helloCall()");
		outputter.output("내 이름은 " + name + "이고, 폰 번호는 " + phone + "이다.");
	}
}

 

FileOutputter.java

package sample05;

import java.io.FileWriter;
import java.io.IOException;

public class FileOutputter implements Outputter {
	private String filePath;
	private String fileName;
	
	public FileOutputter() {
		System.out.println("FileOutputter()");
	}
	
	public void setFilePath(String filePath) {
		this.filePath = filePath;
		System.out.println("setFilePath(String " + filePath+ ")");
	}

	public void setFileName(String fileName) {
		this.fileName = fileName;
		System.out.println("setFileName(String" + fileName + ")");
	}

	@Override
	public void output(String message) {
		System.out.println("output(String" + message + ")");
		
		try {
			FileWriter fileWriter = new FileWriter(filePath + fileName);
			
			fileWriter.write(message);
			fileWriter.close();
			
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}

 

 

3. main 함수

HelloSpring.java

package sample05;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class HelloSpring {

	public static void main(String[] args) {
		ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		MessageBean messageBean = (MessageBean) context.getBean("messageBeanImpl2");
		messageBean.helloCall();
	}

}

 

 

bean 생성

applicationContext.xml 파일을 읽어 메모리에 Bean 생성

ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");

 

 

메모리에 Bean 잡기

  1. fileOutputter
  2. messageBeanImpl2

 

실행 순서

1. FileOutputter 클래스를 Bean 생성

  • 생성자 호출 

2. FileOutputter의 Setter 호출

  • setFilePath() 호출
  • setFileName() 호출

3. MessageBeanImpl2 클래스를 Bean 생성

  • 생성자 호출

4. MessageBeanImpl2의 Setter 호출

  • setPhone() 호출
  • setOutputter() 호출

5. helloCall() 호출

6. outputter의 output() 호출

 

 

 

순서2

  1. messageBeanImpl2
  2. fileOutputter

 

실행 순서

1. MessageBeanImpl2 클래스를 Bean 생성

  • 생성자 호출 

2. MessageBeanImpl2 클래스의 필드 변수 메모리 잡기

  • 나머지 필드 변수 확인
  • FileOutputter 객체 발견
  • FileOutputter 객체 먼저 생성 

3. FileOutputter 클래스를 Bean 생성

  • 생성자 호출

4. FileOutputter의 Setter 호출

  • setFilePath() 호출
  • setFileName() 호출

5. FileOutputter의 Setter 호출

  • setFilePath() 호출
  • setFileName() 호출

6. helloCall() 호출

7. outputter의 output() 호출

 

 

 

 

반응형