d. Bean Scopes - kimxavi/spring_tutorial GitHub Wiki

์Šคํ”„๋ง์—์„œ ์„ ์ •์˜ํ•  ๋•Œ, scope์— ๋Œ€ํ•œ ์„ ์–ธ์„ ํ•  ์ˆ˜ ์žˆ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ์Šคํ”„๋ง์—์„œ ์ƒˆ๋กœ์šด bean์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ํ•˜๋‚˜๊ฐ€ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ์ƒ์‚ฐํ•˜๋Š” ๊ฒƒ์€ bean์˜ scope ์†์„ฑ์„ prototype ์œผ๋กœ ์„ ์–ธํ•œ๋‹ค. ๊ฐ™์€ ๋ฐฉ๋ฒ•์œผ๋กœ ๊ฐ™์€ bean ์ธ์Šคํ„ด์Šค๋ฅผ ํ•„์š”ํ•  ๋•Œ๋งˆ๋‹ค ๋ฐ˜ํ™˜ ๋ฐ›๋„๋ก ํ•˜๋ ค๋ฉด scope๋ฅผ singleton๋กœ ์„ ์–ธํ•œ๋‹ค. ๋‹ค์„ฏ ๊ฐ€์ง€์˜ ์†์„ฑ์ด ์žˆ๋Š”๋ฐ ๊ทธ ์ค‘์— ์„ธ ๊ฐ€์ง€๋Š” web-aware ApplicationContext๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ๋งŒ ์“ธ ์ˆ˜ ์žˆ๋‹ค.

  • singleton : Spring IoC ์ปจํ…Œ์ด๋„ˆ๋งˆ๋‹ค ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค.(default)
  • prototype : ์–ผ๋งˆ๋“ ์ง€ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•ด์„œ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค.
  • request : HTTP ์š”์ฒญ์— ์ •์˜๋œ๋‹ค.
  • session : HTTP ์„ธ์…˜.
  • global-session : global HTTP ์„ธ์…˜.

The singleton scope

๋งŒ์•ฝ scope๊ฐ€ singleton์œผ๋กœ ์„ค์ •๋˜๋ฉด, ์Šคํ”„๋ง IoC ์ปจํ…Œ์ด๋„ˆ๋Š” ์ •ํ™•ํžˆ ํ•˜๋‚˜์˜ ๊ฐ์ฒด ์ธ์Šคํ„ด์Šค๋งŒ์„ ์ƒ์„ฑํ•œ๋‹ค. ์ด ํ•˜๋‚˜์˜ ์ธ์Šคํ„ด์Šค๋Š” singleton beans ์— ์บ์‹œ๋กœ ์ €์žฅ๋œ๋‹ค ๊ทธ๋ฆฌ๊ณ  ๊ทธ ๋‹ค์Œ์˜ ๋ชจ๋“  ์š”์ฒญ๊ณผ ์ฐธ์กฐ๋“ค์€ ์บ์‹œ๋œ ๊ฐ์ฒด๊ฐ€ ๋ฐ˜ํ™˜๋ฉ๋‹ˆ๋‹ค.

<!-- A bean definition with singleton scope -->
<bean id="..." class="..." scope="singleton">
    <!-- collaborators and configuration for this bean go here -->
</bean>

HelloWorld.java

package com.tutorialspoint;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }

   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

MainApp.java

package com.tutorialspoint;

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

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

      HelloWorld objA = (HelloWorld) context.getBean("helloWorld");

      objA.setMessage("I'm object A");
      objA.getMessage();

      HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
      objB.getMessage();
   }
}
  • ํ•œ ๋ฒˆ ๊ฐ’์„ ์„ธํŒ…ํ•œ ๊ฒƒ์ด ๋‹ค์Œ์— bean์„ ๊ฐ€์ ธ์™”์„ ๋•Œ๋„ ๊ทธ๋Œ€๋กœ ์„ค์ •๋˜์–ด ์žˆ๋‹ค.

Beans.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
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" 
      scope="singleton">
   </bean>

</beans>

PRINT

Your Message : I'm object A
Your Message : I'm object A

The prototype scope

๋งŒ์•ฝ scope๊ฐ€ prototype๋กœ ์„ค์ •๋˜์–ด ์žˆ๋‹ค๋ฉด, ์Šคํ”„๋ง IoC ์ปจํ…Œ์ด๋„ˆ๋Š” ์š”์ฒญ์ด ์˜ฌ ๋•Œ๋งˆ๋‹ค ๊ฐ์ฒด์˜ bean ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. state-full ์€ prototype์ด ์“ฐ์ด๊ณ  stateless๋Š” singleton์œผ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

HelloWorld.java

package com.tutorialspoint;

public class HelloWorld {
   private String message;

   public void setMessage(String message){
      this.message  = message;
   }

   public void getMessage(){
      System.out.println("Your Message : " + message);
   }
}

MainApp.java

package com.tutorialspoint;

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

public class MainApp {
   public static void main(String[] args) {
      ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");

      HelloWorld objA = (HelloWorld) context.getBean("helloWorld");

      objA.setMessage("I'm object A");
      objA.getMessage();

      HelloWorld objB = (HelloWorld) context.getBean("helloWorld");
      objB.getMessage();
   }
}

Beans.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
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

   <bean id="helloWorld" class="com.tutorialspoint.HelloWorld" 
      scope="prototype">
   </bean>

</beans>

PRINT

Your Message : I'm object A
Your Message : null
โš ๏ธ **GitHub.com Fallback** โš ๏ธ