🍃 Spring

🌺 Spring 시작하기.. 구조와 원리 (init-method="initMethod")

보배 진 2026. 1. 13. 12:23

applicationContext.xml 

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
		
		<bean class="test04.IPhone" id="teemo" />
		<bean class="test04.GalaxyPhone" id="ari" />
</beans>

applicationContext.xml은

pre loading 방식이 디폴트!

 

이걸 사용하는지 아닌지 관심이 없다

만약 A만 사용하면 B는 없어도 되는데 하지만 A, B 일단 둘 다 만들어 둠

 

그래서

 

<?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
        https://www.springframework.org/schema/beans/spring-beans.xsd">
		
		<bean class="test04.IPhone" id="teemo" lazy-init="true" />
		<bean class="test04.GalaxyPhone" id="ari" scope="singleton" />
</beans>

lazy-init="true" : 지연로딩

대부분 프리로딩을 사용하지만 이러한 옵션도 있다

 

선언하지 않아도 내부적으로 scope="singleton"으로 동작한다

반대는 prototype이 있다. prototype은 객체를 각각 생성한다


 

싱글톤

package test04;

import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;

// 스프링 컨테이너 도입
public class Client {
	public static void main(String[] args) {
		
		// 1- 스프링 컨테이너를 동작시킴
		AbstractApplicationContext factory = new GenericXmlApplicationContext("applicationContext.xml");
		Phone phone = (Phone)factory.getBean("ari");
		// 2- Lookup : 스프링 컨테이너에게 객체를 요청
		
		// 싱글톤 패턴! : 객체 생성이 한 번만 출력되잖슴
		Phone phone1 = (Phone)factory.getBean("ari");
		Phone phone2 = (Phone)factory.getBean("ari");
		Phone phone3 = (Phone)factory.getBean("ari");
		
		
		phone1.turnOn();
		phone2.turnOn();
		phone3.turnOn();
		
		phone.volumeUp();
		phone.volumeDown();
		phone.turnOff();
		
		// 3- 스프링 컨테이너 종료
		factory.close();
	}
}

 

현재 phone이 ari 이다.

phone1가 ari 이다

2, 3 모두 아리이다!!

 

ari가 phone과 phone1이라는 이름을 갖고 있는 것이다

ex) 본체는 하나지만 바로가기가 2개 있는 것임

 


 

 

 

init 메서드

package test04;

import java.util.Scanner;

public class IPhone implements Phone {
	// 멤버변수
	private String name;
	
	public IPhone() { // 기본 생성자 : 생성자의 역할 == 멤버변수 초기화
		System.out.println("아이폰 객체 생성");
	}
	
	// 생성자의 역할을 대체할 메서드
	// => initMethod
	public void initMethod() {
		System.out.println("아이폰 사용자의 이름을 입력하세요. >> ");
		Scanner sc = new Scanner(System.in);
		this.name = sc.next();
	}
	
	@Override
	public void turnOn() {
		System.out.println("아이폰 ON");
	}
	@Override
	public void turnOff() {
		System.out.println("아이폰 OFF");
		System.out.println(this.name);
	}
	@Override
	public void volumeUp() {
		System.out.println("아이폰 ++");
	}
	@Override
	public void volumeDown() {
		System.out.println("아이폰 --");
	}	
}

 

생성자의 역할을 대체하는 메서드를 만들고

 

<?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
    https://www.springframework.org/schema/beans/spring-beans.xsd">
		
	<bean class="test04.IPhone" id="teemo" lazy-init="true" init-method="initMethod" />
	<bean class="test04.GalaxyPhone" id="ari" scope="singleton"/>
</beans>

init-method="initMethod" : 그놈을 호출함

 

 

 

 

 

 

 

[ Console 출력 확인 ]