Spring MVC, Sptring boot

[Team Study] [싱글톤 XML ver] 스프링 싱글톤에 대한 단순한 고찰

고인돌개발자 2021. 5. 8. 14:47

학습목표 : 스프링에서의 싱글톤의 의미를 이해하고, 실제 단순한 구현을 한다.

 

Q) 스프링의 빈들이 싱글톤인 이유?
A)  스프링은 자바엔터프라이즈개발을 위한 프레임워크다.
    이 말인 즉슨, 사용자가 많다는 얘기.
   그 많은 사용자가 요청할 때마다 새로운 객체를 생성해서 제공하는 것은 비용이 크기 때문에
   기본적으로 싱글톤으로 객체의 갯수를 제한한다.

 

* 솔직하게 왜 스프링이 싱글톤을 사용하는지 잘 모르겠음...  좀더 공부가 필요

 

* 단지 스프링이 싱글톤을 사용하는 방법에 대해 공부하고자 함.

 

1) 스프링 컨테이너의 종류 2가지
      (자세한 설명 블로그 - velog.io/@ehdrms2034/Spring-MVC-Application-Context.xml )
  -  Bean Factory와 이를 상속한 ApplicationContext 2가지 유형이 존재한다.

     -> 일반적으로 스프링 프로젝트에서는 ApplicationContext 를 사용함
  

<ApplicationContext>

 

2. 단순 프로그램  (maven 프로젝트에 Spring web mvc 를 추가한 후 java 프로그램으로 테스트)

<새가 날아간다>

 

  a) ProgramSingleton.java (main 으로 사용)

      - 각 단계별로 테스트 진행 함

package com.spring.di.singleton;

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

import com.spring.di.ui.ExamConsole;

public class ProgramSingleton {

	public static void main(String[] args) {
		System.out.println("ProgramSingleton Start!!");
		
		/* Ver1
		System.out.println("------------------------!!");	
		  
		IfFly sparrow = new Sparrow();
			System.out.println(sparrow.fly());
			
		IfFly eagle = new Eagle();	
			System.out.println(eagle.fly());
		*/
		
		/* Ver2 
		System.out.println("------------------------!!");	
		//IfFly iffly = new Sparrow();
		IfFly iffly = new Eagle();
		System.out.println(iffly.fly());
		*/
		
		/* Ver3 Spring IoC */
		ApplicationContext context 
		= new ClassPathXmlApplicationContext("com/spring/di/singleton/SpringBeans.xml");
		
		System.out.println("------------------------!!");	
		
		IfFly iffly = context.getBean(Eagle.class);
			System.out.println(iffly.fly());
		
		IfFly iffly2 = context.getBean(Eagle.class);
		IfFly iffly3 = context.getBean(Eagle.class);
		System.out.println("iffly "+iffly);
		System.out.println("iffly2 "+iffly2);
		System.out.println("iffly3 "+iffly3);

		
		System.out.println("ProgramSingleton End!!");		
	}

}

b. Bird.java (상위 클래스 생성)

package com.spring.di.singleton;

public class Bird {

	String myName;
	
	public Bird() {
		System.out.println("Bird Constructor!!");
		this.myName = "Bird";
	}
	
}

c. Sparrow.java (하위 클래스)

package com.spring.di.singleton;

public class Sparrow extends Bird implements IfFly {
	
	public Sparrow() {
		System.out.println("Sparrow Constructor!!");
		myName = "Sparrow";
	}

	@Override
	public String fly() {
		return myName +" can fly.";
	}

}

d. Eagle.java (하위클래스)

package com.spring.di.singleton;

public class Eagle extends Bird implements IfFly {
	
	public Eagle() {
		System.out.println("Eagle Constructor!!");
		myName = "Eagle";
	}

	@Override
	public String fly() {
		return myName +" can fly.";
	}

}

e. IfFly.java (인터페이스)

package com.spring.di.singleton;

public interface IfFly {
	
	String fly();
}

f. SpringBeans.xml (Beans 생성)

<?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">

	<!-- IfFly iffly = new Sparrow() -->
	<!-- <bean id="iffly" class="com.spring.di.singleton.Eagle" /> -->
	
	<bean id="eagle" class="com.spring.di.singleton.Eagle" />
	<bean id="sparrow" class="com.spring.di.singleton.Sparrow" />
		
</beans>

 

Java Application 으로 실행하고, 

해당 버전마다 생성자의 실행이 언제되는지 보는것도 좋을것 같다.